@leapdev/auth-agent 2.5.5 → 2.6.1

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,34 @@ 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
+ ## setAutoLogout
289
+
290
+ Enable or Disable autoLogout
291
+
292
+ ### Parameters
293
+
294
+ - `enable` **[boolean][36]** enable or disable autoLogout
295
+
296
+ ## setIdleTimeoutInMinutes
297
+
298
+ In minutes, the time interval after which the user is automatically logged out if no activity detected. In order to have this value working, user have to enable "AutoLogout"
299
+
300
+ ### Parameters
301
+
302
+ - `minutes` **[number][36]** number of minute to allow application to be in Idle time (no activity)
303
+
276
304
  [1]: #init
277
305
 
278
306
  [2]: #parameters
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leapdev/auth-agent",
3
- "version": "2.5.5",
3
+ "version": "2.6.1",
4
4
  "description": "LEAP Auth Agent",
5
5
  "license": "LEAP Legal Software, PTY LTD",
6
6
  "main": "./src/index.js",
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
  },
@@ -6788,7 +6788,55 @@
6788
6788
  return list.find(appName => app.startsWith(`http://${appName}`) || app.startsWith(`https://${appName}`));
6789
6789
  };
6790
6790
 
6791
- var _Authentication_accessToken, _Authentication_config, _Authentication_leapAuthService, _Authentication_notification, _Authentication_refreshInfo, _Authentication_dedupeAccessTokenFetch, _Authentication_dedupeUserInfoFetch, _Authentication_exchangeAuthCodeForAccessToken, _Authentication_verifyAndPerformRedirections, _Authentication_startRefreshAccessTokenProcess, _Authentication_destroyRefreshAccessTokenProcess, _Authentication_decodeAccessToken, _Authentication_getRefreshedAccessTokenFactory, _Authentication_getUserInfoFactory;
6791
+ var _IdleTimer_timeoutInMinutes, _IdleTimer_timer, _IdleTimer_onTimeout, _IdleTimer_cleanUpTracker, _IdleTimer_clearTimeout, _IdleTimer_resetTimer;
6792
+ class IdleTimer {
6793
+ constructor(params) {
6794
+ _IdleTimer_timeoutInMinutes.set(this, void 0);
6795
+ _IdleTimer_timer.set(this, void 0);
6796
+ _IdleTimer_onTimeout.set(this, void 0);
6797
+ this.tracker = () => {
6798
+ window.addEventListener('onload', __classPrivateFieldGet(this, _IdleTimer_resetTimer, "f"), true);
6799
+ window.addEventListener('mousemove', __classPrivateFieldGet(this, _IdleTimer_resetTimer, "f"), true);
6800
+ window.addEventListener('onmousedown', __classPrivateFieldGet(this, _IdleTimer_resetTimer, "f"), true);
6801
+ window.addEventListener('onscroll', __classPrivateFieldGet(this, _IdleTimer_resetTimer, "f"), true);
6802
+ window.addEventListener('onkeypress', __classPrivateFieldGet(this, _IdleTimer_resetTimer, "f"), true);
6803
+ };
6804
+ this.resetTracker = () => {
6805
+ __classPrivateFieldGet(this, _IdleTimer_clearTimeout, "f").call(this);
6806
+ __classPrivateFieldSet(this, _IdleTimer_timer, undefined, "f");
6807
+ __classPrivateFieldGet(this, _IdleTimer_cleanUpTracker, "f").call(this);
6808
+ };
6809
+ _IdleTimer_cleanUpTracker.set(this, () => {
6810
+ window.removeEventListener('onload', __classPrivateFieldGet(this, _IdleTimer_resetTimer, "f"), true);
6811
+ window.removeEventListener('mousemove', __classPrivateFieldGet(this, _IdleTimer_resetTimer, "f"), true);
6812
+ window.removeEventListener('onmousedown', __classPrivateFieldGet(this, _IdleTimer_resetTimer, "f"), true);
6813
+ window.removeEventListener('onscroll', __classPrivateFieldGet(this, _IdleTimer_resetTimer, "f"), true);
6814
+ window.removeEventListener('onkeypress', __classPrivateFieldGet(this, _IdleTimer_resetTimer, "f"), true);
6815
+ });
6816
+ _IdleTimer_clearTimeout.set(this, () => {
6817
+ if (__classPrivateFieldGet(this, _IdleTimer_timer, "f")) {
6818
+ clearTimeout(__classPrivateFieldGet(this, _IdleTimer_timer, "f"));
6819
+ }
6820
+ });
6821
+ _IdleTimer_resetTimer.set(this, () => {
6822
+ __classPrivateFieldGet(this, _IdleTimer_clearTimeout, "f").call(this);
6823
+ __classPrivateFieldSet(this, _IdleTimer_timer, setTimeout(() => {
6824
+ __classPrivateFieldGet(this, _IdleTimer_clearTimeout, "f").call(this);
6825
+ __classPrivateFieldGet(this, _IdleTimer_cleanUpTracker, "f").call(this);
6826
+ __classPrivateFieldGet(this, _IdleTimer_onTimeout, "f").call(this);
6827
+ }, __classPrivateFieldGet(this, _IdleTimer_timeoutInMinutes, "f") * 60 * 1000), "f");
6828
+ });
6829
+ const {
6830
+ timeoutInMinutes,
6831
+ onTimeout: onTimeout
6832
+ } = params;
6833
+ __classPrivateFieldSet(this, _IdleTimer_timeoutInMinutes, timeoutInMinutes, "f");
6834
+ __classPrivateFieldSet(this, _IdleTimer_onTimeout, onTimeout, "f");
6835
+ }
6836
+ }
6837
+ _IdleTimer_timeoutInMinutes = new WeakMap(), _IdleTimer_timer = new WeakMap(), _IdleTimer_onTimeout = new WeakMap(), _IdleTimer_cleanUpTracker = new WeakMap(), _IdleTimer_clearTimeout = new WeakMap(), _IdleTimer_resetTimer = new WeakMap();
6838
+
6839
+ var _Authentication_accessToken, _Authentication_config, _Authentication_leapAuthService, _Authentication_notification, _Authentication_idleTimer, _Authentication_refreshInfo, _Authentication_dedupeAccessTokenFetch, _Authentication_dedupeUserInfoFetch, _Authentication_startIdleTimer, _Authentication_exchangeAuthCodeForAccessToken, _Authentication_verifyAndPerformRedirections, _Authentication_startRefreshAccessTokenProcess, _Authentication_destroyRefreshAccessTokenProcess, _Authentication_decodeAccessToken, _Authentication_getRefreshedAccessTokenFactory, _Authentication_getUserInfoFactory;
6792
6840
  const SECONDS_BEFORE_EXPIRE = 30;
