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