Merge branch 'master' into 15530-wb2-logincluster
[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, UserResource } 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 } 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 }
23
24 const initialState: AuthState = {
25     user: undefined,
26     apiToken: undefined,
27     sshKeys: [],
28     sessions: [],
29     localCluster: "",
30     homeCluster: "",
31     loginCluster: "",
32     remoteHosts: {},
33     remoteHostsConfig: {}
34 };
35
36 export const authReducer = (services: ServiceRepository) => (state = initialState, action: AuthAction) => {
37     return authActions.match(action, {
38         SAVE_API_TOKEN: (token: string) => {
39             return { ...state, apiToken: token };
40         },
41         SAVE_USER: (user: UserResource) => {
42             return { ...state, user };
43         },
44         CONFIG: ({ config }) => {
45             return {
46                 ...state,
47                 localCluster: config.uuidPrefix,
48                 remoteHosts: { ...config.remoteHosts, [config.uuidPrefix]: new URL(config.rootUrl).host },
49                 homeCluster: config.loginCluster || config.uuidPrefix,
50                 loginCluster: config.loginCluster,
51                 remoteHostsConfig: { ...state.remoteHostsConfig, [config.uuidPrefix]: config }
52             };
53         },
54         REMOTE_CLUSTER_CONFIG: ({ config }) => {
55             return {
56                 ...state,
57                 remoteHostsConfig: { ...state.remoteHostsConfig, [config.uuidPrefix]: config },
58             };
59         },
60         INIT: ({ 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 };
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 };