/
user-token-manager.js
90 lines (75 loc) · 3.42 KB
/
user-token-manager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
This file is part of botten-nappet -- a Twitch bot and streaming tool.
<https://joelpurra.com/projects/botten-nappet/>
Copyright (c) 2018 Joel Purra <https://joelpurra.com/>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const assert = require("power-assert");
const Promise = require("bluebird");
const RefreshToken = require("refresh-token");
const deepStrictEqual = require("deep-strict-equal");
export default class UserTokenManager {
constructor(logger, userOAuthTokenUri, revocationUri, clientId, clientSecret) {
assert.strictEqual(arguments.length, 5);
assert.strictEqual(typeof logger, "object");
assert.strictEqual(typeof userOAuthTokenUri, "string");
assert(userOAuthTokenUri.length > 0);
assert(userOAuthTokenUri.startsWith("https://"));
assert.strictEqual(typeof revocationUri, "string");
assert(revocationUri.length > 0);
assert(revocationUri.startsWith("https://"));
assert.strictEqual(typeof clientId, "string");
assert(clientId.length > 0);
assert.strictEqual(typeof clientSecret, "string");
assert(clientSecret.length > 0);
this._logger = logger.child("UserTokenManager");
this._userOAuthTokenUri = userOAuthTokenUri;
this._revocationUri = revocationUri;
this._clientId = clientId;
this._clientSecret = clientSecret;
}
get(tokenToRefresh) {
assert.strictEqual(arguments.length, 1);
assert.notStrictEqual(tokenToRefresh, null);
assert.strictEqual(typeof tokenToRefresh, "object");
return Promise.try(() => {
const refreshTokenWithClientIdAndSecret = {
access_token: tokenToRefresh.access_token,
refresh_token: tokenToRefresh.refresh_token,
expires_in: tokenToRefresh.expires_in,
client_id: this._clientId,
client_secret: this._clientSecret,
};
const refreshToken = new RefreshToken(
this._userOAuthTokenUri,
refreshTokenWithClientIdAndSecret
);
const getToken = Promise.promisify(refreshToken.getToken, {
context: refreshToken,
});
return getToken();
})
.then((refreshedAccessToken) => {
const refreshedToken = {
access_token: refreshedAccessToken,
refresh_token: tokenToRefresh.refresh_token,
expires_in: tokenToRefresh.expires_in,
scope: tokenToRefresh.scope,
};
return refreshedToken;
})
.tap((refreshedToken) => {
const wasUpdated = deepStrictEqual(refreshedToken, tokenToRefresh);
this._logger.trace(refreshedToken, tokenToRefresh, wasUpdated, "get");
});
}
}