1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { authActions, AuthAction } from "./auth-action";
6 import { User } from "~/models/user";
7 import { ServiceRepository } from "~/services/services";
8 import { SshKeyResource } from '~/models/ssh-key';
9 import { Session } from "~/models/session";
10 import { Config, mockConfig } from '~/common/config';
12 export interface AuthState {
15 apiTokenExpiration?: Date;
16 extraApiToken?: string;
17 extraApiTokenExpiration?: Date;
18 sshKeys: SshKeyResource[];
23 remoteHosts: { [key: string]: string };
24 remoteHostsConfig: { [key: string]: Config };
28 const initialState: AuthState = {
31 apiTokenExpiration: undefined,
32 extraApiToken: undefined,
33 extraApiTokenExpiration: undefined,
40 remoteHostsConfig: {},
41 config: mockConfig({})
44 export const authReducer = (services: ServiceRepository) => (state = initialState, action: AuthAction) => {
45 return authActions.match(action, {
46 SET_CONFIG: ({ config }) =>
50 localCluster: config.uuidPrefix,
52 ...config.remoteHosts,
53 [config.uuidPrefix]: new URL(config.rootUrl).host
55 homeCluster: config.loginCluster || config.uuidPrefix,
56 loginCluster: config.loginCluster,
58 ...state.remoteHostsConfig,
59 [config.uuidPrefix]: config
62 REMOTE_CLUSTER_CONFIG: ({ config }) =>
66 ...state.remoteHostsConfig,
67 [config.uuidPrefix]: config
70 SET_EXTRA_TOKEN: ({ extraApiToken, extraApiTokenExpiration }) =>
71 ({ ...state, extraApiToken, extraApiTokenExpiration }),
72 RESET_EXTRA_TOKEN: () =>
73 ({ ...state, extraApiToken: undefined, extraApiTokenExpiration: undefined }),
74 INIT_USER: ({ user, token, tokenExpiration }) =>
78 apiTokenExpiration: tokenExpiration,
79 homeCluster: user.uuid.substr(0, 5)
82 LOGOUT: () => ({ ...state, apiToken: undefined }),
83 USER_DETAILS_SUCCESS: (user: User) =>
84 ({ ...state, user, homeCluster: user.uuid.substr(0, 5) }),
85 SET_SSH_KEYS: (sshKeys: SshKeyResource[]) => ({ ...state, sshKeys }),
86 ADD_SSH_KEY: (sshKey: SshKeyResource) =>
87 ({ ...state, sshKeys: state.sshKeys.concat(sshKey) }),
88 REMOVE_SSH_KEY: (uuid: string) =>
89 ({ ...state, sshKeys: state.sshKeys.filter((sshKey) => sshKey.uuid !== uuid) }),
90 SET_HOME_CLUSTER: (homeCluster: string) => ({ ...state, homeCluster }),
91 SET_SESSIONS: (sessions: Session[]) => ({ ...state, sessions }),
92 ADD_SESSION: (session: Session) =>
93 ({ ...state, sessions: state.sessions.concat(session) }),
94 REMOVE_SESSION: (clusterId: string) =>
97 sessions: state.sessions.filter(
98 session => session.clusterId !== clusterId
101 UPDATE_SESSION: (session: Session) =>
104 sessions: state.sessions.map(
105 s => s.clusterId === session.clusterId ? session : s