forked from girder/girder_web_components
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestClient.js
More file actions
155 lines (131 loc) · 4 KB
/
restClient.js
File metadata and controls
155 lines (131 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import axios_ from 'axios'
import cookies from 'js-cookie'
import { stringify } from 'qs'
import mitt from 'mitt'
const GirderTokenLength = 64
export const OauthTokenPrefix = '#girderToken='
export const OauthTokenSuffix = '__'
// Girder headers
const GirderToken = 'Girder-Token'
const GirderOtp = 'Girder-OTP'
const GirderAuthorization = 'Girder-Authorization'
function setCookieFromAuth(auth) {
cookies.set('girderToken', auth.token, { expires: new Date(auth.expires) })
}
function setCookieFromHash(location) {
const arr = location.hash.split(OauthTokenPrefix)
const token = arr[arr.length - 1].split(OauthTokenSuffix)[0]
if (token.length === GirderTokenLength) {
const expires = new Date()
expires.setDate(expires.getDate() + 365)
setCookieFromAuth({ token, expires })
location.hash = location.hash.replace(`${OauthTokenPrefix}${token}${OauthTokenSuffix}`, '')
}
return token
}
export default class RestClient {
constructor({
apiRoot = '/api/v1',
token = cookies.get('girderToken') || setCookieFromHash(window.location),
axios = axios_.create(),
authenticateWithCredentials = false,
useGirderAuthorizationHeader = false,
setLocalCookie = true,
} = {}) {
this.apiRoot = apiRoot;
this.token = token;
this.user = null;
this.setLocalCookie = setLocalCookie;
this.authenticateWithCredentials = authenticateWithCredentials;
this.useGirderAuthorizationHeader = useGirderAuthorizationHeader;
this._axios = axios.create();
this.emitter = mitt();
this._axios.interceptors.request.use((config) => ({
...config,
baseURL: this.apiRoot,
headers: {
[GirderToken]: this.token,
...config.headers,
},
}));
['get', 'post', 'put', 'patch', 'delete'].forEach((method) => {
this[method] = (...args) => {
if (this.apiRoot) {
return this._axios[method](...args);
}
};
});
}
on(event, handler) {
this.emitter.on(event, handler);
}
off(event, handler) {
this.emitter.off(event, handler);
}
emit(event, payload) {
this.emitter.emit(event, payload);
}
async setApiRoot(apiRoot) {
await this.logout();
this.apiRoot = apiRoot;
this.emit('apiRootUpdated', this.apiRoot);
}
async login(username, password, otp = null) {
try {
await this.logout();
} catch (_err) {
// noop
}
let auth;
const headers = { [GirderToken]: null };
if (this.useGirderAuthorizationHeader) {
headers[GirderAuthorization] = `Basic ${window.btoa(`${username}:${password}`)}`;
} else {
auth = { username, password };
}
if (otp) {headers[GirderOtp] = otp;}
const resp = await this.get('user/authentication', {
headers,
auth,
withCredentials: this.authenticateWithCredentials,
});
this.token = resp.data.authToken.token;
this.user = resp.data.user;
if (this.setLocalCookie) {setCookieFromAuth(resp.data.authToken);}
this.emit('userLoggedIn', this.user);
return resp;
}
async logout() {
if (!this.token) {return;}
try {
await this.delete('user/authentication');
} catch (err) {
if (!err.response || err.response.status !== 401) {throw err;}
} finally {
this.token = null;
this.user = null;
cookies.remove('girderToken');
this.emit('userLoggedOut');
}
}
async fetchUser() {
const resp = await this.get('user/me');
this.user = resp.data;
if (this.user === null) {this.token = null;}
this.emit('userFetched');
return this.user;
}
async register(login, email, firstName, lastName, password, admin = false) {
const resp = await this.post(
'user',
stringify({ login, email, firstName, lastName, password, admin })
);
if (!resp.data.authToken) {return resp;}
this.token = resp.data.authToken.token;
this.user = resp.data;
if (this.setLocalCookie) {setCookieFromAuth(resp.data.authToken);}
this.emit('userRegistered', this.user);
this.emit('userLoggedIn', this.user);
return resp;
}
}