6793
6841
  const MAX_SETTIME_OUT = 2147483647;
6794
6842
  const DEFAULT_AUTHORIZE_TIMEOUT_IN_SECONDS = 60;
@@ -6800,6 +6848,7 @@
6800
6848
  _Authentication_config.set(this, void 0);
6801
6849
  _Authentication_leapAuthService.set(this, void 0);
6802
6850
  _Authentication_notification.set(this, void 0);
6851
+ _Authentication_idleTimer.set(this, void 0);
6803
6852
  _Authentication_refreshInfo.set(this, void 0);
6804
6853
  _Authentication_dedupeAccessTokenFetch.set(this, void 0);
6805
6854
  _Authentication_dedupeUserInfoFetch.set(this, void 0);
@@ -7200,6 +7249,7 @@
7200
7249
  this.afterAuthenticated = () => {
7201
7250
  return this.triggerHooks(HookName.afterLogin).then(() => __awaiter(this, void 0, void 0, function* () {
7202
7251
  yield this.initNotification();
7252
+ __classPrivateFieldGet(this, _Authentication_startIdleTimer, "f").call(this);
7203
7253
  return __classPrivateFieldGet(this, _Authentication_accessToken, "f");
7204
7254
  }));
7205
7255
  };
@@ -7242,6 +7292,106 @@
7242
7292
  iframe.setAttribute('src', authorizeUrl);
7243
7293
  });
7244
7294
  };
