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