@cloudbase/oauth 0.1.1-alpha → 1.0.0-alpha.3

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.
Files changed (97) hide show
  1. package/.eslintignore +2 -0
  2. package/.eslintrc +26 -0
  3. package/Dockerfile +15 -0
  4. package/README.md +130 -100
  5. package/_exmaple/assets/scripts/function/function.ts +99 -0
  6. package/_exmaple/assets/scripts/index.ts +101 -0
  7. package/_exmaple/assets/scripts/request.ts +11 -0
  8. package/_exmaple/index.html +15 -0
  9. package/_exmaple/package.json +33 -0
  10. package/_exmaple/tsconfig.json +71 -0
  11. package/_exmaple/typings.d.ts +0 -0
  12. package/_exmaple/webpack.config.js +42 -0
  13. package/dist/auth/apis.d.ts +55 -0
  14. package/dist/auth/apis.js +570 -0
  15. package/{auth → dist/auth}/consts.d.ts +7 -2
  16. package/dist/auth/consts.js +61 -0
  17. package/dist/auth/models.d.ts +355 -0
  18. package/dist/auth/models.js +3 -0
  19. package/dist/captcha/captcha.d.ts +35 -0
  20. package/dist/captcha/captcha.js +267 -0
  21. package/dist/index.d.ts +14 -0
  22. package/dist/index.js +63 -0
  23. package/{oauthclient → dist/oauth2client}/consts.d.ts +22 -1
  24. package/dist/oauth2client/consts.js +73 -0
  25. package/dist/oauth2client/interface.d.ts +15 -0
  26. package/dist/oauth2client/interface.js +10 -0
  27. package/{oauthclient → dist/oauth2client}/models.d.ts +13 -5
  28. package/dist/oauth2client/models.js +3 -0
  29. package/dist/oauth2client/oauth2client.d.ts +71 -0
  30. package/dist/oauth2client/oauth2client.js +634 -0
  31. package/dist/utils/function/single-promise.d.ts +5 -0
  32. package/dist/utils/function/single-promise.js +89 -0
  33. package/dist/utils/uuid.d.ts +1 -0
  34. package/dist/utils/uuid.js +12 -0
  35. package/package.json +28 -29
  36. package/publish.sh +2 -0
  37. package/src/auth/apis.ts +644 -0
  38. package/src/auth/consts.ts +57 -0
  39. package/src/auth/models.ts +445 -0
  40. package/src/captcha/captcha.ts +222 -0
  41. package/src/index.ts +49 -0
  42. package/src/oauth2client/consts.ts +69 -0
  43. package/src/oauth2client/interface.ts +57 -0
  44. package/src/oauth2client/models.ts +61 -0
  45. package/src/oauth2client/oauth2client.ts +615 -0
  46. package/src/utils/function/single-promise.ts +40 -0
  47. package/src/utils/uuid.ts +11 -0
  48. package/tsconfig.json +44 -0
  49. package/wiki/README.md +75 -0
  50. package/app/index.d.ts +0 -28
  51. package/app/index.js +0 -54
  52. package/app/index.js.map +0 -1
  53. package/app/internal.d.ts +0 -12
  54. package/app/internal.js +0 -25
  55. package/app/internal.js.map +0 -1
  56. package/app/openuri.d.ts +0 -20
  57. package/app/openuri.js +0 -104
  58. package/app/openuri.js.map +0 -1
  59. package/app/request.d.ts +0 -18
  60. package/app/request.js +0 -44
  61. package/app/request.js.map +0 -1
  62. package/app/storage.d.ts +0 -41
  63. package/app/storage.js +0 -35
  64. package/app/storage.js.map +0 -1
  65. package/auth/consts.js +0 -57
  66. package/auth/consts.js.map +0 -1
  67. package/auth/index.d.ts +0 -193
  68. package/auth/index.js +0 -403
  69. package/auth/index.js.map +0 -1
  70. package/auth/models.d.ts +0 -158
  71. package/auth/models.js +0 -3
  72. package/auth/models.js.map +0 -1
  73. package/captcha/index.d.ts +0 -45
  74. package/captcha/index.js +0 -133
  75. package/captcha/index.js.map +0 -1
  76. package/index.d.ts +0 -8
  77. package/index.js +0 -17
  78. package/index.js.map +0 -1
  79. package/oauthclient/consts.js +0 -53
  80. package/oauthclient/consts.js.map +0 -1
  81. package/oauthclient/index.d.ts +0 -18
  82. package/oauthclient/index.js +0 -21
  83. package/oauthclient/index.js.map +0 -1
  84. package/oauthclient/interface.d.ts +0 -25
  85. package/oauthclient/interface.js +0 -10
  86. package/oauthclient/interface.js.map +0 -1
  87. package/oauthclient/models.js +0 -3
  88. package/oauthclient/models.js.map +0 -1
  89. package/oauthclient/oauthclient.d.ts +0 -142
  90. package/oauthclient/oauthclient.js +0 -399
  91. package/oauthclient/oauthclient.js.map +0 -1
  92. package/utils/single-promise.d.ts +0 -18
  93. package/utils/single-promise.js +0 -48
  94. package/utils/single-promise.js.map +0 -1
  95. package/utils/uuid.d.ts +0 -5
  96. package/utils/uuid.js +0 -16
  97. package/utils/uuid.js.map +0 -1
