Remove auth client
authorDaniel Kos <daniel.kos@contractors.roche.com>
Fri, 3 Aug 2018 05:32:19 +0000 (07:32 +0200)
committerDaniel Kos <daniel.kos@contractors.roche.com>
Fri, 3 Aug 2018 05:32:19 +0000 (07:32 +0200)
Feature #13901

Arvados-DCO-1.1-Signed-off-by: Daniel Kos <daniel.kos@contractors.roche.com>

src/common/api/server-api.ts [deleted file]
src/services/auth-service/auth-service.ts
src/services/services.ts
src/store/auth/auth-action.ts
src/store/auth/auth-reducer.ts

diff --git a/src/common/api/server-api.ts b/src/common/api/server-api.ts
deleted file mode 100644 (file)
index a700f27..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-// 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;
-    });
-}
index 273208cd3c6f3f3b53b94cb9841c8c4841d1ae32..f96edc79a08acd2e1079a4785b6aafd9f31a6edb 100644 (file)
@@ -24,8 +24,8 @@ export interface UserDetailsResponse {
 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);
@@ -77,12 +77,12 @@ export class AuthService {
 
     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> => {
index 0d19cee55344c51cc565f32d260786a7f1342d06..e137176030bd8a029508d1f0703a35d890f0cd58 100644 (file)
@@ -15,7 +15,6 @@ import Axios from "axios";
 
 export interface ServiceRepository {
     apiClient: AxiosInstance;
-    authClient: AxiosInstance;
 
     authService: AuthService;
     groupsService: GroupsService;
@@ -26,13 +25,10 @@ export interface ServiceRepository {
 }
 
 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);
@@ -41,7 +37,6 @@ export const createServices = (baseUrl: string): ServiceRepository => {
 
     return {
         apiClient,
-        authClient,
         authService,
         groupsService,
         projectService,
index 839134cf95ad73d039bac73a3a7f6d49ac8f153e..6b81c31796a41dce91cce146f560978cd5510d56 100644 (file)
@@ -7,7 +7,7 @@ import { Dispatch } from "redux";
 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>(),
@@ -21,11 +21,21 @@ export const authActions = unionize({
     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 }));
@@ -34,7 +44,7 @@ export const initAuth = () => (dispatch: Dispatch, getState: () => RootState, se
 
 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));
 };
 
@@ -46,7 +56,7 @@ export const login = () => (dispatch: Dispatch, getState: () => RootState, servi
 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());
 };
index ac9ecbe588c2c4aeac36c5cf55494112175c042b..1546212b08fe846834bb84d61cf098dedf2c40c3 100644 (file)
@@ -5,7 +5,6 @@
 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;