@wazo/sdk 0.40.8 → 0.40.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -27,15 +27,13 @@ class Auth {
27
27
  mobile;
28
28
  BACKEND_WAZO;
29
29
  BACKEND_LDAP;
30
- // Useful to avoid the check of the header by doing an extra request to the stack
31
- shouldCheckUserUuidHeader;
30
+ usingEdgeServer;
32
31
  constructor() {
33
32
  this.expiration = DETAULT_EXPIRATION;
34
33
  this.authenticated = false;
35
34
  this.minSubscriptionType = null;
36
35
  this.BACKEND_WAZO = DEFAULT_BACKEND_USER;
37
36
  this.BACKEND_LDAP = BACKEND_LDAP_USER;
38
- this.shouldCheckUserUuidHeader = true;
39
37
  }
40
38
  init(clientId, expiration, minSubscriptionType, authorizationName, mobile) {
41
39
  this.clientId = clientId;
@@ -160,6 +158,7 @@ class Auth {
160
158
  setRefreshToken(null);
161
159
  this.session = null;
162
160
  this.authenticated = false;
161
+ this.usingEdgeServer = undefined;
163
162
  setFetchOptions({});
164
163
  }
165
164
  setOnHostFromHeaders(callback) {
@@ -241,26 +240,44 @@ class Auth {
241
240
  getName() {
242
241
  return `${this.getFirstName()} ${this.getLastName()}`;
243
242
  }
244
- async setHttpUserUuidHeader(uuid) {
245
- if (!uuid) {
246
- return;
247
- }
248
- const headers = {
243
+ _getHttpUserUuidHeaders(uuid) {
244
+ return {
249
245
  ...(getFetchOptions()?.headers || {}),
250
246
  'X-User-UUID': uuid,
251
247
  };
248
+ }
249
+ setHttpUserUuidHeader(uuid) {
250
+ logger.info('Setting http header user uuid', { uuid });
251
+ if (!uuid) {
252
+ logger.warn('attempting to set a null value to user uuid header');
253
+ return;
254
+ }
255
+ const headers = this._getHttpUserUuidHeaders(uuid);
256
+ setFetchOptions({
257
+ ...getFetchOptions(),
258
+ headers,
259
+ });
260
+ }
261
+ async checkHttpUserUuidHeader(uuid) {
262
+ logger.info('Checking user uuid http header', { uuid });
263
+ if (!uuid) {
264
+ return;
265
+ }
266
+ const headers = this._getHttpUserUuidHeaders(uuid);
252
267
  try {
253
- if (this.shouldCheckUserUuidHeader) {
254
- await getApiClient().client.head('auth/0.1/status', null, headers);
268
+ const response = await getApiClient().client.head('auth/0.1/status', null, headers, (r) => r);
269
+ const allowsUserUuidHeader = response.headers.get('access-control-allow-headers')?.includes('X-User-UUID');
270
+ if (this.mobile && !allowsUserUuidHeader) {
271
+ throw new Error('Server does not allow user UUID header on mobile');
255
272
  }
256
273
  // If the previous request went well, it means that the header is accepted
257
- setFetchOptions({
258
- ...getFetchOptions(),
259
- headers,
260
- });
274
+ this.setHttpUserUuidHeader(uuid);
275
+ this.usingEdgeServer = true;
276
+ logger.info('Setting usingEdgeServer value to TRUE', { requestHeaders: headers, responseHeaders: response.headers, allowsUserUuidHeader });
261
277
  }
262
- catch (_) {
263
- // Nothing to do
278
+ catch (e) {
279
+ this.usingEdgeServer = false;
280
+ logger.info('Setting usingEdgeServer to FALSE', { justification: e });
264
281
  }
265
282
  }
266
283
  async _onAuthenticated(rawSession) {
@@ -268,10 +285,15 @@ class Auth {
268
285
  return this.session;
269
286
  }
270
287
  const session = rawSession;
271
- await this.setHttpUserUuidHeader(session.uuid);
272
288
  if (!session) {
273
289
  return null;
274
290
  }
291
+ if (this.usingEdgeServer) {
292
+ this.setHttpUserUuidHeader(session.uuid);
293
+ }
294
+ else if (typeof this.usingEdgeServer === 'undefined') {
295
+ await this.checkHttpUserUuidHeader(session.uuid);
296
+ }
275
297
  setApiToken(session.token);
276
298
  if (session.refreshToken) {
277
299
  setRefreshToken(session.refreshToken);
@@ -1 +1 @@
1
- {"version":3,"file":"Auth.d.ts","sourceRoot":"","sources":["../../../src/simple/Auth.ts"],"names":[],"mappings":"AAsBA,qBAAa,mBAAoB,SAAQ,KAAK;CAAG;AACjD,qBAAa,oBAAqB,SAAQ,KAAK;CAAG;AAClD,qBAAa,eAAgB,SAAQ,KAAK;CAAG;AAC7C,qBAAa,iBAAkB,SAAQ,KAAK;CAAG;;AA+X/C,wBAAuC"}
1
+ {"version":3,"file":"Auth.d.ts","sourceRoot":"","sources":["../../../src/simple/Auth.ts"],"names":[],"mappings":"AAuBA,qBAAa,mBAAoB,SAAQ,KAAK;CAAG;AACjD,qBAAa,oBAAqB,SAAQ,KAAK;CAAG;AAClD,qBAAa,eAAgB,SAAQ,KAAK;CAAG;AAC7C,qBAAa,iBAAkB,SAAQ,KAAK;CAAG;;AA8Z/C,wBAAuC"}
@@ -113,7 +113,7 @@ export default class ApiRequester {
113
113
  }
114
114
  const isHead = method === 'head';
115
115
  const hasEmptyResponse = method === 'delete' || isHead;
116
- const newParse = hasEmptyResponse ? ApiRequester.successResponseParser : parse;
116
+ const newParse = hasEmptyResponse && parse === ApiRequester.defaultParser ? ApiRequester.successResponseParser : parse;
117
117
  const fetchOptions = { ...(this.fetchOptions || {}) };
118
118
  const controller = typeof AbortController !== 'undefined' ? new AbortController() : new NodeAbortController();
119
119
  const extraHeaders = fetchOptions.headers || {};
@@ -62,7 +62,6 @@ class Auth {
62
62
  this.minSubscriptionType = null;
63
63
  this.BACKEND_WAZO = auth_1.DEFAULT_BACKEND_USER;
64
64
  this.BACKEND_LDAP = auth_1.BACKEND_LDAP_USER;
65
- this.shouldCheckUserUuidHeader = true;
66
65
  }
67
66
  init(clientId, expiration, minSubscriptionType, authorizationName, mobile) {
68
67
  this.clientId = clientId;
@@ -197,6 +196,7 @@ class Auth {
197
196
  (0, getApiClient_1.setRefreshToken)(null);
198
197
  this.session = null;
199
198
  this.authenticated = false;
199
+ this.usingEdgeServer = undefined;
200
200
  (0, getApiClient_1.setFetchOptions)({});
201
201
  });
202
202
  }
@@ -280,22 +280,41 @@ class Auth {
280
280
  getName() {
281
281
  return `${this.getFirstName()} ${this.getLastName()}`;
282
282
  }
283
+ _getHttpUserUuidHeaders(uuid) {
284
+ var _a;
285
+ return Object.assign(Object.assign({}, (((_a = (0, getApiClient_1.getFetchOptions)()) === null || _a === void 0 ? void 0 : _a.headers) || {})), { 'X-User-UUID': uuid });
286
+ }
283
287
  setHttpUserUuidHeader(uuid) {
288
+ logger.info('Setting http header user uuid', { uuid });
289
+ if (!uuid) {
290
+ logger.warn('attempting to set a null value to user uuid header');
291
+ return;
292
+ }
293
+ const headers = this._getHttpUserUuidHeaders(uuid);
294
+ (0, getApiClient_1.setFetchOptions)(Object.assign(Object.assign({}, (0, getApiClient_1.getFetchOptions)()), { headers }));
295
+ }
296
+ checkHttpUserUuidHeader(uuid) {
284
297
  var _a;
285
298
  return __awaiter(this, void 0, void 0, function* () {
299
+ logger.info('Checking user uuid http header', { uuid });
286
300
  if (!uuid) {
287
301
  return;
288
302
  }
289
- const headers = Object.assign(Object.assign({}, (((_a = (0, getApiClient_1.getFetchOptions)()) === null || _a === void 0 ? void 0 : _a.headers) || {})), { 'X-User-UUID': uuid });
303
+ const headers = this._getHttpUserUuidHeaders(uuid);
290
304
  try {
291
- if (this.shouldCheckUserUuidHeader) {
292
- yield (0, getApiClient_1.default)().client.head('auth/0.1/status', null, headers);
305
+ const response = yield (0, getApiClient_1.default)().client.head('auth/0.1/status', null, headers, (r) => r);
306
+ const allowsUserUuidHeader = (_a = response.headers.get('access-control-allow-headers')) === null || _a === void 0 ? void 0 : _a.includes('X-User-UUID');
307
+ if (this.mobile && !allowsUserUuidHeader) {
308
+ throw new Error('Server does not allow user UUID header on mobile');
293
309
  }
294
310
  // If the previous request went well, it means that the header is accepted
295
- (0, getApiClient_1.setFetchOptions)(Object.assign(Object.assign({}, (0, getApiClient_1.getFetchOptions)()), { headers }));
311
+ this.setHttpUserUuidHeader(uuid);
312
+ this.usingEdgeServer = true;
313
+ logger.info('Setting usingEdgeServer value to TRUE', { requestHeaders: headers, responseHeaders: response.headers, allowsUserUuidHeader });
296
314
  }
297
- catch (_) {
298
- // Nothing to do
315
+ catch (e) {
316
+ this.usingEdgeServer = false;
317
+ logger.info('Setting usingEdgeServer to FALSE', { justification: e });
299
318
  }
300
319
  });
301
320
  }
@@ -306,10 +325,15 @@ class Auth {
306
325
  return this.session;
307
326
  }
308
327
  const session = rawSession;
309
- yield this.setHttpUserUuidHeader(session.uuid);
310
328
  if (!session) {
311
329
  return null;
312
330
  }
331
+ if (this.usingEdgeServer) {
332
+ this.setHttpUserUuidHeader(session.uuid);
333
+ }
334
+ else if (typeof this.usingEdgeServer === 'undefined') {
335
+ yield this.checkHttpUserUuidHeader(session.uuid);
336
+ }
313
337
  (0, getApiClient_1.setApiToken)(session.token);
314
338
  if (session.refreshToken) {
315
339
  (0, getApiClient_1.setRefreshToken)(session.refreshToken);
@@ -1 +1 @@
1
- {"version":3,"file":"Auth.d.ts","sourceRoot":"","sources":["../../../src/simple/Auth.ts"],"names":[],"mappings":"AAsBA,qBAAa,mBAAoB,SAAQ,KAAK;CAAG;AACjD,qBAAa,oBAAqB,SAAQ,KAAK;CAAG;AAClD,qBAAa,eAAgB,SAAQ,KAAK;CAAG;AAC7C,qBAAa,iBAAkB,SAAQ,KAAK;CAAG;;AA+X/C,wBAAuC"}
1
+ {"version":3,"file":"Auth.d.ts","sourceRoot":"","sources":["../../../src/simple/Auth.ts"],"names":[],"mappings":"AAuBA,qBAAa,mBAAoB,SAAQ,KAAK;CAAG;AACjD,qBAAa,oBAAqB,SAAQ,KAAK;CAAG;AAClD,qBAAa,eAAgB,SAAQ,KAAK;CAAG;AAC7C,qBAAa,iBAAkB,SAAQ,KAAK;CAAG;;AA8Z/C,wBAAuC"}
@@ -114,7 +114,7 @@ class ApiRequester {
114
114
  }
115
115
  const isHead = method === 'head';
116
116
  const hasEmptyResponse = method === 'delete' || isHead;
117
- const newParse = hasEmptyResponse ? ApiRequester.successResponseParser : parse;
117
+ const newParse = hasEmptyResponse && parse === ApiRequester.defaultParser ? ApiRequester.successResponseParser : parse;
118
118
  const fetchOptions = Object.assign({}, (this.fetchOptions || {}));
119
119
  const controller = typeof AbortController !== 'undefined' ? new AbortController() : new node_abort_controller_1.AbortController();
120
120
  const extraHeaders = fetchOptions.headers || {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wazo/sdk",
3
- "version": "0.40.8",
3
+ "version": "0.40.9",
4
4
  "description": "Wazo's JavaScript Software Development Kit.",
5
5
  "main": "index.js",
6
6
  "types": "lib/types/index.d.ts",
@@ -1,4 +1,5 @@
1
1
  /* eslint-disable max-classes-per-file */
2
+ import type { Response } from 'node-fetch';
2
3
  import Session from '../domain/Session';
3
4
  import { BACKEND_LDAP_USER, DEFAULT_BACKEND_USER, DETAULT_EXPIRATION } from '../api/auth';
4
5
  import getApiClient, {
@@ -53,8 +54,7 @@ class Auth {
53
54
 
54
55
  BACKEND_LDAP: string;
55
56
 
56
- // Useful to avoid the check of the header by doing an extra request to the stack
57
- shouldCheckUserUuidHeader: boolean;
57
+ usingEdgeServer: boolean | undefined;
58
58
 
59
59
  constructor() {
60
60
  this.expiration = DETAULT_EXPIRATION;
@@ -62,7 +62,6 @@ class Auth {
62
62
  this.minSubscriptionType = null;
63
63
  this.BACKEND_WAZO = DEFAULT_BACKEND_USER;
64
64
  this.BACKEND_LDAP = BACKEND_LDAP_USER;
65
- this.shouldCheckUserUuidHeader = true;
66
65
  }
67
66
 
68
67
  init(clientId: string, expiration: number, minSubscriptionType: number | null | undefined, authorizationName: string | null, mobile: boolean): void {
@@ -210,6 +209,7 @@ class Auth {
210
209
  setRefreshToken(null);
211
210
  this.session = null;
212
211
  this.authenticated = false;
212
+ this.usingEdgeServer = undefined;
213
213
  setFetchOptions({});
214
214
  }
215
215
 
@@ -318,27 +318,54 @@ class Auth {
318
318
  return `${this.getFirstName()} ${this.getLastName()}`;
319
319
  }
320
320
 
321
- async setHttpUserUuidHeader(uuid: string | null | undefined) {
322
- if (!uuid) {
323
- return;
324
- }
325
- const headers = {
321
+ _getHttpUserUuidHeaders(uuid:string) {
322
+ return {
326
323
  ...(getFetchOptions()?.headers || {}),
327
324
  'X-User-UUID': uuid,
328
325
  };
326
+ }
327
+
328
+ setHttpUserUuidHeader(uuid: string) {
329
+ logger.info('Setting http header user uuid', { uuid });
330
+
331
+ if (!uuid) {
332
+ logger.warn('attempting to set a null value to user uuid header');
333
+ return;
334
+ }
335
+
336
+ const headers = this._getHttpUserUuidHeaders(uuid);
337
+
338
+ setFetchOptions({
339
+ ...getFetchOptions(),
340
+ headers,
341
+ });
342
+ }
343
+
344
+ async checkHttpUserUuidHeader(uuid: string | null | undefined) {
345
+ logger.info('Checking user uuid http header', { uuid });
346
+
347
+ if (!uuid) {
348
+ return;
349
+ }
350
+
351
+ const headers = this._getHttpUserUuidHeaders(uuid);
329
352
 
330
353
  try {
331
- if (this.shouldCheckUserUuidHeader) {
332
- await getApiClient().client.head('auth/0.1/status', null, headers);
354
+ const response: Response = await getApiClient().client.head('auth/0.1/status', null, headers, (r: any) => r);
355
+
356
+ const allowsUserUuidHeader = response.headers.get('access-control-allow-headers')?.includes('X-User-UUID');
357
+
358
+ if (this.mobile && !allowsUserUuidHeader) {
359
+ throw new Error('Server does not allow user UUID header on mobile');
333
360
  }
334
361
 
335
362
  // If the previous request went well, it means that the header is accepted
336
- setFetchOptions({
337
- ...getFetchOptions(),
338
- headers,
339
- });
340
- } catch (_) {
341
- // Nothing to do
363
+ this.setHttpUserUuidHeader(uuid);
364
+ this.usingEdgeServer = true;
365
+ logger.info('Setting usingEdgeServer value to TRUE', { requestHeaders: headers, responseHeaders: response.headers, allowsUserUuidHeader });
366
+ } catch (e) {
367
+ this.usingEdgeServer = false;
368
+ logger.info('Setting usingEdgeServer to FALSE', { justification: e });
342
369
  }
343
370
  }
344
371
 
@@ -348,12 +375,17 @@ class Auth {
348
375
  }
349
376
 
350
377
  const session = rawSession;
351
- await this.setHttpUserUuidHeader(session.uuid);
352
378
 
353
379
  if (!session) {
354
380
  return null;
355
381
  }
356
382
 
383
+ if (this.usingEdgeServer) {
384
+ this.setHttpUserUuidHeader(session.uuid as string);
385
+ } else if (typeof this.usingEdgeServer === 'undefined') {
386
+ await this.checkHttpUserUuidHeader(session.uuid);
387
+ }
388
+
357
389
  setApiToken(session.token);
358
390
 
359
391
  if (session.refreshToken) {
@@ -167,7 +167,7 @@ export default class ApiRequester {
167
167
 
168
168
  const isHead = method === 'head';
169
169
  const hasEmptyResponse = method === 'delete' || isHead;
170
- const newParse = hasEmptyResponse ? ApiRequester.successResponseParser : parse;
170
+ const newParse = hasEmptyResponse && parse === ApiRequester.defaultParser ? ApiRequester.successResponseParser : parse;
171
171
  const fetchOptions = { ...(this.fetchOptions || {}) };
172
172
  const controller = typeof AbortController !== 'undefined' ? new AbortController() : new NodeAbortController();
173
173
  const extraHeaders = fetchOptions.headers || {};