Skip to content

Commit e55333a

Browse files
committed
add APIs to start/stop silent renew DuendeArchive#325
1 parent 6abed7f commit e55333a

File tree

5 files changed

+52
-16
lines changed

5 files changed

+52
-16
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const CordovaIFrameNavigator = require('./src/CordovaIFrameNavigator');
1414
export const CheckSessionIFrame = require('./src/CheckSessionIFrame');
1515
export const TokenRevocationClient = require('./src/TokenRevocationClient');
1616
export const SessionMonitor = require('./src/SessionMonitor');
17-
export const Global = require('./src/Global');
17+
export const Global = require('./src/Global');
1818

1919
export default {
2020
Log,

src/SilentRenewService.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,30 @@ export default class SilentRenewService {
77

88
constructor(userManager) {
99
this._userManager = userManager;
10-
this._userManager.events.addAccessTokenExpiring(this._tokenExpiring.bind(this));
10+
}
1111

12-
// this will trigger loading of the user so the expiring events can be initialized
13-
this._userManager.getUser().then(user=>{
14-
// deliberate nop
15-
}).catch(err=>{
16-
// catch to suppress errors since we're in a ctor
17-
Log.error("Error from getUser:", err.message);
18-
});
12+
start() {
13+
if (!this._callback) {
14+
this._callback = this._tokenExpiring.bind(this);
15+
this._userManager.events.addAccessTokenExpiring(this._callback);
16+
17+
// this will trigger loading of the user so the expiring events can be initialized
18+
this._userManager.getUser().then(user=>{
19+
// deliberate nop
20+
}).catch(err=>{
21+
// catch to suppress errors since we're in a ctor
22+
Log.error("Error from getUser:", err.message);
23+
});
24+
}
1925
}
20-
26+
27+
stop() {
28+
if (this._callback) {
29+
this._userManager.events.removeAccessTokenExpiring(this._callback);
30+
delete this._callback;
31+
}
32+
}
33+
2134
_tokenExpiring() {
2235
Log.debug("SilentRenewService automatically renewing access token");
2336

src/UserManager.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@ export default class UserManager extends OidcClient {
2323
super(settings);
2424

2525
this._events = new UserManagerEvents(settings);
26-
26+
this._silentRenewService = new SilentRenewServiceCtor(this);
27+
2728
// order is important for the following properties; these services depend upon the events.
2829
if (this.settings.automaticSilentRenew) {
29-
Log.debug("automaticSilentRenew is configured, setting up silent renew")
30-
this._silentRenewService = new SilentRenewServiceCtor(this);
30+
Log.debug("automaticSilentRenew is configured, setting up silent renew");
31+
this.startSilentRenew();
3132
}
33+
3234
if (this.settings.monitorSession) {
33-
Log.debug("monitorSession is configured, setting up session monitor")
35+
Log.debug("monitorSession is configured, setting up session monitor");
3436
this._sessionMonitor = new SessionMonitorCtor(this);
3537
}
3638

@@ -429,7 +431,15 @@ export default class UserManager extends OidcClient {
429431

430432
return this._tokenRevocationClient.revoke(access_token, required).then(() => true);
431433
}
432-
434+
435+
startSilentRenew() {
436+
this._silentRenewService.start();
437+
}
438+
439+
stopSilentRenew() {
440+
this._silentRenewService.stop();
441+
}
442+
433443
get _userStoreKey() {
434444
return `user:${this.settings.authority}:${this.settings.client_id}`;
435445
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
export default class StubMetadataService{
5+
start(){
6+
}
7+
stop(){
8+
}
9+
}

test/unit/UserManager.spec.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import UserManagerSettings from '../../src/UserManagerSettings';
88
import User from '../../src/User';
99

1010
import StubMetadataService from './StubMetadataService';
11+
import StubSilentRenewService from './StubSilentRenewService';
1112
import StubStateStore from './StubStateStore';
1213
import StubResponseValidator from './StubResponseValidator';
1314
import StubTokenRevocationClient from './StubTokenRevocationClient';
@@ -22,6 +23,7 @@ describe("UserManager", function () {
2223
let stubMetadataService;
2324
let stubStateStore;
2425
let stubValidator;
26+
let stubSilentRenewService;
2527
let stubNavigator;
2628
let stubUserStore;
2729
let stubTokenRevocationClient;
@@ -37,6 +39,7 @@ describe("UserManager", function () {
3739
stubUserStore = new StubStateStore();
3840
stubStateStore = new StubStateStore();
3941
stubValidator = new StubResponseValidator();
42+
stubSilentRenewService = new StubSilentRenewService();
4043
stubMetadataService = new StubMetadataService();
4144
stubTokenRevocationClient = new StubTokenRevocationClient();
4245

@@ -52,7 +55,8 @@ describe("UserManager", function () {
5255
};
5356

5457
subject = new UserManager(settings,
55-
null, null,
58+
() => stubSilentRenewService,
59+
null,
5660
() => stubTokenRevocationClient);
5761
});
5862

0 commit comments

Comments
 (0)