7295
+ this.getRefreshTokenForApp = params => __awaiter(this, void 0, void 0, function* () {
7296
+ const {
7297
+ clientId,
7298
+ scopes
7299
+ } = params;
7300
+ const code_verifier = createRandomString(64);
7301
+ const state = createRandomString(6);
7302
+ const nonce = createRandomString(6);
7303
+ const {
7304
+ code_challenge,
7305
+ code_challenge_method
7306
+ } = yield createCodeChallenge(code_verifier);
7307
+ const requestScopesArray = scopes || [];
7308
+ requestScopesArray.push('offline_access');
7309
+ const request_scopes = requestScopesArray.join(',');
7310
+ const redirectUri = `${__classPrivateFieldGet(this, _Authentication_config, "f").authHost}/oauth/close`;
7311
+ 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`;
7312
+ return new Promise((res, rej) => {
7313
+ const newWindow = window.open(url, '_blank', 'width=800,height=600');
7314
+ if (!newWindow) {
7315
+ rej('unable to open new window');
7316
+ return;
7317
+ }
7318
+ const timeoutSetTimeoutId = setTimeout(() => {
7319
+ rej('get-refresh-token timeout');
7320
+ window.removeEventListener('message', windowEventHandler, false);
7321
+ newWindow.close();
7322
+ }, DEFAULT_AUTHORIZE_TIMEOUT_IN_SECONDS * 1000);
7323
+ const windowEventHandler = e => __awaiter(this, void 0, void 0, function* () {
7324
+ if (e.origin !== __classPrivateFieldGet(this, _Authentication_config, "f").authHost) return;
7325
+ if (e.data.type !== 'closeMe') return;
7326
+ newWindow.close();
7327
+ clearTimeout(timeoutSetTimeoutId);
7328
+ window.removeEventListener('message', windowEventHandler, false);
7329
+ if (e.data.queryParams) {
7330
+ const {
7331
+ code,
7332
+ state: stateFromCloseWindow
7333
+ } = e.data.queryParams;
7334
+ if (code && state && state === stateFromCloseWindow) {
7335
+ try {
7336
+ const result = yield __classPrivateFieldGet(this, _Authentication_leapAuthService, "f").exchangeAuthCodeForAccessToken({
7337
+ code,
7338
+ verifier: code_verifier,
7339
+ redirectUri: redirectUri,
7340
+ clientId: clientId
7341
+ });
7342
+ if (result && result.refresh_token) {
7343
+ res({
7344
+ refreshToken: result.refresh_token,
7345
+ code_verifier: code_verifier
7346
+ });
7347
+ } else {
7348
+ throw new Error('Failed to exchange auth code for access token');
7349
+ }
7350
+ } catch (_) {
7351
+ console.warn('Failed to exchange auth code for access token');
7352
+ res({
7353
+ refreshToken: undefined,
7354
+ code_verifier: undefined
7355
+ });
7356
+ }
7357
+ }
7358
+ } else {
7359
+ res({
7360
+ refreshToken: undefined,
7361
+ code_verifier: undefined
7362
+ });
7363
+ }
7364
+ });
7365
+ window.addEventListener('message', windowEventHandler);
7366
+ });
7367
+ });
7368
+ this.setIdleTimeoutInMinutes = timeoutInMinutes => {
7369
+ __classPrivateFieldGet(this, _Authentication_config, "f").idleTimeoutInMinutes = timeoutInMinutes;
7370
+ __classPrivateFieldGet(this, _Authentication_startIdleTimer, "f").call(this);
7371
+ };
7372
+ this.setAutoLogout = autoLogout => {
7373
+ __classPrivateFieldGet(this, _Authentication_config, "f").autoLogout = autoLogout;
7374
+ if (autoLogout) {
7375
+ __classPrivateFieldGet(this, _Authentication_startIdleTimer, "f").call(this);
7376
+ } else {
7377
+ if (__classPrivateFieldGet(this, _Authentication_idleTimer, "f")) {
7378
+ __classPrivateFieldGet(this, _Authentication_idleTimer, "f").resetTracker();
7379
+ }
7380
+ }
7381
+ };
7382
+ _Authentication_startIdleTimer.set(this, () => {
7383
+ if (this.autoLogout()) {
7384
+ if (__classPrivateFieldGet(this, _Authentication_idleTimer, "f")) {
7385
+ __classPrivateFieldGet(this, _Authentication_idleTimer, "f").resetTracker();
7386
+ }
7387
+ const timeoutInMinutes = this.idleTimeoutInMinutes();
7388
+ __classPrivateFieldSet(this, _Authentication_idleTimer, new IdleTimer({
7389
+ timeoutInMinutes,
7390
+ onTimeout: this.logout
7391
+ }), "f");
7392
+ __classPrivateFieldGet(this, _Authentication_idleTimer, "f").tracker();
7393
+ }
7394
+ });
7245
7395
  _Authentication_exchangeAuthCodeForAccessToken.set(this, params => __awaiter(this, void 0, void 0, function* () {
7246
7396
  const {
7247
7397
  verifier,
@@ -7385,50 +7535,7 @@
7385
7535
  __classPrivateFieldSet(this, _Authentication_dedupeUserInfoFetch, createInFlightDedupe(), "f");
7386
7536
  }
7387
7537
  }
7388
- _Authentication_accessToken = new WeakMap(), _Authentication_config = new WeakMap(), _Authentication_leapAuthService = new WeakMap(), _Authentication_notification = new WeakMap(), _Authentication_refreshInfo = new WeakMap(), _Authentication_dedupeAccessTokenFetch = new WeakMap(), _Authentication_dedupeUserInfoFetch = new WeakMap(), _Authentication_exchangeAuthCodeForAccessToken = new WeakMap(), _Authentication_verifyAndPerformRedirections = new WeakMap(), _Authentication_startRefreshAccessTokenProcess = new WeakMap(), _Authentication_destroyRefreshAccessTokenProcess = new WeakMap(), _Authentication_decodeAccessToken = new WeakMap(), _Authentication_getRefreshedAccessTokenFactory = new WeakMap(), _Authentication_getUserInfoFactory = new WeakMap();
7389
-
7390
- var _IdleTimer_timeoutInMinutes, _IdleTimer_timer, _IdleTimer_onTimeout, _IdleTimer_cleanUpTracker, _IdleTimer_clearTimeout, _IdleTimer_resetTimer;
7391
- class IdleTimer {
7392
- constructor(params) {
7393
- _IdleTimer_timeoutInMinutes.set(this, void 0);
7394
- _IdleTimer_timer.set(this, void 0);
7395
- _IdleTimer_onTimeout.set(this, void 0);
7396
- this.tracker = () => {
7397
- window.addEventListener('onload', __classPrivateFieldGet(this, _IdleTimer_resetTimer, "f"), true);
7398
- window.addEventListener('mousemove', __classPrivateFieldGet(this, _IdleTimer_resetTimer, "f"), true);
7399
- window.addEventListener('onmousedown', __classPrivateFieldGet(this, _IdleTimer_resetTimer, "f"), true);
7400
- window.addEventListener('onscroll', __classPrivateFieldGet(this, _IdleTimer_resetTimer, "f"), true);
7401
- window.addEventListener('onkeypress', __classPrivateFieldGet(this, _IdleTimer_resetTimer, "f"), true);
7402
- };
7403
- _IdleTimer_cleanUpTracker.set(this, () => {
7404
- window.removeEventListener('onload', __classPrivateFieldGet(this, _IdleTimer_resetTimer, "f"), true);
7405
- window.removeEventListener('mousemove', __classPrivateFieldGet(this, _IdleTimer_resetTimer, "f"), true);
7406
- window.removeEventListener('onmousedown', __classPrivateFieldGet(this, _IdleTimer_resetTimer, "f"), true);
7407
- window.removeEventListener('onscroll', __classPrivateFieldGet(this, _IdleTimer_resetTimer, "f"), true);
7408
- window.removeEventListener('onkeypress', __classPrivateFieldGet(this, _IdleTimer_resetTimer, "f"), true);
7409
- });
7410
- _IdleTimer_clearTimeout.set(this, () => {
7411
- if (__classPrivateFieldGet(this, _IdleTimer_timer, "f")) {
7412
- clearTimeout(__classPrivateFieldGet(this, _IdleTimer_timer, "f"));
7413
- }
7414
- });
7415
- _IdleTimer_resetTimer.set(this, () => {
7416
- __classPrivateFieldGet(this, _IdleTimer_clearTimeout, "f").call(this);
7417
- __classPrivateFieldSet(this, _IdleTimer_timer, setTimeout(() => {
7418
- __classPrivateFieldGet(this, _IdleTimer_clearTimeout, "f").call(this);
7419
- __classPrivateFieldGet(this, _IdleTimer_cleanUpTracker, "f").call(this);
7420
- __classPrivateFieldGet(this, _IdleTimer_onTimeout, "f").call(this);
7421
- }, __classPrivateFieldGet(this, _IdleTimer_timeoutInMinutes, "f") * 60 * 1000), "f");
7422
- });
7423
- const {
7424
- timeoutInMinutes,
7425
- onTimeout: onTimeout
7426
- } = params;
7427
- __classPrivateFieldSet(this, _IdleTimer_timeoutInMinutes, timeoutInMinutes, "f");
7428
- __classPrivateFieldSet(this, _IdleTimer_onTimeout, onTimeout, "f");
7429
- }
7430
- }
7431
- _IdleTimer_timeoutInMinutes = new WeakMap(), _IdleTimer_timer = new WeakMap(), _IdleTimer_onTimeout = new WeakMap(), _IdleTimer_cleanUpTracker = new WeakMap(), _IdleTimer_clearTimeout = new WeakMap(), _IdleTimer_resetTimer = new WeakMap();
7538
+ _Authentication_accessToken = new WeakMap(), _Authentication_config = new WeakMap(), _Authentication_leapAuthService = new WeakMap(), _Authentication_notification = new WeakMap(), _Authentication_idleTimer = new WeakMap(), _Authentication_refreshInfo = new WeakMap(), _Authentication_dedupeAccessTokenFetch = new WeakMap(), _Authentication_dedupeUserInfoFetch = new WeakMap(), _Authentication_startIdleTimer = new WeakMap(), _Authentication_exchangeAuthCodeForAccessToken = new WeakMap(), _Authentication_verifyAndPerformRedirections = new WeakMap(), _Authentication_startRefreshAccessTokenProcess = new WeakMap(), _Authentication_destroyRefreshAccessTokenProcess = new WeakMap(), _Authentication_decodeAccessToken = new WeakMap(), _Authentication_getRefreshedAccessTokenFactory = new WeakMap(), _Authentication_getUserInfoFactory = new WeakMap();
7432
7539
 
7433
7540
  let auth;
7434
7541
  const init = options => __awaiter(void 0, void 0, void 0, function* () {
@@ -7436,15 +7543,6 @@
7436
7543
  if (!auth) {
7437
7544
  return false;
7438
7545
  }
7439
- const autoLogout = auth.autoLogout();
7440
- if (autoLogout) {
7441
- const timeoutInMinutes = auth.idleTimeoutInMinutes();
7442
- const idleTimer = new IdleTimer({
7443
- timeoutInMinutes,
7444
- onTimeout: logout
7445
- });
7446
- idleTimer.tracker();
7447
- }
7448
7546
  const done = yield auth.checkAuthCode();
7449
7547
  if (done) {
7450
7548
  return auth.afterAuthenticated();
@@ -7660,6 +7758,36 @@
7660
7758
  authHost
7661
7759
  });
7662
7760
  });
7761
+ const getRefreshTokenForApp = params => __awaiter(void 0, void 0, void 0, function* () {
7762
+ const {
7763
+ clientId,
7764
+ scopes
7765
+ } = params;
7766
+ if (!auth) {
7767
+ throw Error('Not init yet');
7768
+ }
7769
+ return auth.getRefreshTokenForApp({
7770
+ clientId,
7771
+ scopes
7772
+ });
7773
+ });
7774
+ const setIdleTimeoutInMinutes = timeoutInMinutes => {
7775
+ if (!auth) {
7776
+ throw Error('Not init yet');
7777
+ }
7778
+ if (typeof timeoutInMinutes !== "number") {
7779
+ throw Error('timeoutInMinutes must be a number');
7780
+ }
7781
+ auth.setIdleTimeoutInMinutes(timeoutInMinutes);
7782
+ return true;
7783
+ };
7784
+ const setAutoLogout = autoLogout => {
7785
+ if (!auth) {
7786
+ throw Error('Not init yet');
7787
+ }
7788
+ auth.setAutoLogout(autoLogout);
7789
+ return true;
7790
+ };
7663
7791
  const AuthAgent = {
7664
7792
  init,
7665
7793
  registerHook,
@@ -7684,7 +7812,10 @@
7684
7812
  authoriseSupport,
7685
7813
  changePassword,
7686
7814
  registerEventListener,
7687
- passthrough
7815
+ passthrough,
7816
+ getRefreshTokenForApp,
7817
+ setIdleTimeoutInMinutes,
7818
+ setAutoLogout
7688
7819
  };
7689
7820
 
7690
7821
  exports.AuthAgent = AuthAgent;
@@ -26,4 +26,13 @@ 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
+ }>;
36
+ setIdleTimeoutInMinutes: (timeoutInMinutes: number) => boolean;
37
+ setAutoLogout: (autoLogout: boolean) => boolean;
29
38
  };
@@ -1,6 +1,5 @@
1
1
  import { __awaiter } from "tslib";
2
2
  import { Authentication } from './authentication';
3
- import { IdleTimer } from './idle-timer';
4
3
  import { HookName } from './types';
5
4
  let auth;
6
5
  const init = (options) => __awaiter(void 0, void 0, void 0, function* () {
@@ -8,12 +7,6 @@ const init = (options) => __awaiter(void 0, void 0, void 0, function* () {
8
7
  if (!auth) {
9
8
  return false;
10
9
  }
11
- const autoLogout = auth.autoLogout();
12
- if (autoLogout) {
13
- const timeoutInMinutes = auth.idleTimeoutInMinutes();
14
- const idleTimer = new IdleTimer({ timeoutInMinutes, onTimeout: logout });
15
- idleTimer.tracker();
16
- }
17
10
  const done = yield auth.checkAuthCode();
18
11
  if (done) {
19
12
  return auth.afterAuthenticated();
@@ -195,6 +188,30 @@ const passthrough = (url, newWindow = false, authHost) => __awaiter(void 0, void
195
188
  }
196
189
  yield auth.passthrough({ url, newWindow, authHost });
197
190
  });
191
+ const getRefreshTokenForApp = (params) => __awaiter(void 0, void 0, void 0, function* () {
192
+ const { clientId, scopes } = params;
193
+ if (!auth) {
194
+ throw Error('Not init yet');
195
+ }
196
+ return auth.getRefreshTokenForApp({ clientId, scopes });
197
+ });
198
+ const setIdleTimeoutInMinutes = (timeoutInMinutes) => {
199
+ if (!auth) {
200
+ throw Error('Not init yet');
201
+ }
202
+ if (typeof timeoutInMinutes !== "number") {
203
+ throw Error('timeoutInMinutes must be a number');
204
+ }
205
+ auth.setIdleTimeoutInMinutes(timeoutInMinutes);
206
+ return true;
207
+ };
208
+ const setAutoLogout = (autoLogout) => {
209
+ if (!auth) {
210
+ throw Error('Not init yet');
211
+ }
212
+ auth.setAutoLogout(autoLogout);
213
+ return true;
214
+ };
198
215
  export const AuthAgent = {
199
216
  init,
200
217
  registerHook,
@@ -220,4 +237,7 @@ export const AuthAgent = {
220
237
  changePassword,
221
238
  registerEventListener,
222
239
  passthrough,
240
+ getRefreshTokenForApp,
241
+ setIdleTimeoutInMinutes,
242
+ setAutoLogout,
223
243
  };
@@ -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,13 @@ 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
+ }>;
87
+ setIdleTimeoutInMinutes: (timeoutInMinutes: number) => void;
88
+ setAutoLogout: (autoLogout: boolean) => void;
80
89
  }
@@ -1,4 +1,4 @@
1
- var _Authentication_accessToken, _Authentication_config, _Authentication_leapAuthService, _Authentication_notification, _Authentication_refreshInfo, _Authentication_dedupeAccessTokenFetch, _Authentication_dedupeUserInfoFetch, _Authentication_exchangeAuthCodeForAccessToken, _Authentication_verifyAndPerformRedirections, _Authentication_startRefreshAccessTokenProcess, _Authentication_destroyRefreshAccessTokenProcess, _Authentication_decodeAccessToken, _Authentication_getRefreshedAccessTokenFactory, _Authentication_getUserInfoFactory;
1
+ var _Authentication_accessToken, _Authentication_config, _Authentication_leapAuthService, _Authentication_notification, _Authentication_idleTimer, _Authentication_refreshInfo, _Authentication_dedupeAccessTokenFetch, _Authentication_dedupeUserInfoFetch, _Authentication_startIdleTimer, _Authentication_exchangeAuthCodeForAccessToken, _Authentication_verifyAndPerformRedirections, _Authentication_startRefreshAccessTokenProcess, _Authentication_destroyRefreshAccessTokenProcess, _Authentication_decodeAccessToken, _Authentication_getRefreshedAccessTokenFactory, _Authentication_getUserInfoFactory;
2
2
  import { __awaiter, __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
3
3
  import { isFunction } from 'lodash';
4
4
  import { init } from './config';
@@ -7,6 +7,7 @@ import { HookName, } from './types';
7
7
  import { Notification } from './notification';
8
8
  import { createCodeChallenge, createInFlightDedupe, createLoginUrl, createRandomString, deleteQueryParameter, getQueryParameter } from './utils';
9
9
  import { getRedirectUri } from './redirections';
10
+ import { IdleTimer } from './idle-timer';
10
11
  const SECONDS_BEFORE_EXPIRE = 30;
11
12
  const MAX_SETTIME_OUT = 2147483647;
12
13
  const DEFAULT_AUTHORIZE_TIMEOUT_IN_SECONDS = 60;
@@ -18,6 +19,7 @@ export class Authentication {
18
19
  _Authentication_config.set(this, void 0);
19
20
  _Authentication_leapAuthService.set(this, void 0);
20
21
  _Authentication_notification.set(this, void 0);
22
+ _Authentication_idleTimer.set(this, void 0);
21
23
  _Authentication_refreshInfo.set(this, void 0);
22
24
  _Authentication_dedupeAccessTokenFetch.set(this, void 0);
23
25
  _Authentication_dedupeUserInfoFetch.set(this, void 0);
@@ -381,6 +383,7 @@ export class Authentication {
381
383
  this.afterAuthenticated = () => {
382
384
  return this.triggerHooks(HookName.afterLogin).then(() => __awaiter(this, void 0, void 0, function* () {
383
385
  yield this.initNotification();
386
+ __classPrivateFieldGet(this, _Authentication_startIdleTimer, "f").call(this);
384
387
  return __classPrivateFieldGet(this, _Authentication_accessToken, "f");
385
388
  }));
386
389
  };
@@ -421,6 +424,94 @@ export class Authentication {
421
424
  iframe.setAttribute('src', authorizeUrl);
422
425
  });
423
426
  };
427
+ this.getRefreshTokenForApp = (params) => __awaiter(this, void 0, void 0, function* () {
428
+ const { clientId, scopes } = params;
429
+ const code_verifier = createRandomString(64);
430
+ const state = createRandomString(6);
431
+ const nonce = createRandomString(6);
432
+ const { code_challenge, code_challenge_method } = yield createCodeChallenge(code_verifier);
433
+ const requestScopesArray = scopes || [];
434
+ requestScopesArray.push('offline_access');
435
+ const request_scopes = requestScopesArray.join(',');
436
+ const redirectUri = `${__classPrivateFieldGet(this, _Authentication_config, "f").authHost}/oauth/close`;
437
+ 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`;
438
+ return new Promise((res, rej) => {
439
+ const newWindow = window.open(url, '_blank', 'width=800,height=600');
440
+ if (!newWindow) {
441
+ rej('unable to open new window');
442
+ return;
443
+ }
444
+ const timeoutSetTimeoutId = setTimeout(() => {
445
+ rej('get-refresh-token timeout');
446
+ window.removeEventListener('message', windowEventHandler, false);
447
+ newWindow.close();
448
+ }, DEFAULT_AUTHORIZE_TIMEOUT_IN_SECONDS * 1000);
449
+ const windowEventHandler = (e) => __awaiter(this, void 0, void 0, function* () {
450
+ if (e.origin !== __classPrivateFieldGet(this, _Authentication_config, "f").authHost)
451
+ return;
452
+ if (e.data.type !== 'closeMe')
453
+ return;
454
+ newWindow.close();
455
+ clearTimeout(timeoutSetTimeoutId);
456
+ window.removeEventListener('message', windowEventHandler, false);
457
+ if (e.data.queryParams) {
458
+ const { code, state: stateFromCloseWindow } = e.data.queryParams;
459
+ if (code && state && state === stateFromCloseWindow) {
460
+ try {
461
+ const result = yield __classPrivateFieldGet(this, _Authentication_leapAuthService, "f").exchangeAuthCodeForAccessToken({
462
+ code,
463
+ verifier: code_verifier,
464
+ redirectUri: redirectUri,
465
+ clientId: clientId,
466
+ });
467
+ if (result && result.refresh_token) {
468
+ res({
469
+ refreshToken: result.refresh_token,
470
+ code_verifier: code_verifier,
471
+ });
472
+ }
473
+ else {
474
+ throw new Error('Failed to exchange auth code for access token');
475
+ }
476
+ }
477
+ catch (_) {
478
+ console.warn('Failed to exchange auth code for access token');
479
+ res({ refreshToken: undefined, code_verifier: undefined });
480
+ }
481
+ }
482
+ }
483
+ else {
484
+ res({ refreshToken: undefined, code_verifier: undefined });
485
+ }
486
+ });
487
+ window.addEventListener('message', windowEventHandler);
488
+ });
489
+ });
490
+ this.setIdleTimeoutInMinutes = (timeoutInMinutes) => {
491
+ __classPrivateFieldGet(this, _Authentication_config, "f").idleTimeoutInMinutes = timeoutInMinutes;
492
+ __classPrivateFieldGet(this, _Authentication_startIdleTimer, "f").call(this);
493
+ };
494
+ this.setAutoLogout = (autoLogout) => {
495
+ __classPrivateFieldGet(this, _Authentication_config, "f").autoLogout = autoLogout;
496
+ if (autoLogout) {
497
+ __classPrivateFieldGet(this, _Authentication_startIdleTimer, "f").call(this);
498
+ }
499
+ else {
500
+ if (__classPrivateFieldGet(this, _Authentication_idleTimer, "f")) {
501
+ __classPrivateFieldGet(this, _Authentication_idleTimer, "f").resetTracker();
502
+ }
503
+ }
504
+ };
505
+ _Authentication_startIdleTimer.set(this, () => {
506
+ if (this.autoLogout()) {
507
+ if (__classPrivateFieldGet(this, _Authentication_idleTimer, "f")) {
508
+ __classPrivateFieldGet(this, _Authentication_idleTimer, "f").resetTracker();
509
+ }
510
+ const timeoutInMinutes = this.idleTimeoutInMinutes();
511
+ __classPrivateFieldSet(this, _Authentication_idleTimer, new IdleTimer({ timeoutInMinutes, onTimeout: this.logout }), "f");
512
+ __classPrivateFieldGet(this, _Authentication_idleTimer, "f").tracker();
513
+ }
514
+ });
424
515
  _Authentication_exchangeAuthCodeForAccessToken.set(this, (params) => __awaiter(this, void 0, void 0, function* () {
425
516
  const { verifier, code, redirectUri } = params;
426
517
  if (verifier) {
@@ -553,4 +644,4 @@ export class Authentication {
553
644
  __classPrivateFieldSet(this, _Authentication_dedupeUserInfoFetch, createInFlightDedupe(), "f");
554
645
  }
555
646
  }
556
- _Authentication_accessToken = new WeakMap(), _Authentication_config = new WeakMap(), _Authentication_leapAuthService = new WeakMap(), _Authentication_notification = new WeakMap(), _Authentication_refreshInfo = new WeakMap(), _Authentication_dedupeAccessTokenFetch = new WeakMap(), _Authentication_dedupeUserInfoFetch = new WeakMap(), _Authentication_exchangeAuthCodeForAccessToken = new WeakMap(), _Authentication_verifyAndPerformRedirections = new WeakMap(), _Authentication_startRefreshAccessTokenProcess = new WeakMap(), _Authentication_destroyRefreshAccessTokenProcess = new WeakMap(), _Authentication_decodeAccessToken = new WeakMap(), _Authentication_getRefreshedAccessTokenFactory = new WeakMap(), _Authentication_getUserInfoFactory = new WeakMap();
647
+ _Authentication_accessToken = new WeakMap(), _Authentication_config = new WeakMap(), _Authentication_leapAuthService = new WeakMap(), _Authentication_notification = new WeakMap(), _Authentication_idleTimer = new WeakMap(), _Authentication_refreshInfo = new WeakMap(), _Authentication_dedupeAccessTokenFetch = new WeakMap(), _Authentication_dedupeUserInfoFetch = new WeakMap(), _Authentication_startIdleTimer = new WeakMap(), _Authentication_exchangeAuthCodeForAccessToken = new WeakMap(), _Authentication_verifyAndPerformRedirections = new WeakMap(), _Authentication_startRefreshAccessTokenProcess = new WeakMap(), _Authentication_destroyRefreshAccessTokenProcess = new WeakMap(), _Authentication_decodeAccessToken = new WeakMap(), _Authentication_getRefreshedAccessTokenFactory = new WeakMap(), _Authentication_getUserInfoFactory = new WeakMap();
@@ -5,4 +5,5 @@ export declare class IdleTimer {
5
5
  onTimeout: () => void;
6
6
  });
7
7
  tracker: () => void;
8
+ resetTracker: () => void;
8
9
  }
@@ -12,6 +12,11 @@ export class IdleTimer {
12
12
  window.addEventListener('onscroll', __classPrivateFieldGet(this, _IdleTimer_resetTimer, "f"), true);
13
13
  window.addEventListener('onkeypress', __classPrivateFieldGet(this, _IdleTimer_resetTimer, "f"), true);
14
14
  };
15
+ this.resetTracker = () => {
16
+ __classPrivateFieldGet(this, _IdleTimer_clearTimeout, "f").call(this);
17
+ __classPrivateFieldSet(this, _IdleTimer_timer, undefined, "f");
18
+ __classPrivateFieldGet(this, _IdleTimer_cleanUpTracker, "f").call(this);
19
+ };
15
20
  _IdleTimer_cleanUpTracker.set(this, () => {
16
21
  window.removeEventListener('onload', __classPrivateFieldGet(this, _IdleTimer_resetTimer, "f"), true);
17
22
  window.removeEventListener('mousemove', __classPrivateFieldGet(this, _IdleTimer_resetTimer, "f"), true);