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