946407fe24172610fbc3aaf9cff7b95052a43af8
[arvados-workbench2.git] / src / store / auth / auth-reducer.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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';
11
12 export interface AuthState {
13     user?: User;
14     apiToken?: string;
15     sshKeys: SshKeyResource[];
16     sessions: Session[];
17     localCluster: string;
18     homeCluster: string;
19     loginCluster: string;
20     remoteHosts: { [key: string]: string };
21     remoteHostsConfig: { [key: string]: Config };
22     config: Config;
23 }
24
25 const initialState: AuthState = {
26     user: undefined,
27     apiToken: undefined,
28     sshKeys: [],
29     sessions: [],
30     localCluster: "",
31     homeCluster: "",
32     loginCluster: "",
33     remoteHosts: {},
34     remoteHostsConfig: {},
35     config: mockConfig({})
36 };
37
38 export const authReducer = (services: ServiceRepository) => (state = initialState, action: AuthAction) => {
39     return authActions.match(action, {
40         SET_CONFIG: ({ config }) => {
41             return {
42                 ...state,
43                 config,
44                 localCluster: config.uuidPrefix,
45                 remoteHosts: { ...config.remoteHosts, [config.uuidPrefix]: new URL(config.rootUrl).host },
46                 homeCluster: config.loginCluster || config.uuidPrefix,
47                 loginCluster: config.loginCluster,
48                 remoteHostsConfig: { ...state.remoteHostsConfig, [config.uuidPrefix]: config }
49             };
50         },
51         REMOTE_CLUSTER_CONFIG: ({ config }) => {
52             return {
53                 ...state,
54                 remoteHostsConfig: { ...state.remoteHostsConfig, [config.uuidPrefix]: config },
55             };
56         },
57         INIT_USER: ({ user, token }) => {
58             return { ...state, user, apiToken: token, homeCluster: user.uuid.substr(0, 5) };
59         },
60         LOGIN: () => {
61             return state;
62         },
63         LOGOUT: () => {
64             return { ...state, apiToken: undefined };
65         },
66         USER_DETAILS_SUCCESS: (user: User) => {
67             return { ...state, user, homeCluster: user.uuid.substr(0, 5) };
68         },
69         SET_SSH_KEYS: (sshKeys: SshKeyResource[]) => {
70             return { ...state, sshKeys };
71         },
72         ADD_SSH_KEY: (sshKey: SshKeyResource) => {
73             return { ...state, sshKeys: state.sshKeys.concat(sshKey) };
74         },
75         REMOVE_SSH_KEY: (uuid: string) => {
76             return { ...state, sshKeys: state.sshKeys.filter((sshKey) => sshKey.uuid !== uuid) };
77         },
78         SET_HOME_CLUSTER: (homeCluster: string) => {
79             return { ...state, homeCluster };
80         },
81         SET_SESSIONS: (sessions: Session[]) => {
82             return { ...state, sessions };
83         },
84         ADD_SESSION: (session: Session) => {
85             return { ...state, sessions: state.sessions.concat(session) };
86         },
87         REMOVE_SESSION: (clusterId: string) => {
88             return {
89                 ...state,
90                 sessions: state.sessions.filter(
91                     session => session.clusterId !== clusterId
92                 )
93             };
94         },
95         UPDATE_SESSION: (session: Session) => {
96             return {
97                 ...state,
98                 sessions: state.sessions.map(
99                     s => s.clusterId === session.clusterId ? session : s
100                 )
101             };
102         },
103         default: () => state
104     });
105 };