Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.

Commit 5fb1bd4

Browse files
markusdmarkus-doellinger
andauthored
Fix constant token renewal in the absence of a cookie Max-Age (#427)
Co-authored-by: markus-doellinger <[email protected]>
1 parent f812392 commit 5fb1bd4

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Unreleased
2+
- [FIXED] Stopped constant token auto renewal in the absence of a cookie
3+
header Max-Age
4+
15
# 4.2.4 (2020-03-02)
26
- [FIXED] Pinned Nano to version 8.1 to resolve issue with extending upstream
37
TypeScript changes in Nano version 8.2.0.

lib/tokens/TokenManager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class TokenManager {
3535
let maxAgeSecs = cookie.parse(response.headers['set-cookie'][0])['Max-Age'] || defaultMaxAgeSecs;
3636
let delayMSecs = maxAgeSecs / 2 * 1000;
3737
debug(`Renewing token in ${delayMSecs} milliseconds.`);
38-
setTimeout(this._autoRenew.bind(this), delayMSecs).unref();
38+
setTimeout(this._autoRenew.bind(this, defaultMaxAgeSecs), delayMSecs).unref();
3939
}).catch((error) => {
4040
debug(`Failed to auto renew token - ${error}. Retrying in 60 seconds.`);
4141
setTimeout(this._autoRenew.bind(this), 60000).unref();

test/tokens/TokenManager.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,28 @@ class TokenManagerRenewSuccess extends TokenManager {
2323
constructor() {
2424
super();
2525
this._getTokenCallCount = 0;
26+
this._cookieHeader = 'Max-Age=1';
2627
}
2728

2829
_getToken(callback) {
2930
this._getTokenCallCount += 1;
3031
setTimeout(() => {
31-
callback(null, { headers: { 'set-cookie': [ 'Max-Age=1' ] } });
32+
callback(null, { headers: { 'set-cookie': [ this._cookieHeader ] } });
3233
}, 100);
3334
}
3435

3536
// mock successful token renewal
3637
get getTokenCallCount() {
3738
return this._getTokenCallCount;
3839
}
40+
41+
get cookieHeader() {
42+
return this._cookieHeader;
43+
}
44+
45+
set cookieHeader(cookieHeader) {
46+
this._cookieHeader = cookieHeader;
47+
}
3948
}
4049

4150
class TokenManagerRenewFailure extends TokenManager {
@@ -90,6 +99,17 @@ describe('Token Manger', (done) => {
9099
}, 2000);
91100
});
92101

102+
it('correctly auto renews token in the absence of a cookie Max-Age', (done) => {
103+
let t = new TokenManagerRenewSuccess();
104+
t.cookieHeader = '';
105+
t.startAutoRenew(2);
106+
setTimeout(() => {
107+
// one renew every 1 seconds
108+
assert.equal(t.getTokenCallCount, 2);
109+
done();
110+
}, 2000);
111+
});
112+
93113
it('only makes one renewal request', (done) => {
94114
let t = new TokenManagerRenewSuccess();
95115
let renewalCount = 0;

0 commit comments

Comments
 (0)