@@ -0,0 +1,615 @@
1
+ import { ErrorType } from './consts';
2
+
3
+ import { AuthClient, SimpleStorage } from './interface';
4
+
5
+ import {
6
+ Credentials,
7
+ ResponseError,
8
+ RequestOptions,
9
+ RequestFunction,
10
+ OAuth2ClientOptions,
11
+ AuthClientRequestOptions,
12
+ } from './models';
13
+
14
+ import { uuidv4 } from '../utils/uuid';
15
+
16
+ import { SinglePromise } from '../utils/function/single-promise';
17
+
18
+ const RequestIdHeaderName = 'x-request-id';
19
+ const DeviceIdHeaderName = 'x-device-id';
20
+ const DeviceIdSectionName = 'device_id';
21
+
22
+ export interface ToResponseErrorOptions {
23
+ error?: ErrorType;
24
+ error_description?: string | null;
25
+ error_uri?: string | null;
26
+ details?: any | null;
27
+ }
28
+
29
+ export const defaultRequest: RequestFunction = async <T>(
30
+ url: string,
31
+ options?: RequestOptions,
32
+ ): Promise<T> => {
33
+ let result: T | null = null;
34
+ let responseError: ResponseError | null = null;
35
+ try {
36
+ // Objects must be copied to prevent modification of data such as body.
37
+ const copyOptions = Object.assign({}, options);
38
+ if (!copyOptions.method) {
39
+ copyOptions.method = 'GET';
40
+ }
41
+ if (copyOptions.body && typeof copyOptions.body !== 'string') {
42
+ copyOptions.body = JSON.stringify(copyOptions.body);
43
+ }
44
+ const responseResult: Response = await fetch(url, copyOptions);
45
+ const jsonResponse = await responseResult.json();
46
+ if (jsonResponse?.error) {
47
+ responseError = jsonResponse as ResponseError;
48
+ responseError.error_uri = new URL(url).pathname;
49
+ } else {
50
+ result = jsonResponse as T;
51
+ }
52
+ } catch (error) {
53
+ responseError = {
54
+ error: ErrorType.UNREACHABLE,
55
+ error_description: error.message,
56
+ error_uri: new URL(url).pathname,
57
+ };
58
+ }
59
+ if (responseError) {
60
+ throw responseError;
61
+ } else {
62
+ return result;
63
+ }
64
+ };
65
+
66
+ export const toResponseError = (
67
+ error: ResponseError | Error,
68
+ options?: ToResponseErrorOptions,
69
+ ): ResponseError => {
70
+ let responseError: ResponseError;
71
+ const formatOptions: ToResponseErrorOptions = options || {};
72
+ if (error instanceof Error) {
73
+ responseError = {
74
+ error: formatOptions.error || ErrorType.LOCAL,
75
+ error_description: formatOptions.error_description || error.message,
76
+ error_uri: formatOptions.error_uri,
77
+ details: formatOptions.details || error.stack,
78
+ };
79
+ } else {
80
+ const formatError: ToResponseErrorOptions = error || {};
81
+ responseError = {
82
+ error: formatOptions.error || formatError.error || ErrorType.LOCAL,
83
+ error_description:
84
+ formatOptions.error_description || formatError.error_description,
85
+ error_uri: formatOptions.error_uri || formatError.error_uri,
86
+ details: formatOptions.details || formatError.details,
87
+ };
88
+ }
89
+ return responseError;
90
+ };
91
+
92
+ /**
93
+ * Generate request id.
94
+ * @return {string}
95
+ */
96
+ export function generateRequestId(): string {
97
+ return uuidv4();
98
+ }
99
+
100
+
101
+ /**
102
+ * Default Storage.
103
+ */
104
+ class DefaultStorage implements SimpleStorage {
105
+ /**
106
+ * Get item.
107
+ * @param {string} key
108
+ */
109
+ async getItem(key: string): Promise<string | null> {
110
+ return window.localStorage.getItem(key);
111
+ }
112
+
113
+ /**
114
+ * Remove item.
115
+ * @param {string} key
116
+ */
117
+ async removeItem(key: string): Promise<void> {
118
+ window.localStorage.removeItem(key);
119
+ }
120
+
121
+ /**
122
+ * Set item.
123
+ * @param {string} key
124
+ * @param {string} value
125
+ */
126
+ async setItem(key: string, value: string): Promise<void> {
127
+ window.localStorage.setItem(key, value);
128
+ }
129
+
130
+ /**
131
+ * Get item sync.
132
+ * @param {string} key
133
+ */
134
+ getItemSync(key: string): string | null {
135
+ return window.localStorage.getItem(key);
136
+ }
137
+
138
+ /**
139
+ * Remove item sync.
140
+ * @param {string} key
141
+ */
142
+ removeItemSync(key: string): void {
143
+ window.localStorage.removeItem(key);
144
+ }
145
+
146
+ /**
147
+ * Set item sync.
148
+ * @param {string} key
149
+ * @param {string} value
150
+ */
151
+ setItemSync(key: string, value: string): void {
152
+ window.localStorage.setItem(key, value);
153
+ }
154
+ }
155
+
156
+ export const defaultStorage = new DefaultStorage();
157
+
158
+ interface LocalCredentialsOptions {
159
+ tokenSectionName: string;
160
+ storage: SimpleStorage;
161
+ }
162
+
163
+ /**
164
+ * Check if credentials is expired.
165
+ * @param {Credentials} credentials
166
+ * @return {boolean}
167
+ */
168
+ function isCredentialsExpired(credentials: Credentials): boolean {
169
+ let isExpired = true;
170
+ if (credentials?.expires_at && credentials?.access_token) {
171
+ isExpired = credentials.expires_at < new Date();
172
+ }
173
+ return isExpired;
174
+ }
175
+
176
+ /**
177
+ * Local credentials.
178
+ * Local credentials, with memory cache and storage cache.
179
+ * If the memory cache expires, the storage cache is automatically loaded.
180
+ */
181
+ export class LocalCredentials {
182
+ private _tokenSectionName: string;
183
+
184
+ private _storage: SimpleStorage;
185
+
186
+ private _credentials: Credentials | null = null;
187
+
188
+ private _singlePromise: SinglePromise = new SinglePromise();
189
+
190
+ /**
191
+ * constructor
192
+ * @param {LocalCredentialsOptions} options
193
+ */
194
+ constructor(options: LocalCredentialsOptions) {
195
+ this._tokenSectionName = options.tokenSectionName;
196
+ this._storage = options.storage;
197
+ }
198
+
199
+ /**
200
+ * setCredentials Provides an alternative fetch api request implementation with auth credentials
201
+ * @param {Credentials} credentials
202
+ */
203
+ public async setCredentials(credentials?: Credentials): Promise<void> {
204
+ if (credentials?.expires_in) {
205
+ credentials.expires_at = new Date(
206
+ Date.now() + (credentials.expires_in - 30) * 1000,
207
+ );
208
+ if (this._storage) {
209
+ const tokenStr: string = JSON.stringify(credentials);
210
+ await this._storage.setItem(this._tokenSectionName, tokenStr);
211
+ }
212
+ this._credentials = credentials;
213
+ } else {
214
+ if (this._storage) {
215
+ await this._storage.removeItem(this._tokenSectionName);
216
+ }
217
+ this._credentials = null;
218
+ }
219
+ }
220
+
221
+ /**
222
+ * Get credentials.
223
+ * @return {Promise<Credentials | null>}
224
+ */
225
+ public async getCredentials(): Promise<Credentials | null> {
226
+ return this._singlePromise.run('getCredentials', async () => {
227
+ if (isCredentialsExpired(this._credentials)) {
228
+ this._credentials = await this._getStorageCredentials();
229
+ }
230
+ return this._credentials;
231
+ });
232
+ }
233
+
234
+ /**
235
+ * Get storage credentials.
236
+ */
237
+ private async _getStorageCredentials(): Promise<Credentials | null> {
238
+ return this._singlePromise.run('_getStorageCredentials', async () => {
239
+ let credentials: Credentials = null;
240
+ const tokenStr: string = await this._storage.getItem(
241
+ this._tokenSectionName,
242
+ );
243
+ if (tokenStr !== undefined && tokenStr !== null) {
244
+ try {
245
+ credentials = JSON.parse(tokenStr);
246
+ if (credentials?.expires_at) {
247
+ credentials.expires_at = new Date(credentials.expires_at);
248
+ }
249
+ } catch (error) {
250
+ await this._storage.removeItem(this._tokenSectionName);
251
+ credentials = null;
252
+ }
253
+ }
254
+ return credentials;
255
+ });
256
+ }
257
+
258
+ public _getStorageCredentialsSync(): Credentials | null {
259
+ let credentials: Credentials = null;
260
+ const tokenStr: string = this._storage.getItemSync(
261
+ this._tokenSectionName,
262
+ );
263
+ if (tokenStr !== undefined && tokenStr !== null) {
264
+ try {
265
+ credentials = JSON.parse(tokenStr);
266
+ if (credentials?.expires_at) {
267
+ credentials.expires_at = new Date(credentials.expires_at);
268
+ }
269
+ } catch (error) {
270
+ this._storage.removeItem(this._tokenSectionName);
271
+ credentials = null;
272
+ }
273
+ }
274
+ return credentials
275
+ }
276
+
277
+ }
278
+
279
+ /**
280
+ * OAuth2Client
281
+ */
282
+ export class OAuth2Client implements AuthClient {
283
+ private static _defaultRetry = 2;
284
+ private static _minRetry = 0;
285
+ private static _maxRetry = 5;
286
+ private static _retryInterval = 1000;
287
+
288
+ private _apiOrigin: string;
289
+ private _clientId: string;
290
+ private _retry: number;
291
+ private _clientSecret?: string;
292
+ private _baseRequest: <T>(
293
+ url: string,
294
+ options?: RequestOptions,
295
+ ) => Promise<T>;
296
+ private _localCredentials: LocalCredentials;
297
+ private _storage: SimpleStorage;
298
+ private _deviceID?: string;
299
+ private _tokenInURL?: boolean;
300
+ private _refreshTokenFunc: (refreshToken?: string) => Promise<Credentials>;
301
+ private _headers?: { [key: string]: string };
302
+ private _singlePromise: SinglePromise = new SinglePromise();
303
+
304
+ /**
305
+ * constructor
306
+ * @param {OAuth2ClientOptions} options
307
+ */
308
+ constructor(options: OAuth2ClientOptions) {
309
+ this._apiOrigin = options.apiOrigin;
310
+ this._clientId = options.clientId;
311
+ this._retry = this._formatRetry(options.retry, OAuth2Client._defaultRetry);
312
+ if (options.baseRequest != undefined) {
313
+ this._baseRequest = options.baseRequest;
314
+ } else {
315
+ this._baseRequest = defaultRequest;
316
+ }
317
+ this._tokenInURL = options.tokenInURL;
318
+ this._headers = options.headers;
319
+ // @ts-ignore
320
+ this._storage = options.storage || defaultStorage;
321
+ this._localCredentials = new LocalCredentials({
322
+ tokenSectionName: 'credentials_' + options.clientId,
323
+ storage: this._storage,
324
+ });
325
+ this._clientSecret = options.clientSecret;
326
+ this._refreshTokenFunc =
327
+ options.refreshTokenFunc || this._defaultRefreshTokenFunc;
328
+ }
329
+
330
+ /**
331
+ * setCredentials Provides an alternative fetch api request implementation with auth credentials
332
+ * @param {Credentials} credentials
333
+ * @return {Promise<void>}
334
+ */
335
+ public setCredentials(credentials?: Credentials): Promise<void> {
336
+ return this._localCredentials.setCredentials(credentials);
337
+ }
338
+
339
+ /**
340
+ * getAccessToken return a validate access token
341
+ */
342
+ public async getAccessToken(): Promise<string> {
343
+ const credentials: Credentials = await this._getCredentials();
344
+ if (credentials?.access_token) {
345
+ return Promise.resolve(credentials.access_token);
346
+ }
347
+ return Promise.reject({ error: ErrorType.UNAUTHENTICATED } as ResponseError);
348
+ }
349
+
350
+ /**
351
+ * request http like simple fetch api, exp:request('/v1/user/me', {withCredentials:true})
352
+ * @param {string} url
353
+ * @param {AuthClientRequestOptions} options
354
+ */
355
+ public async request<T>(
356
+ url: string,
357
+ options?: AuthClientRequestOptions,
358
+ ): Promise<T> {
359
+ if (!options) {
360
+ options = {};
361
+ }
362
+ const retry: number = this._formatRetry(options.retry, this._retry);
363
+ options.headers = options.headers || {};
364
+ if (this._headers) {
365
+ options.headers = {
366
+ ...this._headers,
367
+ ...options.headers,
368
+ };
369
+ }
370
+ if (!options.headers[RequestIdHeaderName]) {
371
+ options.headers[RequestIdHeaderName] = generateRequestId();
372
+ }
373
+ if (!options.headers[DeviceIdHeaderName]) {
374
+ const deviceId = await this._getDeviceId();
375
+ options.headers[DeviceIdHeaderName] = deviceId;
376
+ }
377
+ if (options?.withCredentials) {
378
+ const credentials = await this._getCredentials();
379
+ if (credentials) {
380
+ if (this._tokenInURL) {
381
+ if (url.indexOf('?') < 0) {
382
+ url += '?';
383
+ }
384
+ url += 'access_token=' + credentials.access_token;
385
+ } else {
386
+ options.headers['Authorization'] =
387
+ credentials.token_type + ' ' + credentials.access_token;
388
+ }
389
+ }
390
+ } else {
391
+ if (this._clientId && url.indexOf('client_id') < 0) {
392
+ url += url.indexOf('?') < 0 ? '?' : '&';
393
+ url += 'client_id=' + this._clientId;
394
+ }
395
+ }
396
+ if (url.startsWith('/')) {
397
+ url = this._apiOrigin + url;
398
+ }
399
+ let response: T | null = null;
400
+ const maxRequestTimes: number = retry + 1;
401
+ for (
402
+ let requestTime = 0;
403
+ requestTime < maxRequestTimes;
404
+ requestTime++
405
+ ) {
406
+ try {
407
+ response = await this._baseRequest<T>(url, options);
408
+ break;
409
+ } catch (responseError) {
410
+ if (
411
+ requestTime === retry ||
412
+ !responseError ||
413
+ responseError.error !== 'unreachable'
414
+ ) {
415
+ return Promise.reject(responseError);
416
+ }
417
+ }
418
+ await this._sleep(OAuth2Client._retryInterval);
419
+ }
420
+ return response;
421
+ }
422
+
423
+ /**
424
+ * Check retry value.
425
+ * @param {number} retry
426
+ * @return {number}
427
+ */
428
+ private _checkRetry(retry: number): number {
429
+ let responseError: ResponseError | null = null;
430
+ if (
431
+ typeof retry !== 'number' ||
432
+ retry < OAuth2Client._minRetry ||
433
+ retry > OAuth2Client._maxRetry
434
+ ) {
435
+ responseError = {
436
+ error: ErrorType.UNREACHABLE,
437
+ error_description: 'wrong options param: retry',
438
+ };
439
+ }
440
+ if (responseError) {
441
+ throw responseError;
442
+ }
443
+ return retry;
444
+ }
445
+
446
+ /**
447
+ * Format retry value.
448
+ * @param {number} retry
449
+ * @param {number} defaultVale
450
+ * @return {number}
451
+ */
452
+ private _formatRetry(retry: number, defaultVale: number): number {
453
+ if (typeof retry === 'undefined') {
454
+ return defaultVale;
455
+ } else {
456
+ return this._checkRetry(retry);
457
+ }
458
+ }
459
+
460
+ /**
461
+ * Sleep.
462
+ * @param {number} ms
463
+ * @return {Promise<void>}
464
+ */
465
+ private async _sleep(ms: number): Promise<void> {
466
+ return new Promise<void>((resolve) => {
467
+ setTimeout(() => {
468
+ resolve();
469
+ }, ms);
470
+ });
471
+ }
472
+
473
+ /**
474
+ * Refresh expired token.
475
+ * @param {Credentials} credentials
476
+ * @return {Promise<Credentials>}
477
+ */
478
+ private async _refreshToken(credentials: Credentials): Promise<Credentials> {
479
+ return this._singlePromise.run('_refreshToken', async () => {
480
+ if (!credentials || !credentials.refresh_token) {
481
+ return this._unAuthenticatedError('no refresh token found in credentials');
482
+ }
483
+ try {
484
+ const newCredentials: Credentials = await this._refreshTokenFunc(
485
+ credentials.refresh_token,
486
+ );
487
+ await this._localCredentials.setCredentials(newCredentials);
488
+ return newCredentials
489
+ } catch (error) {
490
+ if (error.error === ErrorType.INVALID_GRANT) {
491
+ await this._localCredentials.setCredentials(null);
492
+ return this._unAuthenticatedError(error.error_description);
493
+ }
494
+ return Promise.reject(error);
495
+ }
496
+ });
497
+ }
498
+
499
+ /**
500
+ * anonymous signIn
501
+ * @param {Credentials} credentials
502
+ * @return {Promise<Credentials>}
503
+ */
504
+ private async _anonymousSignIn(credentials: Credentials): Promise<Credentials> {
505
+ return this._singlePromise.run('_anonymous', async () => {
506
+ if (!credentials || credentials.scope !== 'anonymous') {
507
+ return this._unAuthenticatedError('no anonymous in credentials');
508
+ }
509
+ try {
510
+ const newCredentials: Credentials = await this.request('/auth/v1/signin/anonymously', {
511
+ method: 'POST',
512
+ body: {
513
+ client_id: this._clientId,
514
+ client_secret: this._clientSecret,
515
+ },
516
+ });
517
+ await this._localCredentials.setCredentials(newCredentials);
518
+ return newCredentials
519
+ } catch (error) {
520
+ if (error.error === ErrorType.INVALID_GRANT) {
521
+ await this._localCredentials.setCredentials(null);
522
+ return this._unAuthenticatedError(error.error_description);
523
+ }
524
+ return Promise.reject(error);
525
+ }
526
+ });
527
+ }
528
+
529
+ /**
530
+ * Default refresh token function.
531
+ * @param {string} refreshToken
532
+ * @return {Promise<Credentials>}
533
+ */
534
+ private _defaultRefreshTokenFunc(
535
+ refreshToken?: string,
536
+ ): Promise<Credentials> {
537
+ if (refreshToken === undefined || refreshToken === '') {
538
+ return this._unAuthenticatedError('refresh token not found');
539
+ }
540
+ return this.request('/auth/v1/token', {
541
+ method: 'POST',
542
+ body: {
543
+ client_id: this._clientId,
544
+ client_secret: this._clientSecret,
545
+ grant_type: 'refresh_token',
546
+ refresh_token: refreshToken,
547
+ },
548
+ });
549
+ }
550
+
551
+ /**
552
+ * Get credentials.
553
+ */
554
+ public async _getCredentials(): Promise<Credentials | null> {
555
+ let credentials: Credentials = await this._localCredentials.getCredentials();
556
+ if (isCredentialsExpired(credentials)) {
557
+ if (credentials && credentials.scope === 'anonymous') {
558
+ credentials = await this._anonymousSignIn(credentials)
559
+ } else {
560
+ credentials = await this._refreshToken(credentials);
561
+ }
562
+ }
563
+ return credentials;
564
+ }
565
+
566
+ public getCredentialsSync(): Credentials | null {
567
+ const credentials: Credentials = this._localCredentials._getStorageCredentialsSync();
568
+ return credentials
569
+ }
570
+
571
+ public getCredentialsAsync(): Promise<Credentials | null> {
572
+ return this._localCredentials.getCredentials()
573
+ }
574
+
575
+ public async getScope(): Promise<string> {
576
+ let credentials: Credentials = await this._localCredentials.getCredentials();
577
+ if (credentials == null) {
578
+ return this._unAuthenticatedError("credentials not found")
579
+ }
580
+ return credentials.scope;
581
+ }
582
+
583
+
584
+
585
+ /**
586
+ * Get deviceId
587
+ */
588
+ private async _getDeviceId(): Promise<string> {
589
+ if (this._deviceID) {
590
+ return this._deviceID;
591
+ }
592
+ let deviceId: string = await this._storage.getItem(
593
+ DeviceIdSectionName,
594
+ );
595
+ if (!(typeof deviceId === 'string' &&
596
+ deviceId.length >= 16 &&
597
+ deviceId.length <= 48)) {
598
+ deviceId = uuidv4();
599
+ await this._storage.setItem(DeviceIdSectionName, deviceId);
600
+ }
601
+ this._deviceID = deviceId;
602
+ return deviceId;
603
+ }
604
+ /**
605
+ * Generate unAuthenticated error.
606
+ * @param {string} err
607
+ * @return {Promise<T>}
608
+ */
609
+ private _unAuthenticatedError<T>(err?: string): Promise<T> {
610
+ return Promise.reject({
611
+ error: ErrorType.UNAUTHENTICATED,
612
+ error_description: err,
613
+ } as ResponseError);
614
+ }
615
+ }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Single Promise
3
+ */
4
+ export class SinglePromise {
5
+ /**
6
+ * Run single promise.
7
+ * @param {string} key
8
+ * @param {Function} fn
9
+ * @return {Promise<T>}
10
+ */
11
+ async run<T>(key: string, fn: () => Promise<T>): Promise<T> {
12
+ let result: Promise<any> = this._fnPromiseMap.get(key);
13
+ if (!result) {
14
+ result = new Promise<any>(async (resolve, reject) => {
15
+ try {
16
+ // The idle promise must be run to prevent _fnPromiseMap from
17
+ // storing the current promise function.
18
+ await this._runIdlePromise();
19
+ const fnResult: Promise<T> = fn();
20
+ resolve(await fnResult);
21
+ } catch (error) {
22
+ reject(error);
23
+ } finally {
24
+ this._fnPromiseMap.delete(key);
25
+ }
26
+ });
27
+ this._fnPromiseMap.set(key, result);
28
+ }
29
+ return result;
30
+ }
31
+ /**
32
+ * Run idle promise.
33
+ * @return {Promise<void>}
34
+ */
35
+ private _runIdlePromise(): Promise<void> {
36
+ return Promise.resolve();
37
+ }
38
+
39
+ private _fnPromiseMap: Map<string, Promise<any>> = new Map();
40
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Generate uuidv4 string.
3
+ * @return {string}
4
+ */
5
+ export function uuidv4(): string {
6
+ return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
7
+ const r = (Math.random() * 16) | 0;
8
+ const v = c == 'x' ? r : (r & 0x3) | 0x8;
9
+ return v.toString(16);
10
+ });
11
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "compileOnSave": true,
3
+ "compilerOptions": {
4
+ "allowSyntheticDefaultImports": true,
5
+ "declaration": true,
6
+ "esModuleInterop": true,
7
+ "experimentalDecorators": true,
8
+ "emitDecoratorMetadata": true,
9
+ "module": "commonjs",
10
+ "noFallthroughCasesInSwitch": true,
11
+ "noUnusedLocals": true,
12
+ "noUnusedParameters": true,
13
+ "outDir": "dist",
14
+ "rootDir": "src",
15
+ "pretty": true,
16
+ "noImplicitAny": false,
17
+ "removeComments": true,
18
+ "skipLibCheck": true,
19
+ "strictBindCallApply": true,
20
+ "declarationMap": false,
21
+ "inlineSourceMap": true,
22
+ "inlineSources": true,
23
+ "target": "es5",
24
+ "moduleResolution": "node",
25
+ "typeRoots": [
26
+ "node_modules/@types"
27
+ ],
28
+ "types": [
29
+ "node"
30
+ ],
31
+ "lib": [
32
+ "es2015",
33
+ "dom"
34
+ ]
35
+ },
36
+ "include": [
37
+ "src/**/*.ts",
38
+ "package.json"
39
+ ],
40
+ "exclude": [
41
+ "node_modules",
42
+ "dist"
43
+ ]
44
+ }