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
53 REMOTE_CLUSTER_CONFIG: ({ config }) => {
56 remoteHostsConfig: { ...state.remoteHostsConfig, [config.uuidPrefix]: config },
59 INIT: ({ user, token }) => {
60 return { ...state, user, apiToken: token, homeCluster: user.uuid.substr(0, 5) };
66 return { ...state, apiToken: undefined };
68 USER_DETAILS_SUCCESS: (user: User) => {
69 return { ...state, user };
71 SET_SSH_KEYS: (sshKeys: SshKeyResource[]) => {
72 return { ...state, sshKeys };
74 ADD_SSH_KEY: (sshKey: SshKeyResource) => {
75 return { ...state, sshKeys: state.sshKeys.concat(sshKey) };
77 REMOVE_SSH_KEY: (uuid: string) => {
78 return { ...state, sshKeys: state.sshKeys.filter((sshKey) => sshKey.uuid !== uuid) };
80 SET_HOME_CLUSTER: (homeCluster: string) => {
81 return { ...state, homeCluster };
83 SET_SESSIONS: (sessions: Session[]) => {
84 return { ...state, sessions };
86 ADD_SESSION: (session: Session) => {
87 return { ...state, sessions: state.sessions.concat(session) };
89 REMOVE_SESSION: (clusterId: string) => {
92 sessions: state.sessions.filter(
93 session => session.clusterId !== clusterId
97 UPDATE_SESSION: (session: Session) => {
100 sessions: state.sessions.map(
101 s => s.clusterId === session.clusterId ? session : s