X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/d5fb635085181a3cc16dd2f3224c8e6fc8924964..72ee613468c6dee53b98ed1469ce9781a942dbe9:/src/store/auth/auth-reducer.ts diff --git a/src/store/auth/auth-reducer.ts b/src/store/auth/auth-reducer.ts index e58b2535e6..946407fe24 100644 --- a/src/store/auth/auth-reducer.ts +++ b/src/store/auth/auth-reducer.ts @@ -2,56 +2,104 @@ // // SPDX-License-Identifier: AGPL-3.0 -import actions, { AuthAction } from "./auth-action"; -import { User } from "../../models/user"; -import { authService } from "../../services/services"; -import { removeServerApiAuthorizationHeader, setServerApiAuthorizationHeader } from "../../common/api/server-api"; -import { UserDetailsResponse } from "../../services/auth-service/auth-service"; +import { authActions, AuthAction } from "./auth-action"; +import { User } from "~/models/user"; +import { ServiceRepository } from "~/services/services"; +import { SshKeyResource } from '~/models/ssh-key'; +import { Session } from "~/models/session"; +import { Config, mockConfig } from '~/common/config'; export interface AuthState { user?: User; apiToken?: string; + sshKeys: SshKeyResource[]; + sessions: Session[]; + localCluster: string; + homeCluster: string; + loginCluster: string; + remoteHosts: { [key: string]: string }; + remoteHostsConfig: { [key: string]: Config }; + config: Config; +} + +const initialState: AuthState = { + user: undefined, + apiToken: undefined, + sshKeys: [], + sessions: [], + localCluster: "", + homeCluster: "", + loginCluster: "", + remoteHosts: {}, + remoteHostsConfig: {}, + config: mockConfig({}) }; -const authReducer = (state: AuthState = {}, action: AuthAction) => { - return actions.match(action, { - SAVE_API_TOKEN: (token: string) => { - authService.saveApiToken(token); - setServerApiAuthorizationHeader(token); - return {...state, apiToken: token}; - }, - INIT: () => { - const user = authService.getUser(); - const token = authService.getApiToken(); - if (token) { - setServerApiAuthorizationHeader(token); - } - return {user, apiToken: token}; +export const authReducer = (services: ServiceRepository) => (state = initialState, action: AuthAction) => { + return authActions.match(action, { + SET_CONFIG: ({ config }) => { + return { + ...state, + config, + localCluster: config.uuidPrefix, + remoteHosts: { ...config.remoteHosts, [config.uuidPrefix]: new URL(config.rootUrl).host }, + homeCluster: config.loginCluster || config.uuidPrefix, + loginCluster: config.loginCluster, + remoteHostsConfig: { ...state.remoteHostsConfig, [config.uuidPrefix]: config } + }; + }, + REMOTE_CLUSTER_CONFIG: ({ config }) => { + return { + ...state, + remoteHostsConfig: { ...state.remoteHostsConfig, [config.uuidPrefix]: config }, + }; + }, + INIT_USER: ({ user, token }) => { + return { ...state, user, apiToken: token, homeCluster: user.uuid.substr(0, 5) }; }, LOGIN: () => { - authService.login(); return state; }, LOGOUT: () => { - authService.removeApiToken(); - authService.removeUser(); - removeServerApiAuthorizationHeader(); - authService.logout(); - return {...state, apiToken: undefined}; - }, - USER_DETAILS_SUCCESS: (ud: UserDetailsResponse) => { - const user = { - email: ud.email, - firstName: ud.first_name, - lastName: ud.last_name, - uuid: ud.uuid, - ownerUuid: ud.owner_uuid + return { ...state, apiToken: undefined }; + }, + USER_DETAILS_SUCCESS: (user: User) => { + return { ...state, user, homeCluster: user.uuid.substr(0, 5) }; + }, + SET_SSH_KEYS: (sshKeys: SshKeyResource[]) => { + return { ...state, sshKeys }; + }, + ADD_SSH_KEY: (sshKey: SshKeyResource) => { + return { ...state, sshKeys: state.sshKeys.concat(sshKey) }; + }, + REMOVE_SSH_KEY: (uuid: string) => { + return { ...state, sshKeys: state.sshKeys.filter((sshKey) => sshKey.uuid !== uuid) }; + }, + SET_HOME_CLUSTER: (homeCluster: string) => { + return { ...state, homeCluster }; + }, + SET_SESSIONS: (sessions: Session[]) => { + return { ...state, sessions }; + }, + ADD_SESSION: (session: Session) => { + return { ...state, sessions: state.sessions.concat(session) }; + }, + REMOVE_SESSION: (clusterId: string) => { + return { + ...state, + sessions: state.sessions.filter( + session => session.clusterId !== clusterId + ) + }; + }, + UPDATE_SESSION: (session: Session) => { + return { + ...state, + sessions: state.sessions.map( + s => s.clusterId === session.clusterId ? session : s + ) }; - authService.saveUser(user); - return {...state, user}; }, default: () => state }); }; - -export default authReducer;