-
Notifications
You must be signed in to change notification settings - Fork 3
/
registry.js
375 lines (310 loc) · 11.2 KB
/
registry.js
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
const Organization = require('./organization');
const {DEFAULT_REGISTRY} = require('./constants');
let refreshTimers = {};
const throwError = (msg, status, extraData = {}) => {
const e = new Error(msg);
e.status = status;
Object.assign(e, extraData);
throw e;
};
// Credit https://stackoverflow.com/a/38552302
function parseJwt(token) {
if (!token) return {};
var base64Url = token.split('.')[1];
var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
var jsonPayload = decodeURIComponent(atob(base64).split('').map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
return JSON.parse(jsonPayload);
}
module.exports = class Registry {
constructor(url = "https://" + DEFAULT_REGISTRY) {
this.url = url;
this.eventListeners = {};
}
get remote() {
return this.url.replace(/^https?:\/\//, "");
}
get tagUrl() {
// Drop the https prefix if it's secure (it's the default)
return this.secure ? this.remote : this.url;
}
get secure() {
return this.url.startsWith("https://");
}
// Login
async login(username, password, xAuthToken = null) {
const formData = new FormData();
if (username) formData.append("username", username);
if (password) formData.append("password", password);
if (xAuthToken) formData.append("token", xAuthToken);
try {
const res = await fetch(`${this.url}/users/authenticate`, {
method: 'POST',
body: formData
}).then(r => r.json());
if (res.token) {
this.setCredentials(res.username, res.token, res.expires);
this.setAutoRefreshToken();
this.emit("login", res.username);
return res;
} else {
throw new Error(res.error || `Cannot login: ${JSON.stringify(res)}`);
}
} catch (e) {
throw new Error(`Cannot login: ${e.message}`);
}
}
async storageInfo() {
if (this.isLoggedIn()) {
const res = await this.getRequest(`/users/storage`);
if (res.total == null) {
return {
total: null,
used: res.used
};
} else {
return {
total: res.total,
used: res.used,
free: res.total - res.used,
usedPercentage: res.used / res.total
};
}
} else {
throw new Error("not logged in");
}
}
async users() {
return await this.getRequest(`/users`);
}
async userRoles() {
return await this.getRequest(`/users/roles`);
}
async addUser(username, password, roles = []) {
return await this.postRequest('/users', {
username, password, roles: JSON.stringify(roles)
});
}
async deleteUser(username) {
return await this.deleteRequest(`/users/${encodeURIComponent(username)}`);
}
async changePwd(oldPassword, newPassword) {
const res = await this.postRequest('/users/changePwd', {
oldPassword, newPassword
});
if (res.token) {
this.setCredentials(this.getUsername(), res.token, res.expires);
} else {
throwError(res.error || `Cannot change password: ${JSON.stringify(res)}`, res.status);
}
}
async refreshToken() {
if (this.isLoggedIn()) {
const f = await fetch(`${this.url}/users/authenticate/refresh`, {
method: 'POST',
headers: {
Authorization: `Bearer ${this.getAuthToken()}`
}
});
const res = await f.json();
if (res.token) {
this.setCredentials(this.getUsername(), res.token, res.expires);
} else {
throwError(res.error || `Cannot refresh token: ${JSON.stringify(res)}`, f.status);
}
} else {
throw new Error("logged out");
}
}
setAutoRefreshToken(seconds = 3600) {
if (refreshTimers[this.url]) {
clearTimeout(refreshTimers[this.url]);
delete refreshTimers[this.url];
}
setTimeout(async () => {
try {
await this.refreshToken();
this.setAutoRefreshToken(seconds);
} catch (e) {
console.error(e);
// Try again later, unless we're logged out
if (e.message !== "logged out") {
this.setAutoRefreshToken(seconds);
}
}
}, seconds * 1000);
}
logout() {
this.clearCredentials();
this.emit("logout");
}
setCredentials(username, token, expires) {
localStorage.setItem(`${this.url}_username`, username);
localStorage.setItem(`${this.url}_jwt_token`, token);
localStorage.setItem(`${this.url}_jwt_token_expires`, expires);
// Set cookie if the URL matches the current window
if (typeof window !== "undefined") {
if (window.location.origin === this.url) {
document.cookie = `jwtToken=${token};${expires * 1000};path=/`;
}
}
}
getAuthToken() {
if (this.getAuthTokenExpiration() > new Date()) {
return localStorage.getItem(`${this.url}_jwt_token`);
}
}
getUsername() {
if (this.isLoggedIn()) {
return localStorage.getItem(`${this.url}_username`);
}
}
isAdmin() {
if (this.isLoggedIn()) {
const token = this.getAuthToken();
if (!token)
return false;
const decoded = parseJwt(token);
if (!decoded)
return false;
return decoded.admin;
}
}
getAuthTokenExpiration() {
const expires = localStorage.getItem(`${this.url}_jwt_token_expires`);
if (expires) {
return new Date(expires * 1000);
}
}
async createOrganization(slug, name, description, isPublic) {
if (!this.isLoggedIn())
throw new Error("not logged in");
return await this.postRequest(`/orgs`, {
slug,
name,
description,
isPublic
});
}
async updateOrganization(slug, name, description, isPublic) {
if (!this.isLoggedIn())
throw new Error("not logged in");
return await this.putRequest(`/orgs/${slug}`, {
slug: slug,
name,
description,
isPublic
});
}
async getOrganizations() {
const res = await this.getRequest(`/orgs`);
return res.map(org => new Organization(this, org));
}
async deleteOrganization(orgSlug) {
if (!this.isLoggedIn())
throw new Error("not logged in");
return await this.deleteRequest(`/orgs/${orgSlug}`);
}
clearCredentials() {
localStorage.removeItem(`${this.url}_jwt_token`);
localStorage.removeItem(`${this.url}_jwt_token_expires`);
localStorage.removeItem(`${this.url}_username`);
// Clear cookie if the needed
if (typeof window !== "undefined") {
if (window.location.origin === this.url) {
document.cookie = `jwtToken=;-1;path=/`;
}
}
}
isLoggedIn() {
const loggedIn = this.getAuthToken() !== null && this.getAuthTokenExpiration() > new Date();
if (!loggedIn) this.clearCredentials();
return loggedIn;
}
async makeRequest(endpoint, method = "GET", body = null) {
const headers = {};
const authToken = this.getAuthToken();
if (authToken) headers.Authorization = `Bearer ${authToken}`;
const options = {
method,
headers
};
if (body) {
const formData = new FormData();
for (let k in body) {
if (Array.isArray(body[k]))
body[k].forEach(v => formData.append(k, v));
else
formData.append(k, body[k]);
}
options.body = formData;
}
const response = await fetch(`${this.url}${endpoint}`, options);
if (response.status === 204) return true;
else if (response.status === 401) {
const contentType = response.headers.get("Content-Type");
if (contentType && contentType.indexOf("application/json") !== -1) {
let json = await response.json();
throwError(json.error || "Unauthorized", 401, json);
} else {
throwError("Unauthorized", 401);
}
}
else if (response.status === 404) throwError("Not found", 404);
else if (method === "HEAD" && response.status === 200) return true;
else {
const contentType = response.headers.get("Content-Type");
if (contentType && contentType.indexOf("application/json") !== -1) {
let json = await response.json();
if (json.error) throwError(json.error, response.status);
if (response.status === 200 || response.status === 201) return json;
else throwError(`Server responded with: ${JSON.stringify(json)}`, response.status);
} else if (contentType && contentType.indexOf("text/") !== -1) {
let text = await response.text();
if (response.status === 200 || response.status === 201) return text;
else throwError(`Server responded with: ${text}`, response.status);
} else {
throwError(`Server responded with: ${await response.text()}`, response.status);
}
}
}
async getRequest(endpoint) {
return this.makeRequest(endpoint, "GET");
}
async postRequest(endpoint, body = {}) {
return this.makeRequest(endpoint, "POST", body);
}
async putRequest(endpoint, body = {}) {
return this.makeRequest(endpoint, "PUT", body);
}
async deleteRequest(endpoint, body = {}) {
return this.makeRequest(endpoint, "DELETE", body);
}
async headRequest(endpoint) {
return this.makeRequest(endpoint, "HEAD");
}
Organization(name) {
return new Organization(this, name);
}
addEventListener(event, cb) {
this.eventListeners[event] = this.eventListeners[event] || [];
if (!this.eventListeners[event].find(e => e === cb)) {
this.eventListeners[event].push(cb);
}
}
removeEventListener(event, cb) {
this.eventListeners[event] = this.eventListeners[event] || [];
this.eventListeners[event] = this.eventListeners[event].filter(e => e !== cb);
}
emit(event, ...params) {
if (this.eventListeners[event]) {
this.eventListeners[event].forEach(listener => {
listener(...params);
});
}
}
}