@leapdev/auth-agent 2.5.4 → 2.6.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.
package/README.md CHANGED
@@ -273,6 +273,19 @@ Create an auth session and open a new app
273
273
  - `newWindow` **[boolean][37]** If true, the new app will be open in the new browser tab. If false, app will be open in the current browser tab. (optional, default `false`)
274
274
  - `authHost` **[string][36]** The authHost that the new app is using. (optional, default value is the same authHost as current app)
275
275
 
276
+ ## getRefreshTokenForApp
277
+
278
+ Getting an RefreshToken For App.
279
+ In order to be able get a refreshToken for an App, app needs to
280
+ - have "offline_access" scope enabled in its client
281
+ - have `${leap-auth-url}/oauth/close` in its client's "allowed redirect urls"
282
+
283
+ ### Parameters
284
+
285
+ - `clientId` **[string][36]** clientId of the app (requried)
286
+ - `scopes` **[Array<string>][37]** requested scopes
287
+
288
+
276
289
  [1]: #init
277
290
 
278
291
  [2]: #parameters
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leapdev/auth-agent",
3
- "version": "2.5.4",
3
+ "version": "2.6.0",
4
4
  "description": "LEAP Auth Agent",
5
5
  "license": "LEAP Legal Software, PTY LTD",
6
6
  "main": "./src/index.js",
@@ -8,7 +8,7 @@
8
8
  "dependencies": {
9
9
  "lodash": "^4.17.21",
10
10
  "uuid": "^8.3.2",
11
- "pubnub": "10.2.3",
11
+ "pubnub": "11.0.1",
12
12
  "tslib": "^2.3.0"
13
13
  },
14
14
  "peerDependencies": {}
package/src/index.umd.js CHANGED
@@ -4470,7 +4470,7 @@
4470
4470
  grant_type: 'authorization_code',
4471
4471
  code: params.code,
4472
4472
  code_verifier: params.verifier,
4473
- client_id: __classPrivateFieldGet(this, _LeapAuthService_clientId, "f"),
4473
+ client_id: params.clientId || __classPrivateFieldGet(this, _LeapAuthService_clientId, "f"),
4474
4474
  redirect_uri: params.redirectUri
4475
4475
  }
4476
4476
  },
@@ -7242,6 +7242,79 @@
7242
7242
  iframe.setAttribute('src', authorizeUrl);
7243
7243
  });
7244
7244
  };
7245
+ this.getRefreshTokenForApp = params => __awaiter(this, void 0, void 0, function* () {
7246
+ const {
7247
+ clientId,
7248
+ scopes
7249
+ } = params;
7250
+ const code_verifier = createRandomString(64);
7251
+ const state = createRandomString(6);
7252
+ const nonce = createRandomString(6);
7253
+ const {
7254
+ code_challenge,
7255
+ code_challenge_method
7256
+ } = yield createCodeChallenge(code_verifier);
7257
+ const requestScopesArray = scopes || [];
7258
+ requestScopesArray.push('offline_access');
7259
+ const request_scopes = requestScopesArray.join(',');
7260
+ const redirectUri = `${__classPrivateFieldGet(this, _Authentication_config, "f").authHost}/oauth/close`;
7261
+ const url = `${__classPrivateFieldGet(this, _Authentication_config, "f").authHost}/oauth/authorize?response_type=code&scope=${request_scopes}&client_id=${clientId}&redirect_uri=${encodeURIComponent(redirectUri)}&code_challenge=${encodeURIComponent(code_challenge)}&code_challenge_method=${code_challenge_method}&nonce=${nonce}&state=${state}&prompt=none`;
7262
+ return new Promise((res, rej) => {
7263
+ const newWindow = window.open(url, '_blank', 'width=800,height=600');
7264
+ if (!newWindow) {
7265
+ rej('unable to open new window');
7266
+ return;
7267
+ }
7268
+ const timeoutSetTimeoutId = setTimeout(() => {
7269
+ rej('get-refresh-token timeout');
7270
+ window.removeEventListener('message', windowEventHandler, false);
7271
+ newWindow.close();
7272
+ }, DEFAULT_AUTHORIZE_TIMEOUT_IN_SECONDS * 1000);
7273
+ const windowEventHandler = e => __awaiter(this, void 0, void 0, function* () {
7274
+ if (e.origin !== __classPrivateFieldGet(this, _Authentication_config, "f").authHost) return;
7275
+ if (e.data.type !== 'closeMe') return;
7276
+ newWindow.close();
7277
+ clearTimeout(timeoutSetTimeoutId);
7278
+ window.removeEventListener('message', windowEventHandler, false);
7279
+ if (e.data.queryParams) {
7280
+ const {
7281
+ code,
7282
+ state: stateFromCloseWindow
7283
+ } = e.data.queryParams;
7284
+ if (code && state && state === stateFromCloseWindow) {
7285
+ try {
7286
+ const result = yield __classPrivateFieldGet(this, _Authentication_leapAuthService, "f").exchangeAuthCodeForAccessToken({
7287
+ code,
7288
+ verifier: code_verifier,
7289
+ redirectUri: redirectUri,
7290
+ clientId: clientId
7291
+ });
7292
+ if (result && result.refresh_token) {
7293
+ res({
7294
+ refreshToken: result.refresh_token,
7295
+ code_verifier: code_verifier
7296
+ });
7297
+ } else {
7298
+ throw new Error('Failed to exchange auth code for access token');
7299
+ }
7300
+ } catch (_) {
7301
+ console.warn('Failed to exchange auth code for access token');
7302
+ res({
7303
+ refreshToken: undefined,
7304
+ code_verifier: undefined
7305
+ });
7306
+ }
7307
+ }
7308
+ } else {
7309
+ res({
7310
+ refreshToken: undefined,
7311
+ code_verifier: undefined
7312
+ });
7313
+ }
7314
+ });
7315
+ window.addEventListener('message', windowEventHandler);
7316
+ });
7317
+ });
7245
7318
  _Authentication_exchangeAuthCodeForAccessToken.set(this, params => __awaiter(this, void 0, void 0, function* () {
7246
7319
  const {
7247
7320
  verifier,
@@ -7660,6 +7733,19 @@
7660
7733
  authHost
7661
7734
  });
7662
7735
  });
