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 apiTokenLocation?: string;
17 extraApiToken?: string;
18 extraApiTokenExpiration?: Date;
19 sshKeys: SshKeyResource[];
24 remoteHosts: { [key: string]: string };
25 remoteHostsConfig: { [key: string]: Config };
29 const initialState: AuthState = {
32 apiTokenExpiration: undefined,
33 apiTokenLocation: undefined,
34 extraApiToken: undefined,
35 extraApiTokenExpiration: undefined,
42 remoteHostsConfig: {},
43 config: mockConfig({})
46 export const authReducer = (services: ServiceRepository) => (state = initialState, action: AuthAction) => {
47 return authActions.match(action, {
48 SET_CONFIG: ({ config }) =>
52 localCluster: config.uuidPrefix,
54 ...config.remoteHosts,
55 [config.uuidPrefix]: new URL(config.rootUrl).host
57 homeCluster: config.loginCluster || config.uuidPrefix,
58 loginCluster: config.loginCluster,
60 ...state.remoteHostsConfig,
61 [config.uuidPrefix]: config
64 REMOTE_CLUSTER_CONFIG: ({ config }) =>
68 ...state.remoteHostsConfig,
69 [config.uuidPrefix]: config
72 SET_EXTRA_TOKEN: ({ extraApiToken, extraApiTokenExpiration }) =>
73 ({ ...state, extraApiToken, extraApiTokenExpiration }),
74 RESET_EXTRA_TOKEN: () =>
75 ({ ...state, extraApiToken: undefined, extraApiTokenExpiration: undefined }),
76 INIT_USER: ({ user, token, tokenExpiration, tokenLocation = state.apiTokenLocation }) =>
80 apiTokenExpiration: tokenExpiration,
81 apiTokenLocation: tokenLocation,
82 homeCluster: user.uuid.substring(0, 5)
85 LOGOUT: () => ({ ...state, apiToken: undefined }),
86 USER_DETAILS_SUCCESS: (user: User) =>
87 ({ ...state, user, homeCluster: user.uuid.substring(0, 5) }),
88 SET_SSH_KEYS: (sshKeys: SshKeyResource[]) => ({ ...state, sshKeys }),
89 ADD_SSH_KEY: (sshKey: SshKeyResource) =>
90 ({ ...state, sshKeys: state.sshKeys.concat(sshKey) }),
91 REMOVE_SSH_KEY: (uuid: string) =>
92 ({ ...state, sshKeys: state.sshKeys.filter((sshKey) => sshKey.uuid !== uuid) }),
93 SET_HOME_CLUSTER: (homeCluster: string) => ({ ...state, homeCluster }),
94 SET_SESSIONS: (sessions: Session[]) => ({ ...state, sessions }),
95 ADD_SESSION: (session: Session) =>
96 ({ ...state, sessions: state.sessions.concat(session) }),
97 REMOVE_SESSION: (clusterId: string) =>
100 sessions: state.sessions.filter(
101 session => session.clusterId !== clusterId
104 UPDATE_SESSION: (session: Session) =>
107 sessions: state.sessions.map(
108 s => s.clusterId === session.clusterId ? session : s