16848: Resets extra cached token from the store when not valid anymore.
[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         RESET_EXTRA_TOKEN: () =>
73             ({ ...state, extraApiToken: undefined, extraApiTokenExpiration: undefined }),
74         INIT_USER: ({ user, token, tokenExpiration }) =>
75             ({ ...state,
76                 user,
77                 apiToken: token,
78                 apiTokenExpiration: tokenExpiration,
79                 homeCluster: user.uuid.substr(0, 5)
80             }),
81         LOGIN: () => state,
82         LOGOUT: () => ({ ...state, apiToken: undefined }),
83         USER_DETAILS_SUCCESS: (user: User) =>
84             ({ ...state, user, homeCluster: user.uuid.substr(0, 5) }),
85         SET_SSH_KEYS: (sshKeys: SshKeyResource[]) => ({ ...state, sshKeys }),
86         ADD_SSH_KEY: (sshKey: SshKeyResource) =>
87             ({ ...state, sshKeys: state.sshKeys.concat(sshKey) }),
88         REMOVE_SSH_KEY: (uuid: string) =>
89             ({ ...state, sshKeys: state.sshKeys.filter((sshKey) => sshKey.uuid !== uuid) }),
90         SET_HOME_CLUSTER: (homeCluster: string) => ({ ...state, homeCluster }),
91         SET_SESSIONS: (sessions: Session[]) => ({ ...state, sessions }),
92         ADD_SESSION: (session: Session) =>
93             ({ ...state, sessions: state.sessions.concat(session) }),
94         REMOVE_SESSION: (clusterId: string) =>
95             ({
96                 ...state,
97                 sessions: state.sessions.filter(
98                     session => session.clusterId !== clusterId
99                 )
100             }),
101         UPDATE_SESSION: (session: Session) =>
102             ({
103                 ...state,
104                 sessions: state.sessions.map(
105                     s => s.clusterId === session.clusterId ? session : s
106                 )
107             }),
108         default: () => state
109     });
110 };