baf80595f300ad9fb7d181aa8ba5441303e71aab
[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 import { initSessions } from "~/store/auth/auth-action-session";
15
16 export const authActions = unionize({
17     SAVE_API_TOKEN: ofType<string>(),
18     LOGIN: {},
19     LOGOUT: {},
20     CONFIG: ofType<{ config: Config }>(),
21     INIT: ofType<{ user: User, token: string }>(),
22     USER_DETAILS_REQUEST: {},
23     USER_DETAILS_SUCCESS: ofType<User>(),
24     SET_SSH_KEYS: ofType<SshKeyResource[]>(),
25     ADD_SSH_KEY: ofType<SshKeyResource>(),
26     REMOVE_SSH_KEY: ofType<string>(),
27     SET_HOME_CLUSTER: ofType<string>(),
28     SET_SESSIONS: ofType<Session[]>(),
29     ADD_SESSION: ofType<Session>(),
30     REMOVE_SESSION: ofType<string>(),
31     UPDATE_SESSION: ofType<Session>()
32 });
33
34 function setAuthorizationHeader(services: ServiceRepository, token: string) {
35     services.apiClient.defaults.headers.common = {
36         Authorization: `OAuth2 ${token}`
37     };
38     services.webdavClient.defaults.headers = {
39         Authorization: `OAuth2 ${token}`
40     };
41 }
42
43 function removeAuthorizationHeader(client: AxiosInstance) {
44     delete client.defaults.headers.common.Authorization;
45 }
46
47 export const initAuth = (config: Config) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
48     const user = services.authService.getUser();
49     const token = services.authService.getApiToken();
50     if (token) {
51         setAuthorizationHeader(services, token);
52     }
53     dispatch(authActions.CONFIG({ config }));
54     if (token && user) {
55         dispatch(authActions.INIT({ user, token }));
56         dispatch<any>(initSessions(services.authService, config, user));
57         dispatch<any>(getUserDetails()).then((user: User) => {
58             dispatch(authActions.INIT({ user, token }));
59         });
60     }
61 };
62
63 export const saveApiToken = (token: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
64     services.authService.saveApiToken(token);
65     setAuthorizationHeader(services, token);
66     dispatch(authActions.SAVE_API_TOKEN(token));
67 };
68
69 export const login = (uuidPrefix: string, homeCluster: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
70     services.authService.login(uuidPrefix, homeCluster);
71     dispatch(authActions.LOGIN());
72 };
73
74 export const logout = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
75     services.authService.removeApiToken();
76     services.authService.removeUser();
77     removeAuthorizationHeader(services.apiClient);
78     services.authService.logout();
79     dispatch(authActions.LOGOUT());
80 };
81
82 export const getUserDetails = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<User> => {
83     dispatch(authActions.USER_DETAILS_REQUEST());
84     return services.authService.getUserDetails().then(user => {
85         services.authService.saveUser(user);
86         dispatch(authActions.USER_DETAILS_SUCCESS(user));
87         return user;
88     });
89 };
90
91 export type AuthAction = UnionOf<typeof authActions>;