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