@cinerino/sdk 11.0.0 → 11.1.0-alpha.0

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.
@@ -15,6 +15,9 @@ export async function login() {
15
15
  redirectUri: 'https://localhost/signIn',
16
16
  logoutUri: 'https://localhost/signOut'
17
17
  });
18
+ auth.onTokenRefreshed(async (params) => {
19
+ console.log('onTokenRefreshed called.', params);
20
+ });
18
21
 
19
22
  const scopes: string[] = [];
20
23
  const state = '12345';
@@ -48,13 +51,13 @@ export async function login() {
48
51
  return;
49
52
  }
50
53
 
51
- let credentials = await auth.getToken(code, codeVerifier);
54
+ const credentials = await auth.getToken(code, codeVerifier);
52
55
  // console.log('credentials published', credentials);
53
56
 
54
57
  auth.setCredentials(credentials);
55
58
 
56
- credentials = await auth.refreshAccessToken();
57
- console.log('credentials refreshed', credentials);
59
+ // credentials = await auth.refreshAccessToken();
60
+ // console.log('credentials refreshed', credentials);
58
61
 
59
62
  // rl.close();
60
63
  resolve();
@@ -1,4 +1,4 @@
1
- import ICredentials from './credentials';
1
+ import { ICredentials } from './credentials';
2
2
  import OAuth2client from './oAuth2client';
