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 } 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 CONFIG: ({ config }) => {
41 localCluster: config.uuidPrefix,
42 remoteHosts: { ...config.remoteHosts, [config.uuidPrefix]: new URL(config.rootUrl).host },
43 homeCluster: config.loginCluster || config.uuidPrefix,
44 loginCluster: config.loginCluster,
45 remoteHostsConfig: { ...state.remoteHostsConfig, [config.uuidPrefix]: config }
48 REMOTE_CLUSTER_CONFIG: ({ config }) => {
51 remoteHostsConfig: { ...state.remoteHostsConfig, [config.uuidPrefix]: config },
54 INIT: ({ user, token }) => {
55 return { ...state, user, apiToken: token, homeCluster: user.uuid.substr(0, 5) };
61 return { ...state, apiToken: undefined };
63 USER_DETAILS_SUCCESS: (user: User) => {
64 return { ...state, user };
66 SET_SSH_KEYS: (sshKeys: SshKeyResource[]) => {
67 return { ...state, sshKeys };
69 ADD_SSH_KEY: (sshKey: SshKeyResource) => {
70 return { ...state, sshKeys: state.sshKeys.concat(sshKey) };
72 REMOVE_SSH_KEY: (uuid: string) => {
73 return { ...state, sshKeys: state.sshKeys.filter((sshKey) => sshKey.uuid !== uuid) };
75 SET_HOME_CLUSTER: (homeCluster: string) => {
76 return { ...state, homeCluster };
78 SET_SESSIONS: (sessions: Session[]) => {
79 return { ...state, sessions };
81 ADD_SESSION: (session: Session) => {
82 return { ...state, sessions: state.sessions.concat(session) };
84 REMOVE_SESSION: (clusterId: string) => {
87 sessions: state.sessions.filter(
88 session => session.clusterId !== clusterId
92 UPDATE_SESSION: (session: Session) => {
95 sessions: state.sessions.map(
96 s => s.clusterId === session.clusterId ? session : s