blob: e876f780e178fc11bcb232962ab680f8da5c30af [file] [log] [blame]
Paul Fertser6de03412024-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 Fertser1ff8e892024-06-10 08:51:51 +000013 sessionURI: localStorage.getItem('sessionURI'),
Paul Fertser09a3b9e2024-07-03 14:11:03 +000014 xAuthToken: null,
Derick Montaguee080a1a2019-12-04 16:30:08 -060015 },
16 getters: {
kirankumarb07b89eed22023-01-12 15:50:30 +053017 consoleWindow: (state) => state.consoleWindow,
Derick Montague602e98a2020-10-21 16:20:00 -050018 authError: (state) => state.authError,
19 isLoggedIn: (state) => {
Paul Fertser1ff8e892024-06-10 08:51:51 +000020 // We might have gotten XSRF-TOKEN (and HttpOnly SESSION cookie) by Mutual TLS authentication,
21 // without going through explicit Session creation
Yoshie Muranakad624dae2020-09-21 14:35:58 -070022 return (
Paul Fertser09a3b9e2024-07-03 14:11:03 +000023 state.xsrfCookie !== undefined ||
24 state.isAuthenticatedCookie == 'true' ||
25 state.xAuthToken !== null
Yoshie Muranakad624dae2020-09-21 14:35:58 -070026 );
27 },
Paul Fertser1ff8e892024-06-10 08:51:51 +000028 // Used to authenticate WebSocket connections via subprotocol value
Derick Montague602e98a2020-10-21 16:20:00 -050029 token: (state) => state.xsrfCookie,
Derick Montaguee080a1a2019-12-04 16:30:08 -060030 },
31 mutations: {
Paul Fertser09a3b9e2024-07-03 14:11:03 +000032 authSuccess(state, { session, token }) {
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080033 state.authError = false;
Yoshie Muranakad624dae2020-09-21 14:35:58 -070034 state.xsrfCookie = Cookies.get('XSRF-TOKEN');
Paul Fertser1ff8e892024-06-10 08:51:51 +000035 // Preserve session data across page reloads and browser restarts
36 localStorage.setItem('sessionURI', session);
37 state.sessionURI = session;
Paul Fertser09a3b9e2024-07-03 14:11:03 +000038 // If we didn't get the XSRF cookie it means we are talking to a
39 // Redfish implementation that is not bmcweb. In this case get the token
40 // from headers and send it with the future requests, do not permanently
41 // save anywhere.
42 if (state.xsrfCookie === undefined) {
43 api.set_auth_token(token);
44 state.xAuthToken = token;
45 }
Derick Montaguee080a1a2019-12-04 16:30:08 -060046 },
Derick Montaguea06fe462020-03-11 13:48:42 -050047 authError(state, authError = true) {
48 state.authError = authError;
Derick Montague676f2fc2019-12-23 20:53:49 -060049 },
Yoshie Muranakad624dae2020-09-21 14:35:58 -070050 logout(state) {
Derick Montaguefded0d12019-12-11 06:16:40 -060051 Cookies.remove('XSRF-TOKEN');
Yoshie Muranakad624dae2020-09-21 14:35:58 -070052 Cookies.remove('IsAuthenticated');
Paul Fertser09a3b9e2024-07-03 14:11:03 +000053 api.set_auth_token(undefined);
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +053054 localStorage.removeItem('storedUsername');
Yoshie Muranakad624dae2020-09-21 14:35:58 -070055 state.xsrfCookie = undefined;
56 state.isAuthenticatedCookie = undefined;
Paul Fertser1ff8e892024-06-10 08:51:51 +000057 localStorage.removeItem('sessionURI');
58 state.sessionURI = null;
Paul Fertser09a3b9e2024-07-03 14:11:03 +000059 state.xAuthToken = null;
Paul Fertser1ff8e892024-06-10 08:51:51 +000060 state.consoleWindow = false;
Derick Montague602e98a2020-10-21 16:20:00 -050061 },
Derick Montaguee080a1a2019-12-04 16:30:08 -060062 },
63 actions: {
Yoshie Muranakad624dae2020-09-21 14:35:58 -070064 login({ commit }, { username, password }) {
Derick Montaguea06fe462020-03-11 13:48:42 -050065 commit('authError', false);
Derick Montaguee080a1a2019-12-04 16:30:08 -060066 return api
Paul Fertser1ff8e892024-06-10 08:51:51 +000067 .post('/redfish/v1/SessionService/Sessions', {
68 UserName: username,
69 Password: password,
Ed Tanousebef6ee2023-08-07 18:25:41 -070070 })
Paul Fertserce7db822024-07-05 11:04:04 +000071 .then(({ headers, data }) => {
Paul Fertser1ff8e892024-06-10 08:51:51 +000072 commit('authSuccess', {
Paul Fertserce7db822024-07-05 11:04:04 +000073 session: headers['location'],
74 token: headers['x-auth-token'],
Paul Fertser1ff8e892024-06-10 08:51:51 +000075 });
Paul Fertserce7db822024-07-05 11:04:04 +000076 setSessionPrivilege(commit, data);
77 return isPasswordExpired(data);
Paul Fertser1ff8e892024-06-10 08:51:51 +000078 })
Derick Montague602e98a2020-10-21 16:20:00 -050079 .catch((error) => {
Derick Montaguefded0d12019-12-11 06:16:40 -060080 commit('authError');
Derick Montaguee080a1a2019-12-04 16:30:08 -060081 throw new Error(error);
82 });
83 },
Paul Fertser1ff8e892024-06-10 08:51:51 +000084 logout({ commit, state }) {
Yoshie Muranaka6ce1a072019-12-06 14:13:59 -080085 api
Paul Fertser1ff8e892024-06-10 08:51:51 +000086 .delete(state.sessionURI)
Paul Fertserce7db822024-07-05 11:04:04 +000087 .catch(() =>
88 console.log(
89 "Couldn't DELETE Session, proceeding with the logout anyway to get in sync with the backend.",
90 ),
91 )
Paul Fertser1ff8e892024-06-10 08:51:51 +000092 .then(() => commit('logout'))
Thang Q. Nguyen780733a2023-04-19 14:27:18 +070093 .then(() => router.push('/login'))
Derick Montague602e98a2020-10-21 16:20:00 -050094 .catch((error) => console.log(error));
Yoshie Muranaka2c98b092020-06-22 13:28:09 -070095 },
Paul Fertserce7db822024-07-05 11:04:04 +000096 getSessionPrivilege({ commit, state }) {
Damian Celicoaeb19812022-11-24 02:00:53 +010097 return api
Paul Fertserce7db822024-07-05 11:04:04 +000098 .get(state.sessionURI)
99 .then(({ data }) => setSessionPrivilege(commit, data));
Yoshie Muranakad624dae2020-09-21 14:35:58 -0700100 },
101 resetStoreState({ state }) {
102 state.authError = false;
103 state.xsrfCookie = Cookies.get('XSRF-TOKEN');
104 state.isAuthenticatedCookie = Cookies.get('IsAuthenticated');
Derick Montague602e98a2020-10-21 16:20:00 -0500105 },
106 },
Derick Montaguee080a1a2019-12-04 16:30:08 -0600107};
108
Paul Fertserce7db822024-07-05 11:04:04 +0000109const setSessionPrivilege = (commit, data) => {
110 // If the backend didn't provide the role information in the Session object
111 // our best bet is to assume the Administrator role to avoid hiding
112 // potentially useful UI elements. Everything security-sensitive is validated
113 // on the backend side anyway, so this is safe.
114 commit('global/setPrivilege', data.Roles?.[0] ?? roles.administrator, {
115 root: true,
116 });
117};
118
Derick Montaguee080a1a2019-12-04 16:30:08 -0600119export default AuthenticationStore;