/
application-token-manager.ts
212 lines (159 loc) · 6.4 KB
/
application-token-manager.ts
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
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/>.
*/
import {
assert,
} from "check-types";
import axios from "axios";
import ConnectionManager from "../../connection/connection-manager";
import PinoLogger from "../../util/pino-logger";
import IPollingConnection from "../polling/ipolling-connection";
import IRawToken from "./iraw-token";
export default class ApplicationTokenManager extends ConnectionManager<IRawToken> {
private waitForFirstTokenPromise: Promise<undefined>;
private tokenHasBeenSet: (() => void) | null;
private applicationAccessToken: string | null;
private rawOAuthToken: IRawToken | null;
private oauthTokenRevocationHeaders: {};
private oauthTokenRevocationMethod: string;
private oauthTokenRevocationUri: string;
private clientId: string;
constructor(
logger: PinoLogger,
connection: IPollingConnection<IRawToken>,
clientId: string,
oauthTokenRevocationUri: string,
) {
super(logger, connection);
assert.hasLength(arguments, 4);
assert.equal(typeof logger, "object");
assert.equal(typeof connection, "object");
assert.equal(typeof clientId, "string");
assert.greater(clientId.length, 0);
assert.equal(typeof oauthTokenRevocationUri, "string");
assert.greater(oauthTokenRevocationUri.length, 0);
assert(oauthTokenRevocationUri.startsWith("https://"));
this.logger = logger.child("ApplicationTokenManager");
this.clientId = clientId;
this.oauthTokenRevocationUri = oauthTokenRevocationUri;
this.oauthTokenRevocationMethod = "post";
this.oauthTokenRevocationHeaders = {};
// TODO: use rawOAuthToken for anything?
this.rawOAuthToken = null;
this.applicationAccessToken = null;
this.tokenHasBeenSet = null;
this.waitForFirstTokenPromise = new Promise((resolve, reject) => {
const waitForToken = () => {
resolve();
};
this.tokenHasBeenSet = waitForToken;
})
.then(() => {
this.logger.info("Received first token.", "waitForFirstTokenPromise");
return undefined;
});
}
public async start() {
assert.hasLength(arguments, 0);
await super.start();
const pollingConnection = this.connection as IPollingConnection<IRawToken>;
return pollingConnection.send(undefined);
}
public async stop() {
assert.hasLength(arguments, 0);
await this.revokeTokenIfSet(this.applicationAccessToken);
return super.stop();
}
public async revokeToken(token: string): Promise<void> {
assert.hasLength(arguments, 1);
assert.equal(typeof token, "string");
assert.greater(token.length, 0);
const data = {
client_id: this.clientId,
token,
};
this.logger.trace(data, "revokeToken");
await this.sendRevocation(data);
}
public async get(): Promise<string | null> {
assert.hasLength(arguments, 0);
return this.applicationAccessToken;
}
public async getOrWait(): Promise<string> {
assert.hasLength(arguments, 0);
await this.waitForFirstTokenPromise;
const token = await this.get();
return token!;
}
protected async dataHandler(data: IRawToken): Promise<void> {
assert.hasLength(arguments, 1);
assert.equal(typeof data, "object");
this.logger.trace(data, "dataHandler");
await Promise.all([
this.revokeTokenIfSet(this.applicationAccessToken),
this.setToken(data),
]);
}
protected async filter(data: IRawToken): Promise<boolean> {
assert.hasLength(arguments, 1);
assert.equal(typeof data, "object");
if (typeof data !== "object") {
return false;
}
if (typeof data.access_token !== "string") {
return false;
}
return true;
}
private async setToken(data: IRawToken) {
assert.hasLength(arguments, 1);
assert.equal(typeof data, "object");
this.rawOAuthToken = data;
this.applicationAccessToken = data.access_token;
await this.tokenHasBeenSet!();
return undefined;
}
private async sendRevocation(params: object): Promise<object> {
assert.hasLength(arguments, 1);
assert.equal(typeof params, "object");
// TODO: use TokenHelper.revoke().
const axiosInstanceConfig = {
baseURL: this.oauthTokenRevocationUri,
headers: this.oauthTokenRevocationHeaders,
// NOTE: per-instance method has no effect due to bug in axios, must use per request.
// TODO: remove per-call overrides once axios has been fixed.
// https://github.com/axios/axios/issues/723
method: this.oauthTokenRevocationMethod,
params,
};
const axiosInstance = axios.create(axiosInstanceConfig);
const response = await axiosInstance.request({
// NOTE: per-instance method has no effect due to bug in axios, must use per request.
// TODO: remove per-call overrides once axios has been fixed.
// https://github.com/axios/axios/issues/723
method: this.oauthTokenRevocationMethod,
});
// TODO: check if {status:"ok"};
const data = response.data;
return data;
}
private async revokeTokenIfSet(token: string | null): Promise<void> {
assert.hasLength(arguments, 1);
if (typeof token === "string") {
return this.revokeToken(token);
}
return undefined;
}
}