Merge branch 'master' into 13883-arrow-animation-is-not-working-after-loading
[arvados-workbench2.git] / src / services / auth-service / auth-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { API_HOST } from "../../common/api/server-api";
6 import { User } from "../../models/user";
7 import { AxiosInstance } from "../../../node_modules/axios";
8
9 export const API_TOKEN_KEY = 'apiToken';
10 export const USER_EMAIL_KEY = 'userEmail';
11 export const USER_FIRST_NAME_KEY = 'userFirstName';
12 export const USER_LAST_NAME_KEY = 'userLastName';
13 export const USER_UUID_KEY = 'userUuid';
14 export const USER_OWNER_UUID_KEY = 'userOwnerUuid';
15
16 export interface UserDetailsResponse {
17     email: string;
18     first_name: string;
19     last_name: string;
20     uuid: string;
21     owner_uuid: string;
22     is_admin: boolean;
23 }
24
25 export class AuthService {
26
27     constructor(
28         protected authClient: AxiosInstance,
29         protected apiClient: AxiosInstance) { }
30
31     public saveApiToken(token: string) {
32         localStorage.setItem(API_TOKEN_KEY, token);
33     }
34
35     public removeApiToken() {
36         localStorage.removeItem(API_TOKEN_KEY);
37     }
38
39     public getApiToken() {
40         return localStorage.getItem(API_TOKEN_KEY) || undefined;
41     }
42
43     public getUuid() {
44         return localStorage.getItem(USER_UUID_KEY) || undefined;
45     }
46
47     public getOwnerUuid() {
48         return localStorage.getItem(USER_OWNER_UUID_KEY) || undefined;
49     }
50
51     public getUser(): User | undefined {
52         const email = localStorage.getItem(USER_EMAIL_KEY);
53         const firstName = localStorage.getItem(USER_FIRST_NAME_KEY);
54         const lastName = localStorage.getItem(USER_LAST_NAME_KEY);
55         const uuid = localStorage.getItem(USER_UUID_KEY);
56         const ownerUuid = localStorage.getItem(USER_OWNER_UUID_KEY);
57
58         return email && firstName && lastName && uuid && ownerUuid
59             ? { email, firstName, lastName, uuid, ownerUuid }
60             : undefined;
61     }
62
63     public saveUser(user: User) {
64         localStorage.setItem(USER_EMAIL_KEY, user.email);
65         localStorage.setItem(USER_FIRST_NAME_KEY, user.firstName);
66         localStorage.setItem(USER_LAST_NAME_KEY, user.lastName);
67         localStorage.setItem(USER_UUID_KEY, user.uuid);
68         localStorage.setItem(USER_OWNER_UUID_KEY, user.ownerUuid);
69     }
70
71     public removeUser() {
72         localStorage.removeItem(USER_EMAIL_KEY);
73         localStorage.removeItem(USER_FIRST_NAME_KEY);
74         localStorage.removeItem(USER_LAST_NAME_KEY);
75         localStorage.removeItem(USER_UUID_KEY);
76         localStorage.removeItem(USER_OWNER_UUID_KEY);
77     }
78
79     public login() {
80         const currentUrl = `${window.location.protocol}//${window.location.host}/token`;
81         window.location.assign(`${this.authClient.defaults.baseURL || ""}/login?return_to=${currentUrl}`);
82     }
83
84     public logout() {
85         const currentUrl = `${window.location.protocol}//${window.location.host}`;
86         window.location.assign(`${this.authClient.defaults.baseURL || ""}/logout?return_to=${currentUrl}`);
87     }
88
89     public getUserDetails = (): Promise<User> => {
90         return this.apiClient
91             .get<UserDetailsResponse>('/users/current')
92             .then(resp => ({
93                 email: resp.data.email,
94                 firstName: resp.data.first_name,
95                 lastName: resp.data.last_name,
96                 uuid: resp.data.uuid,
97                 ownerUuid: resp.data.owner_uuid
98             }));
99     }
100
101     public getRootUuid() {
102         const uuid = this.getOwnerUuid();
103         const uuidParts = uuid ? uuid.split('-') : [];
104         return uuidParts.length > 1 ? `${uuidParts[0]}-${uuidParts[1]}` : undefined;
105     }
106 }