blob: 3ad41c6b1af90252aafecb4eabf3b52a38c26e4f [file] [log] [blame]
SurenNeware61859092020-10-01 09:37:32 +05301import api from '@/store/api';
Derick Montaguefded0d12019-12-11 06:16:40 -06002import Cookies from 'js-cookie';
SurenNeware61859092020-10-01 09:37:32 +05303import router from '@/router';
Paul Fertser2b335262024-04-11 10:51:41 +00004import { roles } from '@/router/routes';
Derick Montaguee080a1a2019-12-04 16:30:08 -06005
6const AuthenticationStore = {
7 namespaced: true,
8 state: {
kirankumarb07b89eed22023-01-12 15:50:30 +05309 consoleWindow: null,
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080010 authError: false,
Yoshie Muranakad624dae2020-09-21 14:35:58 -070011 xsrfCookie: Cookies.get('XSRF-TOKEN'),
Derick Montague602e98a2020-10-21 16:20:00 -050012 isAuthenticatedCookie: Cookies.get('IsAuthenticated'),
Paul Fertser1ff8e892024-06-10 08:51:51 +000013 sessionURI: localStorage.getItem('sessionURI'),
Derick Montaguee080a1a2019-12-04 16:30:08 -060014 },
15 getters: {
kirankumarb07b89eed22023-01-12 15:50:30 +053016 consoleWindow: (state) => state.consoleWindow,
Derick Montague602e98a2020-10-21 16:20:00 -050017 authError: (state) => state.authError,
18 isLoggedIn: (state) => {
Paul Fertser1ff8e892024-06-10 08:51:51 +000019 // We might have gotten XSRF-TOKEN (and HttpOnly SESSION cookie) by Mutual TLS authentication,
20 // without going through explicit Session creation
Yoshie Muranakad624dae2020-09-21 14:35:58 -070021 return (
22 state.xsrfCookie !== undefined || state.isAuthenticatedCookie == 'true'
23 );
24 },
Paul Fertser1ff8e892024-06-10 08:51:51 +000025 // Used to authenticate WebSocket connections via subprotocol value
Derick Montague602e98a2020-10-21 16:20:00 -050026 token: (state) => state.xsrfCookie,
Derick Montaguee080a1a2019-12-04 16:30:08 -060027 },
28 mutations: {
Paul Fertser1ff8e892024-06-10 08:51:51 +000029 authSuccess(state, { session }) {
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080030 state.authError = false;
Yoshie Muranakad624dae2020-09-21 14:35:58 -070031 state.xsrfCookie = Cookies.get('XSRF-TOKEN');
Paul Fertser1ff8e892024-06-10 08:51:51 +000032 // Preserve session data across page reloads and browser restarts
33 localStorage.setItem('sessionURI', session);
34 state.sessionURI = session;
Derick Montaguee080a1a2019-12-04 16:30:08 -060035 },
Derick Montaguea06fe462020-03-11 13:48:42 -050036 authError(state, authError = true) {
37 state.authError = authError;
Derick Montague676f2fc2019-12-23 20:53:49 -060038 },
Yoshie Muranakad624dae2020-09-21 14:35:58 -070039 logout(state) {
Derick Montaguefded0d12019-12-11 06:16:40 -060040 Cookies.remove('XSRF-TOKEN');
Yoshie Muranakad624dae2020-09-21 14:35:58 -070041 Cookies.remove('IsAuthenticated');
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +053042 localStorage.removeItem('storedUsername');
Yoshie Muranakad624dae2020-09-21 14:35:58 -070043 state.xsrfCookie = undefined;
44 state.isAuthenticatedCookie = undefined;
Paul Fertser1ff8e892024-06-10 08:51:51 +000045 localStorage.removeItem('sessionURI');
46 state.sessionURI = null;
47 state.consoleWindow = false;
Derick Montague602e98a2020-10-21 16:20:00 -050048 },
Derick Montaguee080a1a2019-12-04 16:30:08 -060049 },
50 actions: {
Yoshie Muranakad624dae2020-09-21 14:35:58 -070051 login({ commit }, { username, password }) {
Derick Montaguea06fe462020-03-11 13:48:42 -050052 commit('authError', false);
Derick Montaguee080a1a2019-12-04 16:30:08 -060053 return api
Paul Fertser1ff8e892024-06-10 08:51:51 +000054 .post('/redfish/v1/SessionService/Sessions', {
55 UserName: username,
56 Password: password,
Ed Tanousebef6ee2023-08-07 18:25:41 -070057 })
Paul Fertser1ff8e892024-06-10 08:51:51 +000058 .then((response) => {
59 commit('authSuccess', {
60 session: response.headers['location'],
61 });
62 })
Derick Montague602e98a2020-10-21 16:20:00 -050063 .catch((error) => {
Derick Montaguefded0d12019-12-11 06:16:40 -060064 commit('authError');
Derick Montaguee080a1a2019-12-04 16:30:08 -060065 throw new Error(error);
66 });
67 },
Paul Fertser1ff8e892024-06-10 08:51:51 +000068 logout({ commit, state }) {
Yoshie Muranaka6ce1a072019-12-06 14:13:59 -080069 api
Paul Fertser1ff8e892024-06-10 08:51:51 +000070 .delete(state.sessionURI)
71 .then(() => commit('logout'))
Thang Q. Nguyen780733a2023-04-19 14:27:18 +070072 .then(() => router.push('/login'))
Derick Montague602e98a2020-10-21 16:20:00 -050073 .catch((error) => console.log(error));
Yoshie Muranaka2c98b092020-06-22 13:28:09 -070074 },
Paul Fertserbceaffa2024-04-10 16:27:53 +000075 getUserInfo({ commit }, username) {
Damian Celicoaeb19812022-11-24 02:00:53 +010076 return api
Yoshie Muranaka2c98b092020-06-22 13:28:09 -070077 .get(`/redfish/v1/AccountService/Accounts/${username}`)
Paul Fertserbceaffa2024-04-10 16:27:53 +000078 .then(({ data }) => {
79 commit('global/setPrivilege', data.RoleId, { root: true });
80 return data;
81 })
Paul Fertser2b335262024-04-11 10:51:41 +000082 .catch((error) => {
83 if (error.response?.status === 404) {
84 // We have valid credentials but user isn't known, assume remote
85 // authentication (e.g. LDAP) and do not restrict the routing
86 commit('global/setPrivilege', roles.administrator, { root: true });
87 return {};
88 } else {
89 console.log(error);
90 }
91 });
Yoshie Muranakad624dae2020-09-21 14:35:58 -070092 },
93 resetStoreState({ state }) {
94 state.authError = false;
95 state.xsrfCookie = Cookies.get('XSRF-TOKEN');
96 state.isAuthenticatedCookie = Cookies.get('IsAuthenticated');
Derick Montague602e98a2020-10-21 16:20:00 -050097 },
98 },
Derick Montaguee080a1a2019-12-04 16:30:08 -060099};
100
101export default AuthenticationStore;