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 INIT_USER: ({ user, token, tokenExpiration }) =>
76 apiTokenExpiration: tokenExpiration,
77 homeCluster: user.uuid.substr(0, 5)
80 LOGOUT: () => ({ ...state, apiToken: undefined }),
81 USER_DETAILS_SUCCESS: (user: User) =>
82 ({ ...state, user, homeCluster: user.uuid.substr(0, 5) }),
83 SET_SSH_KEYS: (sshKeys: SshKeyResource[]) => ({ ...state, sshKeys }),
84 ADD_SSH_KEY: (sshKey: SshKeyResource) =>
85 ({ ...state, sshKeys: state.sshKeys.concat(sshKey) }),
86 REMOVE_SSH_KEY: (uuid: string) =>
87 ({ ...state, sshKeys: state.sshKeys.filter((sshKey) => sshKey.uuid !== uuid) }),
88 SET_HOME_CLUSTER: (homeCluster: string) => ({ ...state, homeCluster }),
89 SET_SESSIONS: (sessions: Session[]) => ({ ...state, sessions }),
90 ADD_SESSION: (session: Session) =>
91 ({ ...state, sessions: state.sessions.concat(session) }),
92 REMOVE_SESSION: (clusterId: string) =>
95 sessions: state.sessions.filter(
96 session => session.clusterId !== clusterId
99 UPDATE_SESSION: (session: Session) =>
102 sessions: state.sessions.map(
103 s => s.clusterId === session.clusterId ? session : s