SurenNeware | 6185909 | 2020-10-01 09:37:32 +0530 | [diff] [blame] | 1 | import api from '@/store/api'; |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 2 | import Cookies from 'js-cookie'; |
SurenNeware | 6185909 | 2020-10-01 09:37:32 +0530 | [diff] [blame] | 3 | import router from '@/router'; |
Paul Fertser | 2b33526 | 2024-04-11 10:51:41 +0000 | [diff] [blame] | 4 | import { roles } from '@/router/routes'; |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 5 | |
| 6 | const AuthenticationStore = { |
| 7 | namespaced: true, |
| 8 | state: { |
kirankumarb07 | b89eed2 | 2023-01-12 15:50:30 +0530 | [diff] [blame] | 9 | consoleWindow: null, |
Yoshie Muranaka | 4b0fc1d | 2020-01-06 07:36:16 -0800 | [diff] [blame] | 10 | authError: false, |
Yoshie Muranaka | d624dae | 2020-09-21 14:35:58 -0700 | [diff] [blame] | 11 | xsrfCookie: Cookies.get('XSRF-TOKEN'), |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 12 | isAuthenticatedCookie: Cookies.get('IsAuthenticated'), |
Paul Fertser | 1ff8e89 | 2024-06-10 08:51:51 +0000 | [diff] [blame] | 13 | sessionURI: localStorage.getItem('sessionURI'), |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 14 | }, |
| 15 | getters: { |
kirankumarb07 | b89eed2 | 2023-01-12 15:50:30 +0530 | [diff] [blame] | 16 | consoleWindow: (state) => state.consoleWindow, |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 17 | authError: (state) => state.authError, |
| 18 | isLoggedIn: (state) => { |
Paul Fertser | 1ff8e89 | 2024-06-10 08:51:51 +0000 | [diff] [blame] | 19 | // We might have gotten XSRF-TOKEN (and HttpOnly SESSION cookie) by Mutual TLS authentication, |
| 20 | // without going through explicit Session creation |
Yoshie Muranaka | d624dae | 2020-09-21 14:35:58 -0700 | [diff] [blame] | 21 | return ( |
| 22 | state.xsrfCookie !== undefined || state.isAuthenticatedCookie == 'true' |
| 23 | ); |
| 24 | }, |
Paul Fertser | 1ff8e89 | 2024-06-10 08:51:51 +0000 | [diff] [blame] | 25 | // Used to authenticate WebSocket connections via subprotocol value |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 26 | token: (state) => state.xsrfCookie, |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 27 | }, |
| 28 | mutations: { |
Paul Fertser | 1ff8e89 | 2024-06-10 08:51:51 +0000 | [diff] [blame] | 29 | authSuccess(state, { session }) { |
Yoshie Muranaka | 4b0fc1d | 2020-01-06 07:36:16 -0800 | [diff] [blame] | 30 | state.authError = false; |
Yoshie Muranaka | d624dae | 2020-09-21 14:35:58 -0700 | [diff] [blame] | 31 | state.xsrfCookie = Cookies.get('XSRF-TOKEN'); |
Paul Fertser | 1ff8e89 | 2024-06-10 08:51:51 +0000 | [diff] [blame] | 32 | // Preserve session data across page reloads and browser restarts |
| 33 | localStorage.setItem('sessionURI', session); |
| 34 | state.sessionURI = session; |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 35 | }, |
Derick Montague | a06fe46 | 2020-03-11 13:48:42 -0500 | [diff] [blame] | 36 | authError(state, authError = true) { |
| 37 | state.authError = authError; |
Derick Montague | 676f2fc | 2019-12-23 20:53:49 -0600 | [diff] [blame] | 38 | }, |
Yoshie Muranaka | d624dae | 2020-09-21 14:35:58 -0700 | [diff] [blame] | 39 | logout(state) { |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 40 | Cookies.remove('XSRF-TOKEN'); |
Yoshie Muranaka | d624dae | 2020-09-21 14:35:58 -0700 | [diff] [blame] | 41 | Cookies.remove('IsAuthenticated'); |
Sukanya Pandey | b1f559f | 2020-04-28 20:18:28 +0530 | [diff] [blame] | 42 | localStorage.removeItem('storedUsername'); |
Yoshie Muranaka | d624dae | 2020-09-21 14:35:58 -0700 | [diff] [blame] | 43 | state.xsrfCookie = undefined; |
| 44 | state.isAuthenticatedCookie = undefined; |
Paul Fertser | 1ff8e89 | 2024-06-10 08:51:51 +0000 | [diff] [blame] | 45 | localStorage.removeItem('sessionURI'); |
| 46 | state.sessionURI = null; |
| 47 | state.consoleWindow = false; |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 48 | }, |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 49 | }, |
| 50 | actions: { |
Yoshie Muranaka | d624dae | 2020-09-21 14:35:58 -0700 | [diff] [blame] | 51 | login({ commit }, { username, password }) { |
Derick Montague | a06fe46 | 2020-03-11 13:48:42 -0500 | [diff] [blame] | 52 | commit('authError', false); |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 53 | return api |
Paul Fertser | 1ff8e89 | 2024-06-10 08:51:51 +0000 | [diff] [blame] | 54 | .post('/redfish/v1/SessionService/Sessions', { |
| 55 | UserName: username, |
| 56 | Password: password, |
Ed Tanous | ebef6ee | 2023-08-07 18:25:41 -0700 | [diff] [blame] | 57 | }) |
Paul Fertser | 1ff8e89 | 2024-06-10 08:51:51 +0000 | [diff] [blame] | 58 | .then((response) => { |
| 59 | commit('authSuccess', { |
| 60 | session: response.headers['location'], |
| 61 | }); |
| 62 | }) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 63 | .catch((error) => { |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 64 | commit('authError'); |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 65 | throw new Error(error); |
| 66 | }); |
| 67 | }, |
Paul Fertser | 1ff8e89 | 2024-06-10 08:51:51 +0000 | [diff] [blame] | 68 | logout({ commit, state }) { |
Yoshie Muranaka | 6ce1a07 | 2019-12-06 14:13:59 -0800 | [diff] [blame] | 69 | api |
Paul Fertser | 1ff8e89 | 2024-06-10 08:51:51 +0000 | [diff] [blame] | 70 | .delete(state.sessionURI) |
| 71 | .then(() => commit('logout')) |
Thang Q. Nguyen | 780733a | 2023-04-19 14:27:18 +0700 | [diff] [blame] | 72 | .then(() => router.push('/login')) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 73 | .catch((error) => console.log(error)); |
Yoshie Muranaka | 2c98b09 | 2020-06-22 13:28:09 -0700 | [diff] [blame] | 74 | }, |
Paul Fertser | bceaffa | 2024-04-10 16:27:53 +0000 | [diff] [blame] | 75 | getUserInfo({ commit }, username) { |
Damian Celico | aeb1981 | 2022-11-24 02:00:53 +0100 | [diff] [blame] | 76 | return api |
Yoshie Muranaka | 2c98b09 | 2020-06-22 13:28:09 -0700 | [diff] [blame] | 77 | .get(`/redfish/v1/AccountService/Accounts/${username}`) |
Paul Fertser | bceaffa | 2024-04-10 16:27:53 +0000 | [diff] [blame] | 78 | .then(({ data }) => { |
| 79 | commit('global/setPrivilege', data.RoleId, { root: true }); |
| 80 | return data; |
| 81 | }) |
Paul Fertser | 2b33526 | 2024-04-11 10:51:41 +0000 | [diff] [blame] | 82 | .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 Muranaka | d624dae | 2020-09-21 14:35:58 -0700 | [diff] [blame] | 92 | }, |
| 93 | resetStoreState({ state }) { |
| 94 | state.authError = false; |
| 95 | state.xsrfCookie = Cookies.get('XSRF-TOKEN'); |
| 96 | state.isAuthenticatedCookie = Cookies.get('IsAuthenticated'); |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 97 | }, |
| 98 | }, |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | export default AuthenticationStore; |