blob: 3122ab2f7c702756f3baef4543127616c1f6ef32 [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'),
Paul Fertserbfe7ad82024-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 Fertser630ce392024-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 Fertserbfe7ad82024-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 Fertser630ce392024-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 Fertserbfe7ad82024-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 Fertser630ce392024-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 Fertserbfe7ad82024-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 Fertserbfe7ad82024-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 Fertser630ce392024-06-10 08:51:51 +000057 localStorage.removeItem('sessionURI');
58 state.sessionURI = null;
Paul Fertserbfe7ad82024-07-03 14:11:03 +000059 state.xAuthToken = null;
Paul Fertser630ce392024-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 Fertser630ce392024-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 Fertser630ce392024-06-10 08:51:51 +000071 .then((response) => {
72 commit('authSuccess', {
73 session: response.headers['location'],
Paul Fertserbfe7ad82024-07-03 14:11:03 +000074 token: response.headers['x-auth-token'],
Paul Fertser630ce392024-06-10 08:51:51 +000075 });
Paul Fertser83d70f72024-07-05 10:46:38 +000076 return isPasswordExpired(response);
Paul Fertser630ce392024-06-10 08:51:51 +000077 })
Derick Montague602e98a2020-10-21 16:20:00 -050078 .catch((error) => {
Derick Montaguefded0d12019-12-11 06:16:40 -060079 commit('authError');
Derick Montaguee080a1a2019-12-04 16:30:08 -060080 throw new Error(error);
81 });
82 },
Paul Fertser630ce392024-06-10 08:51:51 +000083 logout({ commit, state }) {
Yoshie Muranaka6ce1a072019-12-06 14:13:59 -080084 api
Paul Fertser630ce392024-06-10 08:51:51 +000085 .delete(state.sessionURI)
86 .then(() => commit('logout'))
Thang Q. Nguyen780733a2023-04-19 14:27:18 +070087 .then(() => router.push('/login'))
Derick Montague602e98a2020-10-21 16:20:00 -050088 .catch((error) => console.log(error));
Yoshie Muranaka2c98b092020-06-22 13:28:09 -070089 },
Paul Fertserbceaffa2024-04-10 16:27:53 +000090 getUserInfo({ commit }, username) {
Damian Celicoaeb19812022-11-24 02:00:53 +010091 return api
Yoshie Muranaka2c98b092020-06-22 13:28:09 -070092 .get(`/redfish/v1/AccountService/Accounts/${username}`)
Paul Fertserbceaffa2024-04-10 16:27:53 +000093 .then(({ data }) => {
94 commit('global/setPrivilege', data.RoleId, { root: true });
95 return data;
96 })
Paul Fertser2b335262024-04-11 10:51:41 +000097 .catch((error) => {
98 if (error.response?.status === 404) {
99 // We have valid credentials but user isn't known, assume remote
100 // authentication (e.g. LDAP) and do not restrict the routing
101 commit('global/setPrivilege', roles.administrator, { root: true });
102 return {};
103 } else {
104 console.log(error);
105 }
106 });
Yoshie Muranakad624dae2020-09-21 14:35:58 -0700107 },
108 resetStoreState({ state }) {
109 state.authError = false;
110 state.xsrfCookie = Cookies.get('XSRF-TOKEN');
111 state.isAuthenticatedCookie = Cookies.get('IsAuthenticated');
Derick Montague602e98a2020-10-21 16:20:00 -0500112 },
113 },
Derick Montaguee080a1a2019-12-04 16:30:08 -0600114};
115
116export default AuthenticationStore;