7736
+ const getRefreshTokenForApp = params => __awaiter(void 0, void 0, void 0, function* () {
7737
+ const {
7738
+ clientId,
7739
+ scopes
7740
+ } = params;
7741
+ if (!auth) {
7742
+ throw Error('Not init yet');
7743
+ }
7744
+ return auth.getRefreshTokenForApp({
7745
+ clientId,
7746
+ scopes
7747
+ });
7748
+ });
7663
7749
  const AuthAgent = {
7664
7750
  init,
7665
7751
  registerHook,
@@ -7684,7 +7770,8 @@
7684
7770
  authoriseSupport,
7685
7771
  changePassword,
7686
7772
  registerEventListener,
7687
- passthrough
7773
+ passthrough,
7774
+ getRefreshTokenForApp
7688
7775
  };
7689
7776
 
7690
7777
  exports.AuthAgent = AuthAgent;
@@ -26,4 +26,11 @@ export declare const AuthAgent: {
26
26
  changePassword: (redirectUrl?: string | undefined, newWindow?: boolean | undefined, callback?: any) => void;
27
27
  registerEventListener: (topic: string, messageType: string, callback: any) => void;
28
28
  passthrough: (url: string, newWindow?: boolean, authHost?: string | undefined) => Promise<void>;
29
+ getRefreshTokenForApp: (params: {
30
+ clientId: string;
31
+ scopes?: string[];
32
+ }) => Promise<{
33
+ refreshToken: string | undefined;
34
+ code_verifier: string | undefined;
35
+ }>;
29
36
  };
@@ -195,6 +195,13 @@ const passthrough = (url, newWindow = false, authHost) => __awaiter(void 0, void
195
195
  }
196
196
  yield auth.passthrough({ url, newWindow, authHost });
197
197
  });
198
+ const getRefreshTokenForApp = (params) => __awaiter(void 0, void 0, void 0, function* () {
199
+ const { clientId, scopes } = params;
200
+ if (!auth) {
201
+ throw Error('Not init yet');
202
+ }
203
+ return auth.getRefreshTokenForApp({ clientId, scopes });
204
+ });
198
205
  export const AuthAgent = {
199
206
  init,
200
207
  registerHook,
@@ -220,4 +227,5 @@ export const AuthAgent = {
220
227
  changePassword,
221
228
  registerEventListener,
222
229
  passthrough,
230
+ getRefreshTokenForApp,
223
231
  };
@@ -8,6 +8,7 @@ export declare class LeapAuthService {
8
8
  code: string;
9
9
  redirectUri: string;
10
10
  verifier: string;
11
+ clientId?: string;
11
12
  }) => Promise<LeapToken>;
