blob: 2006661bfde2d316d7d76211165ccb00158d770a [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'),
Derick Montaguee080a1a2019-12-04 16:30:08 -060013 },
14 getters: {
kirankumarb07b89eed22023-01-12 15:50:30 +053015 consoleWindow: (state) => state.consoleWindow,
Derick Montague602e98a2020-10-21 16:20:00 -050016 authError: (state) => state.authError,
17 isLoggedIn: (state) => {
Yoshie Muranakad624dae2020-09-21 14:35:58 -070018 return (
19 state.xsrfCookie !== undefined || state.isAuthenticatedCookie == 'true'
20 );
21 },
Derick Montague602e98a2020-10-21 16:20:00 -050022 token: (state) => state.xsrfCookie,
Derick Montaguee080a1a2019-12-04 16:30:08 -060023 },
24 mutations: {
Yoshie Muranaka6ce1a072019-12-06 14:13:59 -080025 authSuccess(state) {
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080026 state.authError = false;
Yoshie Muranakad624dae2020-09-21 14:35:58 -070027 state.xsrfCookie = Cookies.get('XSRF-TOKEN');
Derick Montaguee080a1a2019-12-04 16:30:08 -060028 },
Derick Montaguea06fe462020-03-11 13:48:42 -050029 authError(state, authError = true) {
30 state.authError = authError;
Derick Montague676f2fc2019-12-23 20:53:49 -060031 },
Yoshie Muranakad624dae2020-09-21 14:35:58 -070032 logout(state) {
Derick Montaguefded0d12019-12-11 06:16:40 -060033 Cookies.remove('XSRF-TOKEN');
Yoshie Muranakad624dae2020-09-21 14:35:58 -070034 Cookies.remove('IsAuthenticated');
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +053035 localStorage.removeItem('storedUsername');
Yoshie Muranakad624dae2020-09-21 14:35:58 -070036 state.xsrfCookie = undefined;
37 state.isAuthenticatedCookie = undefined;
Derick Montague602e98a2020-10-21 16:20:00 -050038 },
kirankumarb07b89eed22023-01-12 15:50:30 +053039 setConsoleWindow: (state, window) => (state.consoleWindow = window),
Derick Montaguee080a1a2019-12-04 16:30:08 -060040 },
41 actions: {
Yoshie Muranakad624dae2020-09-21 14:35:58 -070042 login({ commit }, { username, password }) {
Derick Montaguea06fe462020-03-11 13:48:42 -050043 commit('authError', false);
Derick Montaguee080a1a2019-12-04 16:30:08 -060044 return api
Ed Tanousebef6ee2023-08-07 18:25:41 -070045 .post('/login', {
46 username: username,
47 password: password,
48 })
Derick Montaguefded0d12019-12-11 06:16:40 -060049 .then(() => commit('authSuccess'))
Derick Montague602e98a2020-10-21 16:20:00 -050050 .catch((error) => {
Derick Montaguefded0d12019-12-11 06:16:40 -060051 commit('authError');
Derick Montaguee080a1a2019-12-04 16:30:08 -060052 throw new Error(error);
53 });
54 },
55 logout({ commit }) {
Yoshie Muranaka6ce1a072019-12-06 14:13:59 -080056 api
Derick Montaguefded0d12019-12-11 06:16:40 -060057 .post('/logout', { data: [] })
kirankumarb07b89eed22023-01-12 15:50:30 +053058 .then(() => {
59 commit('setConsoleWindow', false);
60 commit('logout');
61 })
Thang Q. Nguyen780733a2023-04-19 14:27:18 +070062 .then(() => router.push('/login'))
Derick Montague602e98a2020-10-21 16:20:00 -050063 .catch((error) => console.log(error));
Yoshie Muranaka2c98b092020-06-22 13:28:09 -070064 },
Paul Fertserbceaffa2024-04-10 16:27:53 +000065 getUserInfo({ commit }, username) {
Damian Celicoaeb19812022-11-24 02:00:53 +010066 return api
Yoshie Muranaka2c98b092020-06-22 13:28:09 -070067 .get(`/redfish/v1/AccountService/Accounts/${username}`)
Paul Fertserbceaffa2024-04-10 16:27:53 +000068 .then(({ data }) => {
69 commit('global/setPrivilege', data.RoleId, { root: true });
70 return data;
71 })
Paul Fertser2b335262024-04-11 10:51:41 +000072 .catch((error) => {
73 if (error.response?.status === 404) {
74 // We have valid credentials but user isn't known, assume remote
75 // authentication (e.g. LDAP) and do not restrict the routing
76 commit('global/setPrivilege', roles.administrator, { root: true });
77 return {};
78 } else {
79 console.log(error);
80 }
81 });
Yoshie Muranakad624dae2020-09-21 14:35:58 -070082 },
83 resetStoreState({ state }) {
84 state.authError = false;
85 state.xsrfCookie = Cookies.get('XSRF-TOKEN');
86 state.isAuthenticatedCookie = Cookies.get('IsAuthenticated');
Derick Montague602e98a2020-10-21 16:20:00 -050087 },
88 },
Derick Montaguee080a1a2019-12-04 16:30:08 -060089};
90
91export default AuthenticationStore;