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 } 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';
12 export interface AuthState {
15 sshKeys: SshKeyResource[];
20 remoteHosts: { [key: string]: string };
21 remoteHostsConfig: { [key: string]: Config };
25 const initialState: AuthState = {
34 remoteHostsConfig: {},
35 config: mockConfig({})
38 export const authReducer = (services: ServiceRepository) => (state = initialState, action: AuthAction) => {
39 return authActions.match(action, {
40 SET_CONFIG: ({ config }) => {
44 localCluster: config.uuidPrefix,
45 remoteHosts: { ...config.remoteHosts, [config.uuidPrefix]: new URL(config.rootUrl).host },
46 homeCluster: config.loginCluster || config.uuidPrefix,
47 loginCluster: config.loginCluster,
48 remoteHostsConfig: { ...state.remoteHostsConfig, [config.uuidPrefix]: config }
51 REMOTE_CLUSTER_CONFIG: ({ config }) => {
54 remoteHostsConfig: { ...state.remoteHostsConfig, [config.uuidPrefix]: config },
57 INIT_USER: ({ user, token }) => {
58 return { ...state, user, apiToken: token, homeCluster: user.uuid.substr(0, 5) };
64 return { ...state, apiToken: undefined };
66 USER_DETAILS_SUCCESS: (user: User) => {
67 return { ...state, user, homeCluster: user.uuid.substr(0, 5) };
69 SET_SSH_KEYS: (sshKeys: SshKeyResource[]) => {
70 return { ...state, sshKeys };
72 ADD_SSH_KEY: (sshKey: SshKeyResource) => {
73 return { ...state, sshKeys: state.sshKeys.concat(sshKey) };
75 REMOVE_SSH_KEY: (uuid: string) => {
76 return { ...state, sshKeys: state.sshKeys.filter((sshKey) => sshKey.uuid !== uuid) };
78 SET_HOME_CLUSTER: (homeCluster: string) => {
79 return { ...state, homeCluster };
81 SET_SESSIONS: (sessions: Session[]) => {
82 return { ...state, sessions };
84 ADD_SESSION: (session: Session) => {
85 return { ...state, sessions: state.sessions.concat(session) };
87 REMOVE_SESSION: (clusterId: string) => {
90 sessions: state.sessions.filter(
91 session => session.clusterId !== clusterId
95 UPDATE_SESSION: (session: Session) => {
98 sessions: state.sessions.map(
99 s => s.clusterId === session.clusterId ? session : s