1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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';
12 export interface AuthState {
15 sshKeys: SshKeyResource[];
20 remoteHosts: { [key: string]: string };
21 remoteHostsConfig: { [key: string]: Config };
24 const initialState: AuthState = {
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 };
41 SAVE_USER: (user: UserResource) => {
42 return { ...state, user };
44 CONFIG: ({ config }) => {
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 }
54 REMOTE_CLUSTER_CONFIG: ({ config }) => {
57 remoteHostsConfig: { ...state.remoteHostsConfig, [config.uuidPrefix]: config },
60 INIT: ({ user, token }) => {
61 return { ...state, user, apiToken: token, homeCluster: user.uuid.substr(0, 5) };
67 return { ...state, apiToken: undefined };
69 USER_DETAILS_SUCCESS: (user: User) => {
70 return { ...state, user };
72 SET_SSH_KEYS: (sshKeys: SshKeyResource[]) => {
73 return { ...state, sshKeys };
75 ADD_SSH_KEY: (sshKey: SshKeyResource) => {
76 return { ...state, sshKeys: state.sshKeys.concat(sshKey) };
78 REMOVE_SSH_KEY: (uuid: string) => {
79 return { ...state, sshKeys: state.sshKeys.filter((sshKey) => sshKey.uuid !== uuid) };
81 SET_HOME_CLUSTER: (homeCluster: string) => {
82 return { ...state, homeCluster };
84 SET_SESSIONS: (sessions: Session[]) => {
85 return { ...state, sessions };
87 ADD_SESSION: (session: Session) => {
88 return { ...state, sessions: state.sessions.concat(session) };
90 REMOVE_SESSION: (clusterId: string) => {
93 sessions: state.sessions.filter(
94 session => session.clusterId !== clusterId
98 UPDATE_SESSION: (session: Session) => {
101 sessions: state.sessions.map(
102 s => s.clusterId === session.clusterId ? session : s