Add site manager and initial validation
[arvados-workbench2.git] / src / store / auth / auth-action.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { ofType, unionize, UnionOf } from '~/common/unionize';
6 import { Dispatch } from "redux";
7 import { AxiosInstance } from "axios";
8 import { RootState } from "../store";
9 import { ServiceRepository } from "~/services/services";
10 import { SshKeyResource } from '~/models/ssh-key';
11 import { User } from "~/models/user";
12 import { Session } from "~/models/session";
13 import { Config } from '~/common/config';
14
15 export const authActions = unionize({
16     SAVE_API_TOKEN: ofType<string>(),
17     LOGIN: {},
18     LOGOUT: {},
19     INIT: ofType<{ user: User, token: string }>(),
20     USER_DETAILS_REQUEST: {},
21     USER_DETAILS_SUCCESS: ofType<User>(),
22     SET_SSH_KEYS: ofType<SshKeyResource[]>(),
23     ADD_SSH_KEY: ofType<SshKeyResource>(),
24     REMOVE_SSH_KEY: ofType<string>(),
25     SET_SESSIONS: ofType<Session[]>(),
26     ADD_SESSION: ofType<Session>(),
27     REMOVE_SESSION: ofType<string>()
28 });
29
30 function setAuthorizationHeader(services: ServiceRepository, token: string) {
31     services.apiClient.defaults.headers.common = {
32         Authorization: `OAuth2 ${token}`
33     };
34     services.webdavClient.defaults.headers = {
35         Authorization: `OAuth2 ${token}`
36     };
37 }
38
39 function removeAuthorizationHeader(client: AxiosInstance) {
40     delete client.defaults.headers.common.Authorization;
41 }
42
43 export const initAuth = (config: Config) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
44     const user = services.authService.getUser();
45     const token = services.authService.getApiToken();
46     if (token) {
47         setAuthorizationHeader(services, token);
48     }
49     if (token && user) {
50         dispatch(authActions.INIT({ user, token }));
51     }
52     const sessions = services.authService.buildSessions(config, user);
53     services.authService.saveSessions(sessions);
54     dispatch(authActions.SET_SESSIONS(sessions));
55 };
56
57 export const saveApiToken = (token: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
58     services.authService.saveApiToken(token);
59     setAuthorizationHeader(services, token);
60     dispatch(authActions.SAVE_API_TOKEN(token));
61 };
62
63 export const login = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
64     services.authService.login();
65     dispatch(authActions.LOGIN());
66 };
67
68 export const logout = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
69     services.authService.removeApiToken();
70     services.authService.removeUser();
71     removeAuthorizationHeader(services.apiClient);
72     services.authService.logout();
73     dispatch(authActions.LOGOUT());
74 };
75
76 export const getUserDetails = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<User> => {
77     dispatch(authActions.USER_DETAILS_REQUEST());
78     return services.authService.getUserDetails().then(user => {
79         services.authService.saveUser(user);
80         dispatch(authActions.USER_DETAILS_SUCCESS(user));
81         return user;
82     });
83 };
84
85 export type AuthAction = UnionOf<typeof authActions>;