Merge branch 'master' into 14452-my-account
[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, userPrefs } 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 export const USER_IDENTITY_URL = 'identityUrl';
18 export const USER_PREFS = 'prefs';
19
20 export interface UserDetailsResponse {
21     email: string;
22     first_name: string;
23     last_name: string;
24     uuid: string;
25     owner_uuid: string;
26     is_admin: boolean;
27     identity_url: string;
28     prefs: userPrefs;
29 }
30
31 export class AuthService {
32
33     constructor(
34         protected apiClient: AxiosInstance,
35         protected baseUrl: string,
36         protected actions: ApiActions) { }
37
38     public saveApiToken(token: string) {
39         localStorage.setItem(API_TOKEN_KEY, token);
40     }
41
42     public removeApiToken() {
43         localStorage.removeItem(API_TOKEN_KEY);
44     }
45
46     public getApiToken() {
47         return localStorage.getItem(API_TOKEN_KEY) || undefined;
48     }
49
50     public getUuid() {
51         return localStorage.getItem(USER_UUID_KEY) || undefined;
52     }
53
54     public getOwnerUuid() {
55         return localStorage.getItem(USER_OWNER_UUID_KEY) || undefined;
56     }
57
58     public getIsAdmin(): boolean {
59         return localStorage.getItem(USER_IS_ADMIN) === 'true';
60     }
61
62     public getUser(): User | undefined {
63         const email = localStorage.getItem(USER_EMAIL_KEY);
64         const firstName = localStorage.getItem(USER_FIRST_NAME_KEY);
65         const lastName = localStorage.getItem(USER_LAST_NAME_KEY);
66         const uuid = this.getUuid();
67         const ownerUuid = this.getOwnerUuid();
68         const isAdmin = this.getIsAdmin();
69         const identityUrl = localStorage.getItem(USER_IDENTITY_URL);
70         const prefs = JSON.parse(localStorage.getItem(USER_PREFS) || '{"profile": {}}');
71
72         return email && firstName && lastName && uuid && ownerUuid && identityUrl && prefs
73             ? { email, firstName, lastName, uuid, ownerUuid, isAdmin, identityUrl, prefs }
74             : undefined;
75     }
76
77     public saveUser(user: User) {
78         localStorage.setItem(USER_EMAIL_KEY, user.email);
79         localStorage.setItem(USER_FIRST_NAME_KEY, user.firstName);
80         localStorage.setItem(USER_LAST_NAME_KEY, user.lastName);
81         localStorage.setItem(USER_UUID_KEY, user.uuid);
82         localStorage.setItem(USER_OWNER_UUID_KEY, user.ownerUuid);
83         localStorage.setItem(USER_IS_ADMIN, JSON.stringify(user.isAdmin));
84         localStorage.setItem(USER_IDENTITY_URL, user.identityUrl);
85         localStorage.setItem(USER_PREFS, JSON.stringify(user.prefs));
86     }
87
88     public removeUser() {
89         localStorage.removeItem(USER_EMAIL_KEY);
90         localStorage.removeItem(USER_FIRST_NAME_KEY);
91         localStorage.removeItem(USER_LAST_NAME_KEY);
92         localStorage.removeItem(USER_UUID_KEY);
93         localStorage.removeItem(USER_OWNER_UUID_KEY);
94         localStorage.removeItem(USER_IS_ADMIN);
95         localStorage.removeItem(USER_IDENTITY_URL);
96         localStorage.removeItem(USER_PREFS);
97     }
98
99     public login() {
100         const currentUrl = `${window.location.protocol}//${window.location.host}/token`;
101         window.location.assign(`${this.baseUrl || ""}/login?return_to=${currentUrl}`);
102     }
103
104     public logout() {
105         const currentUrl = `${window.location.protocol}//${window.location.host}`;
106         window.location.assign(`${this.baseUrl || ""}/logout?return_to=${currentUrl}`);
107     }
108
109     public getUserDetails = (): Promise<User> => {
110         const reqId = uuid();
111         this.actions.progressFn(reqId, true);
112         return this.apiClient
113             .get<UserDetailsResponse>('/users/current')
114             .then(resp => {
115                 this.actions.progressFn(reqId, false);
116                 const prefs = resp.data.prefs.profile ? resp.data.prefs : { profile: {}};
117                 console.log(resp.data);
118                 return {
119                     email: resp.data.email,
120                     firstName: resp.data.first_name,
121                     lastName: resp.data.last_name,
122                     uuid: resp.data.uuid,
123                     ownerUuid: resp.data.owner_uuid,
124                     isAdmin: resp.data.is_admin,
125                     identityUrl: resp.data.identity_url,
126                     prefs
127                 };
128             })
129             .catch(e => {
130                 this.actions.progressFn(reqId, false);
131                 this.actions.errorFn(reqId, e);
132                 throw e;
133             });
134     }
135
136     public getRootUuid() {
137         const uuid = this.getOwnerUuid();
138         const uuidParts = uuid ? uuid.split('-') : [];
139         return uuidParts.length > 1 ? `${uuidParts[0]}-${uuidParts[1]}` : undefined;
140     }
141 }