blob: b64def06953a6d7ab2c0d0e909d970f29b4b98b8 [file] [log] [blame]
Paul Fertser83d70f72024-07-05 10:46:38 +00001import api, { isPasswordExpired } 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 Fertser630ce392024-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 Fertser630ce392024-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 Fertser630ce392024-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 Fertser630ce392024-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 Fertser630ce392024-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 Fertser630ce392024-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 Fertser630ce392024-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 Fertser630ce392024-06-10 08:51:51 +000058 .then((response) => {
59 commit('authSuccess', {
60 session: response.headers['location'],
61 });
Paul Fertser83d70f72024-07-05 10:46:38 +000062 return isPasswordExpired(response);
Paul Fertser630ce392024-06-10 08:51:51 +000063 })
Derick Montague602e98a2020-10-21 16:20:00 -050064 .catch((error) => {
Derick Montaguefded0d12019-12-11 06:16:40 -060065 commit('authError');
Derick Montaguee080a1a2019-12-04 16:30:08 -060066 throw new Error(error);
67 });
68 },
Paul Fertser630ce392024-06-10 08:51:51 +000069 logout({ commit, state }) {
Yoshie Muranaka6ce1a072019-12-06 14:13:59 -080070 api
Paul Fertser630ce392024-06-10 08:51:51 +000071 .delete(state.sessionURI)
72 .then(() => commit('logout'))
Thang Q. Nguyen780733a2023-04-19 14:27:18 +070073 .then(() => router.push('/login'))
Derick Montague602e98a2020-10-21 16:20:00 -050074 .catch((error) => console.log(error));
Yoshie Muranaka2c98b092020-06-22 13:28:09 -070075 },
Paul Fertserbceaffa2024-04-10 16:27:53 +000076 getUserInfo({ commit }, username) {
Damian Celicoaeb19812022-11-24 02:00:53 +010077 return api
Yoshie Muranaka2c98b092020-06-22 13:28:09 -070078 .get(`/redfish/v1/AccountService/Accounts/${username}`)
Paul Fertserbceaffa2024-04-10 16:27:53 +000079 .then(({ data }) => {
80 commit('global/setPrivilege', data.RoleId, { root: true });
81 return data;
82 })
Paul Fertser2b335262024-04-11 10:51:41 +000083 .catch((error) => {
84 if (error.response?.status === 404) {
85 // We have valid credentials but user isn't known, assume remote
86 // authentication (e.g. LDAP) and do not restrict the routing
87 commit('global/setPrivilege', roles.administrator, { root: true });
88 return {};
89 } else {
90 console.log(error);
91 }
92 });
Yoshie Muranakad624dae2020-09-21 14:35:58 -070093 },
94 resetStoreState({ state }) {
95 state.authError = false;
96 state.xsrfCookie = Cookies.get('XSRF-TOKEN');
97 state.isAuthenticatedCookie = Cookies.get('IsAuthenticated');
Derick Montague602e98a2020-10-21 16:20:00 -050098 },
99 },
Derick Montaguee080a1a2019-12-04 16:30:08 -0600100};
101
102export default AuthenticationStore;