+++ /dev/null
-// Copyright (C) The Arvados Authors. All rights reserved.
-//
-// SPDX-License-Identifier: AGPL-3.0
-
-import { AxiosInstance } from "axios";
-
-export function setServerApiAuthorizationHeader(clients: AxiosInstance[], token: string) {
- clients.forEach(client => {
- client.defaults.headers.common = {
- Authorization: `OAuth2 ${token}`
- };
- });
-}
-
-export function removeServerApiAuthorizationHeader(clients: AxiosInstance[]) {
- clients.forEach(client => {
- delete client.defaults.headers.common.Authorization;
- });
-}
export class AuthService {
constructor(
- protected authClient: AxiosInstance,
- protected apiClient: AxiosInstance) { }
+ protected apiClient: AxiosInstance,
+ protected baseUrl: string) { }
public saveApiToken(token: string) {
localStorage.setItem(API_TOKEN_KEY, token);
public login() {
const currentUrl = `${window.location.protocol}//${window.location.host}/token`;
- window.location.assign(`${this.authClient.defaults.baseURL || ""}/login?return_to=${currentUrl}`);
+ window.location.assign(`${this.baseUrl || ""}/login?return_to=${currentUrl}`);
}
public logout() {
const currentUrl = `${window.location.protocol}//${window.location.host}`;
- window.location.assign(`${this.authClient.defaults.baseURL || ""}/logout?return_to=${currentUrl}`);
+ window.location.assign(`${this.baseUrl || ""}/logout?return_to=${currentUrl}`);
}
public getUserDetails = (): Promise<User> => {
export interface ServiceRepository {
apiClient: AxiosInstance;
- authClient: AxiosInstance;
authService: AuthService;
groupsService: GroupsService;
}
export const createServices = (baseUrl: string): ServiceRepository => {
- const authClient = Axios.create();
const apiClient = Axios.create();
-
- authClient.defaults.baseURL = baseUrl;
apiClient.defaults.baseURL = `${baseUrl}/arvados/v1`;
- const authService = new AuthService(authClient, apiClient);
+ const authService = new AuthService(apiClient, baseUrl);
const groupsService = new GroupsService(apiClient);
const projectService = new ProjectService(apiClient);
const linkService = new LinkService(apiClient);
return {
apiClient,
- authClient,
authService,
groupsService,
projectService,
import { User } from "../../models/user";
import { RootState } from "../store";
import { ServiceRepository } from "../../services/services";
-import { removeServerApiAuthorizationHeader, setServerApiAuthorizationHeader } from "../../common/api/server-api";
+import { AxiosInstance } from "axios";
export const authActions = unionize({
SAVE_API_TOKEN: ofType<string>(),
value: 'payload'
});
+function setAuthorizationHeader(client: AxiosInstance, token: string) {
+ client.defaults.headers.common = {
+ Authorization: `OAuth2 ${token}`
+ };
+}
+
+function removeAuthorizationHeader(client: AxiosInstance) {
+ delete client.defaults.headers.common.Authorization;
+}
+
export const initAuth = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
const user = services.authService.getUser();
const token = services.authService.getApiToken();
if (token) {
- setServerApiAuthorizationHeader([services.authClient, services.apiClient], token);
+ setAuthorizationHeader(services.apiClient, token);
}
if (token && user) {
dispatch(authActions.INIT({ user, token }));
export const saveApiToken = (token: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
services.authService.saveApiToken(token);
- setServerApiAuthorizationHeader([services.authClient, services.apiClient], token);
+ setAuthorizationHeader(services.apiClient, token);
dispatch(authActions.SAVE_API_TOKEN(token));
};
export const logout = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
services.authService.removeApiToken();
services.authService.removeUser();
- removeServerApiAuthorizationHeader([services.authClient, services.apiClient]);
+ removeAuthorizationHeader(services.apiClient);
services.authService.logout();
dispatch(authActions.LOGOUT());
};
import { authActions, AuthAction } from "./auth-action";
import { User } from "../../models/user";
import { ServiceRepository } from "../../services/services";
-import { removeServerApiAuthorizationHeader, setServerApiAuthorizationHeader } from "../../common/api/server-api";
export interface AuthState {
user?: User;