16848: Adds expiration date to the "Get API Token" dialog.
[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     apiTokenExpiration?: Date;
16     extraApiToken?: string;
17     extraApiTokenExpiration?: Date;
18     sshKeys: SshKeyResource[];
19     sessions: Session[];
20     localCluster: string;
21     homeCluster: string;
22     loginCluster: string;
23     remoteHosts: { [key: string]: string };
24     remoteHostsConfig: { [key: string]: Config };
25     config: Config;
26 }
27
28 const initialState: AuthState = {
29     user: undefined,
30     apiToken: undefined,
31     apiTokenExpiration: undefined,
32     extraApiToken: undefined,
33     extraApiTokenExpiration: undefined,
34     sshKeys: [],
35     sessions: [],
36     localCluster: "",
37     homeCluster: "",
38     loginCluster: "",
39     remoteHosts: {},
40     remoteHostsConfig: {},
41     config: mockConfig({})
42 };
43
44 export const authReducer = (services: ServiceRepository) => (state = initialState, action: AuthAction) => {
45     return authActions.match(action, {
46         SET_CONFIG: ({ config }) =>
47             ({
48                 ...state,
49                 config,
50                 localCluster: config.uuidPrefix,
51                 remoteHosts: {
52                     ...config.remoteHosts,
53                     [config.uuidPrefix]: new URL(config.rootUrl).host
54                 },
55                 homeCluster: config.loginCluster || config.uuidPrefix,
56                 loginCluster: config.loginCluster,
57                 remoteHostsConfig: {
58                     ...state.remoteHostsConfig,
59                     [config.uuidPrefix]: config
60                 }
61             }),
62         REMOTE_CLUSTER_CONFIG: ({ config }) =>
63             ({
64                 ...state,
65                 remoteHostsConfig: {
66                     ...state.remoteHostsConfig,
67                     [config.uuidPrefix]: config
68                 },
69             }),
70         SET_EXTRA_TOKEN: ({ extraApiToken, extraApiTokenExpiration }) =>
71             ({ ...state, extraApiToken, extraApiTokenExpiration }),
72         INIT_USER: ({ user, token, tokenExpiration }) =>
73             ({ ...state,
74                 user,
75                 apiToken: token,
76                 apiTokenExpiration: tokenExpiration,
77                 homeCluster: user.uuid.substr(0, 5)
78             }),
79         LOGIN: () => state,
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) =>
93             ({
94                 ...state,
95                 sessions: state.sessions.filter(
96                     session => session.clusterId !== clusterId
97                 )
98             }),
99         UPDATE_SESSION: (session: Session) =>
100             ({
101                 ...state,
102                 sessions: state.sessions.map(
103                     s => s.clusterId === session.clusterId ? session : s
104                 )
105             }),
106         default: () => state
107     });
108 };