12
13
  renewAccessToken: (params: {
13
14
  refreshToken: string;
@@ -52,7 +52,7 @@ export class LeapAuthService {
52
52
  grant_type: 'authorization_code',
53
53
  code: params.code,
54
54
  code_verifier: params.verifier,
55
- client_id: __classPrivateFieldGet(this, _LeapAuthService_clientId, "f"),
55
+ client_id: params.clientId || __classPrivateFieldGet(this, _LeapAuthService_clientId, "f"),
56
56
  redirect_uri: params.redirectUri,
57
57
  },
58
58
  },
@@ -77,4 +77,11 @@ export declare class Authentication {
77
77
  triggerHooks: (hookName: HookName) => Promise<any>;
78
78
  afterAuthenticated: () => Promise<string | undefined>;
79
79
  runIframe: (authorizeUrl: string, state: string, timeoutInSeconds?: number) => Promise<any>;
80
+ getRefreshTokenForApp: (params: {
81
+ clientId: string;
82
+ scopes?: string[];
83
+ }) => Promise<{
84
+ refreshToken: string | undefined;
85
+ code_verifier: string | undefined;
86
+ }>;
80
87
  }
@@ -421,6 +421,69 @@ export class Authentication {
421
421
  iframe.setAttribute('src', authorizeUrl);
422
422
  });
423
423
  };
424
+ this.getRefreshTokenForApp = (params) => __awaiter(this, void 0, void 0, function* () {
425
+ const { clientId, scopes } = params;
426
+ const code_verifier = createRandomString(64);
427
+ const state = createRandomString(6);
428
+ const nonce = createRandomString(6);
429
+ const { code_challenge, code_challenge_method } = yield createCodeChallenge(code_verifier);
430
+ const requestScopesArray = scopes || [];
431
+ requestScopesArray.push('offline_access');
432
+ const request_scopes = requestScopesArray.join(',');
433
+ const redirectUri = `${__classPrivateFieldGet(this, _Authentication_config, "f").authHost}/oauth/close`;
434
+ const url = `${__classPrivateFieldGet(this, _Authentication_config, "f").authHost}/oauth/authorize?response_type=code&scope=${request_scopes}&client_id=${clientId}&redirect_uri=${encodeURIComponent(redirectUri)}&code_challenge=${encodeURIComponent(code_challenge)}&code_challenge_method=${code_challenge_method}&nonce=${nonce}&state=${state}&prompt=none`;
435
+ return new Promise((res, rej) => {
436
+ const newWindow = window.open(url, '_blank', 'width=800,height=600');
437
+ if (!newWindow) {
438
+ rej('unable to open new window');
439
+ return;
440
+ }
441
+ const timeoutSetTimeoutId = setTimeout(() => {
442
+ rej('get-refresh-token timeout');
443
+ window.removeEventListener('message', windowEventHandler, false);
444
+ newWindow.close();
445
+ }, DEFAULT_AUTHORIZE_TIMEOUT_IN_SECONDS * 1000);
446
+ const windowEventHandler = (e) => __awaiter(this, void 0, void 0, function* () {
447
+ if (e.origin !== __classPrivateFieldGet(this, _Authentication_config, "f").authHost)
448
+ return;
449
+ if (e.data.type !== 'closeMe')
450
+ return;
451
+ newWindow.close();
452
+ clearTimeout(timeoutSetTimeoutId);
453
+ window.removeEventListener('message', windowEventHandler, false);
454
+ if (e.data.queryParams) {
455
+ const { code, state: stateFromCloseWindow } = e.data.queryParams;
456
+ if (code && state && state === stateFromCloseWindow) {
457
+ try {
458
+ const result = yield __classPrivateFieldGet(this, _Authentication_leapAuthService, "f").exchangeAuthCodeForAccessToken({
459
+ code,
460
+ verifier: code_verifier,
461
+ redirectUri: redirectUri,
462
+ clientId: clientId,
463
+ });
464
+ if (result && result.refresh_token) {
465
+ res({
466
+ refreshToken: result.refresh_token,
467
+ code_verifier: code_verifier,
468
+ });
469
+ }
470
+ else {
471
+ throw new Error('Failed to exchange auth code for access token');
472
+ }
473
+ }
474
+ catch (_) {
475
+ console.warn('Failed to exchange auth code for access token');
476
+ res({ refreshToken: undefined, code_verifier: undefined });
477
+ }
478
+ }
479
+ }
480
+ else {
481
+ res({ refreshToken: undefined, code_verifier: undefined });
482
+ }
483
+ });
484
+ window.addEventListener('message', windowEventHandler);
485
+ });
486
+ });
424
487
  _Authentication_exchangeAuthCodeForAccessToken.set(this, (params) => __awaiter(this, void 0, void 0, function* () {
425
488
  const { verifier, code, redirectUri } = params;
426
489
  if (verifier) {