1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { ofType, unionize, UnionOf } from '~/common/unionize';
6 import { Dispatch } from "redux";
7 import { reset, stopSubmit } from 'redux-form';
8 import { AxiosInstance } from "axios";
9 import { RootState } from "../store";
10 import { snackbarActions } from '~/store/snackbar/snackbar-actions';
11 import { dialogActions } from '~/store/dialog/dialog-actions';
12 import { setBreadcrumbs } from '~/store/breadcrumbs/breadcrumbs-actions';
13 import { ServiceRepository } from "~/services/services";
14 import { getAuthorizedKeysServiceError, AuthorizedKeysServiceError } from '~/services/authorized-keys-service/authorized-keys-service';
15 import { KeyType, SshKeyResource } from '~/models/ssh-key';
16 import { User } from "~/models/user";
18 export const authActions = unionize({
19 SAVE_API_TOKEN: ofType<string>(),
22 INIT: ofType<{ user: User, token: string }>(),
23 USER_DETAILS_REQUEST: {},
24 USER_DETAILS_SUCCESS: ofType<User>(),
25 SET_SSH_KEYS: ofType<SshKeyResource[]>(),
26 ADD_SSH_KEY: ofType<SshKeyResource>()
29 export const SSH_KEY_CREATE_FORM_NAME = 'sshKeyCreateFormName';
31 export interface SshKeyCreateFormDialogData {
36 function setAuthorizationHeader(services: ServiceRepository, token: string) {
37 services.apiClient.defaults.headers.common = {
38 Authorization: `OAuth2 ${token}`
40 services.webdavClient.defaults.headers = {
41 Authorization: `OAuth2 ${token}`
45 function removeAuthorizationHeader(client: AxiosInstance) {
46 delete client.defaults.headers.common.Authorization;
49 export const initAuth = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
50 const user = services.authService.getUser();
51 const token = services.authService.getApiToken();
53 setAuthorizationHeader(services, token);
56 dispatch(authActions.INIT({ user, token }));
60 export const saveApiToken = (token: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
61 services.authService.saveApiToken(token);
62 setAuthorizationHeader(services, token);
63 dispatch(authActions.SAVE_API_TOKEN(token));
66 export const login = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
67 services.authService.login();
68 dispatch(authActions.LOGIN());
71 export const logout = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
72 services.authService.removeApiToken();
73 services.authService.removeUser();
74 removeAuthorizationHeader(services.apiClient);
75 services.authService.logout();
76 dispatch(authActions.LOGOUT());
79 export const getUserDetails = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<User> => {
80 dispatch(authActions.USER_DETAILS_REQUEST());
81 return services.authService.getUserDetails().then(user => {
82 services.authService.saveUser(user);
83 dispatch(authActions.USER_DETAILS_SUCCESS(user));
88 export const openSshKeyCreateDialog = () => dialogActions.OPEN_DIALOG({ id: SSH_KEY_CREATE_FORM_NAME, data: {} });
90 export const createSshKey = (data: SshKeyCreateFormDialogData) =>
91 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
93 const userUuid = getState().auth.user!.uuid;
94 const { name, publicKey } = data;
95 const newSshKey = await services.authorizedKeysService.create({
99 authorizedUserUuid: userUuid
101 dispatch(dialogActions.CLOSE_DIALOG({ id: SSH_KEY_CREATE_FORM_NAME }));
102 dispatch(reset(SSH_KEY_CREATE_FORM_NAME));
103 dispatch(authActions.ADD_SSH_KEY(newSshKey));
104 dispatch(snackbarActions.OPEN_SNACKBAR({
105 message: "Public key has been successfully created.",
109 const error = getAuthorizedKeysServiceError(e);
110 if (error === AuthorizedKeysServiceError.UNIQUE_PUBLIC_KEY) {
111 dispatch(stopSubmit(SSH_KEY_CREATE_FORM_NAME, { publicKey: 'Public key already exists.' }));
112 } else if (error === AuthorizedKeysServiceError.INVALID_PUBLIC_KEY) {
113 dispatch(stopSubmit(SSH_KEY_CREATE_FORM_NAME, { publicKey: 'Public key is invalid' }));
118 export const loadSshKeysPanel = () =>
119 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
121 dispatch(setBreadcrumbs([{ label: 'SSH Keys'}]));
122 const response = await services.authorizedKeysService.list();
123 dispatch(authActions.SET_SSH_KEYS(response.items));
130 export type AuthAction = UnionOf<typeof authActions>;