Merge branch 'origin/master' into 14478-log-in-into-clusters
[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     UPDATE_SESSION: ofType<Session>()
29 });
30
31 function setAuthorizationHeader(services: ServiceRepository, token: string) {
32     services.apiClient.defaults.headers.common = {
33         Authorization: `OAuth2 ${token}`
34     };
35     services.webdavClient.defaults.headers = {
36         Authorization: `OAuth2 ${token}`
37     };
38 }
39
40 function removeAuthorizationHeader(client: AxiosInstance) {
41     delete client.defaults.headers.common.Authorization;
42 }
43
44 export const initAuth = (config: Config) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
45     const user = services.authService.getUser();
46     const token = services.authService.getApiToken();
47     if (token) {
48         setAuthorizationHeader(services, token);
49     }
50     if (token && user) {
51         dispatch(authActions.INIT({ user, token }));
52     }
53     const sessions = services.authService.buildSessions(config, user);
54     services.authService.saveSessions(sessions);
55     dispatch(authActions.SET_SESSIONS(sessions));
56 };
57
58 export const saveApiToken = (token: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
59     services.authService.saveApiToken(token);
60     setAuthorizationHeader(services, token);
61     dispatch(authActions.SAVE_API_TOKEN(token));
62 };
63
64 export const login = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
65     services.authService.login();
66     dispatch(authActions.LOGIN());
67 };
68
69 export const logout = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
70     services.authService.removeApiToken();
71     services.authService.removeUser();
72     removeAuthorizationHeader(services.apiClient);
73     services.authService.logout();
74     dispatch(authActions.LOGOUT());
75 };
76
77 export const getUserDetails = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<User> => {
78     dispatch(authActions.USER_DETAILS_REQUEST());
79     return services.authService.getUserDetails().then(user => {
80         services.authService.saveUser(user);
81         dispatch(authActions.USER_DETAILS_SUCCESS(user));
82         return user;
83     });
84 };
85
86 export type AuthAction = UnionOf<typeof authActions>;