3
3
  export interface IOptions {
4
4
  domain: string;
@@ -31,4 +31,4 @@ interface ICredentials {
31
31
  idTokenPayload?: any;
32
32
  state?: string;
33
33
  }
34
- export default ICredentials;
34
+ export { ICredentials };
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  import type { AuthClient } from '../abstract/auth/authClient';
3
- import type ICredentials from './credentials';
3
+ import { ICredentials } from './credentials';
4
4
  import { LoginTicket } from './loginTicket';
5
5
  export declare const MAX_RETRY_NUM = 2;
6
6
  export interface IGenerateAuthUrlOpts {
@@ -8,6 +8,8 @@ export interface IGenerateAuthUrlOpts {
8
8
  state: string;
9
9
  codeVerifier?: string;
10
10
  }
11
+ declare type IOnTokenRefreshedParams = Pick<ICredentials, 'access_token' | 'expiry_date' | 'id_token' | 'token_type' | 'refresh_token'>;
12
+ declare type IOnTokenRefreshed = (params: IOnTokenRefreshedParams) => Promise<void>;
11
13
  export interface IOptions {
12
14
  domain: string;
13
15
  clientId?: string;
@@ -47,6 +49,10 @@ export default class OAuth2client implements AuthClient {
47
49
  */
48
50
  credentials: ICredentials;
49
51
  options: IOptions;
52
+ /**
53
+ * 認証クライアントインスタンス内で発生するイベント
54
+ */
55
+ private readonly events;
50
56
  constructor(options: IOptions);
51
57
  static BASE64URLENCODE(str: Buffer): string;
52
58
  static SHA256(buffer: any): Buffer;
@@ -89,6 +95,10 @@ export default class OAuth2client implements AuthClient {
89
95
  * 結果にはIDトークンの付加情報が含まれます。
90
96
  */
91
97
  verifyIdToken(options: IVerifyIdTokenOptions): LoginTicket;
98
+ /**
99
+ * トークン更新イベントハンドラを登録する
100
+ */
101
+ onTokenRefreshed(params: IOnTokenRefreshed): void;
92
102
  /**
93
103
  * Provides a request implementation with OAuth 2.0 flow.
94
104
  * If credentials have a refresh_token, in cases of HTTP
@@ -120,3 +130,4 @@ export default class OAuth2client implements AuthClient {
120
130
  */
121
131
  private verifySignedJwt;
122
132
  }
133
+ export {};
@@ -1,4 +1,15 @@
1
1
  "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
2
13
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
14
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
15
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -70,6 +81,7 @@ var OAuth2client = /** @class */ (function () {
70
81
  // TODO add minimum validation
71
82
  this.options = options;
72
83
  this.credentials = {};
84
+ this.events = {};
73
85
  }
74
86
  OAuth2client.BASE64URLENCODE = function (str) {
75
87
  return str.toString('base64')
@@ -188,12 +200,22 @@ var OAuth2client = /** @class */ (function () {
188
200
  throw new Error('No refresh token is set.');
189
201
  }
190
202
  return [2 /*return*/, this.refreshToken(this.credentials.refresh_token)
191
- .then(function (tokens) {
192
- tokens.refresh_token = _this.credentials.refresh_token;
193
- debug('setting credentials...', tokens);
194
- _this.credentials = tokens;
195
- return _this.credentials;
196
- })];
203
+ .then(function (refreshTokenResult) { return __awaiter(_this, void 0, void 0, function () {
204
+ return __generator(this, function (_a) {
205
+ switch (_a.label) {
206
+ case 0:
207
+ // tokens.refresh_token = this.credentials.refresh_token;
208
+ debug('setting credentials...', refreshTokenResult);
209
+ this.credentials = __assign(__assign({}, refreshTokenResult), { refresh_token: this.credentials.refresh_token });
210
+ if (!(typeof this.events.onTokenRefreshed === 'function')) return [3 /*break*/, 2];
211
+ return [4 /*yield*/, this.events.onTokenRefreshed(this.credentials)];
212
+ case 1:
213
+ _a.sent();
214
+ _a.label = 2;
215
+ case 2: return [2 /*return*/, this.credentials];
216
+ }
217
+ });
218
+ }); })];
197
219
  });
198
220
  });
199
221
  };
@@ -339,6 +361,12 @@ var OAuth2client = /** @class */ (function () {
339
361
  // return this.verifySignedJwt(options.idToken, options.audience, OAuth2Client.ISSUERS_);
340
362
  return this.verifySignedJwt(this.credentials.id_token, options.audience);
341
363
  };
364
+ /**
365
+ * トークン更新イベントハンドラを登録する
366
+ */
367
+ OAuth2client.prototype.onTokenRefreshed = function (params) {
368
+ this.events.onTokenRefreshed = params;
369
+ };
342
370
  /**
343
371
  * Provides a request implementation with OAuth 2.0 flow.
344
372
  * If credentials have a refresh_token, in cases of HTTP
@@ -404,7 +432,7 @@ var OAuth2client = /** @class */ (function () {
404
432
  debug('fetching...', options);
405
433
  return [2 /*return*/, fetch("https://" + this.options.domain + OAuth2client.OAUTH2_TOKEN_URI, options)
406
434
  .then(function (response) { return __awaiter(_this, void 0, void 0, function () {
407
- var body, body, tokens;
435
+ var body, body, result, refreshTokenResponse, access_token, id_token, expires_in, token_type;
408
436
  return __generator(this, function (_a) {
409
437
  switch (_a.label) {
410
438
  case 0:
@@ -420,17 +448,29 @@ var OAuth2client = /** @class */ (function () {
420
448
  body = _a.sent();
421
449
  throw new Error(body);
422
450
  case 4: return [3 /*break*/, 7];
423
- case 5: return [4 /*yield*/, response.json()];
451
+ case 5:
452
+ result = void 0;
453
+ return [4 /*yield*/, response.json()];
424
454
  case 6:
425
- tokens = _a.sent();
455
+ refreshTokenResponse = _a.sent();
426
456
  // tslint:disable-next-line:no-single-line-block-comment
427
457
  /* istanbul ignore else */
428
- if (tokens && tokens.expires_in) {
429
- // tslint:disable-next-line:no-magic-numbers
430
- tokens.expiry_date = ((new Date()).getTime() + (tokens.expires_in * 1000));
431
- delete tokens.expires_in;
458
+ if (refreshTokenResponse !== undefined && typeof refreshTokenResponse.expires_in === 'number') {
459
+ access_token = refreshTokenResponse.access_token, id_token = refreshTokenResponse.id_token, expires_in = refreshTokenResponse.expires_in, token_type = refreshTokenResponse.token_type;
460
+ result = {
461
+ access_token: access_token, id_token: id_token, token_type: token_type,
462
+ // tslint:disable-next-line:no-magic-numbers
463
+ expiry_date: ((new Date()).getTime() + (expires_in * 1000))
464
+ };
465
+ // delete tokens.expires_in;
432
466
  }
433
- return [2 /*return*/, tokens];
467
+ else {
468
+ // 想定していないリフレッシュトークンレスポンスはログ出力
469
+ // tslint:disable-next-line:no-console
470
+ console.error('unexpected refreshToken response:', refreshTokenResponse);
471
+ throw new Error('unexpected refreshToken response.');
472
+ }
473
+ return [2 /*return*/, result];
434
474
  case 7: return [2 /*return*/];
435
475
  }
436
476
  });
package/lib/bundle.js CHANGED
@@ -27226,6 +27226,17 @@ exports.LoginTicket = LoginTicket;
27226
27226
  },{}],173:[function(require,module,exports){
27227
27227
  (function (Buffer){
27228
27228
  "use strict";
27229
+ var __assign = (this && this.__assign) || function () {
27230
+ __assign = Object.assign || function(t) {
27231
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
27232
+ s = arguments[i];
27233
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
27234
+ t[p] = s[p];
27235
+ }
27236
+ return t;
27237
+ };
27238
+ return __assign.apply(this, arguments);
27239
+ };
27229
27240
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
27230
27241
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27231
27242
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -27297,6 +27308,7 @@ var OAuth2client = /** @class */ (function () {
27297
27308
  // TODO add minimum validation
27298
27309
  this.options = options;
27299
27310
  this.credentials = {};
27311
+ this.events = {};
27300
27312
  }
27301
27313
  OAuth2client.BASE64URLENCODE = function (str) {
27302
27314
  return str.toString('base64')
@@ -27415,12 +27427,22 @@ var OAuth2client = /** @class */ (function () {
27415
27427
  throw new Error('No refresh token is set.');
27416
27428
  }
27417
27429
  return [2 /*return*/, this.refreshToken(this.credentials.refresh_token)
27418
- .then(function (tokens) {
27419
- tokens.refresh_token = _this.credentials.refresh_token;
27420
- debug('setting credentials...', tokens);
27421
- _this.credentials = tokens;
27422
- return _this.credentials;
27423
- })];
27430
+ .then(function (refreshTokenResult) { return __awaiter(_this, void 0, void 0, function () {
27431
+ return __generator(this, function (_a) {
27432
+ switch (_a.label) {
27433
+ case 0:
27434
+ // tokens.refresh_token = this.credentials.refresh_token;
27435
+ debug('setting credentials...', refreshTokenResult);
27436
+ this.credentials = __assign(__assign({}, refreshTokenResult), { refresh_token: this.credentials.refresh_token });
27437
+ if (!(typeof this.events.onTokenRefreshed === 'function')) return [3 /*break*/, 2];
27438
+ return [4 /*yield*/, this.events.onTokenRefreshed(this.credentials)];
27439
+ case 1:
27440
+ _a.sent();
27441
+ _a.label = 2;
27442
+ case 2: return [2 /*return*/, this.credentials];
27443
+ }
27444
+ });
27445
+ }); })];
27424
27446
  });
27425
27447
  });
27426
27448
  };
@@ -27566,6 +27588,12 @@ var OAuth2client = /** @class */ (function () {
27566
27588
  // return this.verifySignedJwt(options.idToken, options.audience, OAuth2Client.ISSUERS_);
27567
27589
  return this.verifySignedJwt(this.credentials.id_token, options.audience);
27568
27590
  };
27591
+ /**
27592
+ * トークン更新イベントハンドラを登録する
27593
+ */
27594
+ OAuth2client.prototype.onTokenRefreshed = function (params) {
27595
+ this.events.onTokenRefreshed = params;
27596
+ };
27569
27597
  /**
27570
27598
  * Provides a request implementation with OAuth 2.0 flow.
27571
27599
  * If credentials have a refresh_token, in cases of HTTP
@@ -27631,7 +27659,7 @@ var OAuth2client = /** @class */ (function () {
27631
27659
  debug('fetching...', options);
27632
27660
  return [2 /*return*/, fetch("https://" + this.options.domain + OAuth2client.OAUTH2_TOKEN_URI, options)
27633
27661
  .then(function (response) { return __awaiter(_this, void 0, void 0, function () {
27634
- var body, body, tokens;
27662
+ var body, body, result, refreshTokenResponse, access_token, id_token, expires_in, token_type;
27635
27663
  return __generator(this, function (_a) {
27636
27664
  switch (_a.label) {
27637
27665
  case 0:
@@ -27647,17 +27675,29 @@ var OAuth2client = /** @class */ (function () {
27647
27675
  body = _a.sent();
27648
27676
  throw new Error(body);
27649
27677
  case 4: return [3 /*break*/, 7];
27650
- case 5: return [4 /*yield*/, response.json()];
27678
+ case 5:
27679
+ result = void 0;
27680
+ return [4 /*yield*/, response.json()];
27651
27681
  case 6:
27652
- tokens = _a.sent();
27682
+ refreshTokenResponse = _a.sent();
27653
27683
  // tslint:disable-next-line:no-single-line-block-comment
27654
27684
  /* istanbul ignore else */
27655
- if (tokens && tokens.expires_in) {
27656
- // tslint:disable-next-line:no-magic-numbers
27657
- tokens.expiry_date = ((new Date()).getTime() + (tokens.expires_in * 1000));
27658
- delete tokens.expires_in;
27685
+ if (refreshTokenResponse !== undefined && typeof refreshTokenResponse.expires_in === 'number') {
27686
+ access_token = refreshTokenResponse.access_token, id_token = refreshTokenResponse.id_token, expires_in = refreshTokenResponse.expires_in, token_type = refreshTokenResponse.token_type;
27687
+ result = {
27688
+ access_token: access_token, id_token: id_token, token_type: token_type,
27689
+ // tslint:disable-next-line:no-magic-numbers
27690
+ expiry_date: ((new Date()).getTime() + (expires_in * 1000))
27691
+ };
27692
+ // delete tokens.expires_in;
27659
27693
  }
27660
- return [2 /*return*/, tokens];
27694
+ else {
27695
+ // 想定していないリフレッシュトークンレスポンスはログ出力
27696
+ // tslint:disable-next-line:no-console
27697
+ console.error('unexpected refreshToken response:', refreshTokenResponse);
27698
+ throw new Error('unexpected refreshToken response.');
27699
+ }
27700
+ return [2 /*return*/, result];
27661
27701
  case 7: return [2 /*return*/];
27662
27702
  }
27663
27703
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cinerino/sdk",
3
- "version": "11.0.0",
3
+ "version": "11.1.0-alpha.0",
4
4
  "description": "Cinerino SDK",
5
5
  "main": "./lib/index.js",
6
6
  "browser": {