| 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360 |
1x
1x
1x
1x
1x
1x
1x
2x
2x
2x
1x
1x
1x
1x
1x
2x
1x
| import * as express from 'express';
import * as jsonwebtoken from 'jsonwebtoken';
import { Client } from './models/client';
import { OAuth2FrameworkError } from './models/oauth2-error';
import { Token } from './models/token';
export { Client } from './models/client';
export { Token } from './models/token';
export { OAuth2FrameworkRouter } from './router';
export { OAuth2FrameworkError } from './models/oauth2-error';
export class OAuth2Framework {
constructor(public model: {
findClient(client_id: string, request: express.Request): Promise<Client>,
generateAccessToken(client_id: string, username: string, scopes: string[], request: express.Request): Promise<string>,
generateCode(client_id: string, username: string, scopes: string[], request: express.Request): Promise<string>,
register(client_id: string, emailAddress: string, username: string, password: string, request: express.Request): Promise<boolean>,
resetPassword(client_id: string, username: string, password: string, request: express.Request): Promise<boolean>,
sendForgotPasswordEmail(client_id: string, username: string, resetPasswordUrl: string, request: express.Request): Promise<boolean>,
sendVerificationEmail(client_id: string, emailAddress: string, username: string, verificationUrl: string, request: express.Request): Promise<boolean>,
validateAccessToken(access_token: string, request: express.Request): Promise<Token>,
validateCode(code: string, request: express.Request): Promise<Token>,
validateCredentials(client_id: string, username: string, password: string, request: express.Request): Promise<boolean>,
verify(client_id: string, username: string, request: express.Request): Promise<boolean>,
}, public secret: string,
) {
}
public async accessTokenRequest(
grant_type: string,
code: string,
redirect_uri: string,
client_id: string,
client_secret: string,
username: string,
password: string,
scopes: string[],
request: express.Request): Promise<string> {
this.throwIfInvalidGrantType(grant_type);
const client: Client = await this.findClientAndValidate(client_id, redirect_uri, scopes, request);
if (grant_type === 'password') {
const validCredentials: boolean = await this.model.validateCredentials(
client_id,
username,
password,
request);
this.throwIfInvalidCredentials(validCredentials);
return this.model.generateAccessToken(client_id, username, scopes, request);
} else if (grant_type === 'authorization_code') {
this.throwIfClientDoesNotMatchClientSecret(client, client_secret);
const token: Token = await this.model.validateCode(code, request);
return this.model.generateAccessToken(
token.client_id,
token.username,
token.scopes,
request);
}
}
public async authorizationRequest(
response_type: string,
client_id: string,
redirect_uri: string,
scopes: string[],
state: string,
username: string,
password: string,
request: express.Request): Promise<string> {
this.throwIfInvalidResponseType(response_type);
const client: Client = await this.findClientAndValidate(client_id, redirect_uri, scopes, request);
const validCredentials: boolean = await this.model.validateCredentials(client_id, username, password, request);
this.throwIfInvalidCredentials(validCredentials);
switch (response_type) {
case 'code':
return this.model.generateCode(client_id, username, scopes, request);
case 'token':
return this.model.generateAccessToken(client_id, username, scopes, request);
}
}
public async validateAccessToken(access_token: string, request: express.Request): Promise<boolean> {
const token: Token = await this.model.validateAccessToken(access_token, request);
if (!token) {
return false;
}
return true;
}
public async decodeAccessToken(access_token: string, request: express.Request): Promise<Token> {
const token: Token = await this.model.validateAccessToken(access_token, request);
if (!token) {
return null;
}
return token;
}
public async forgotPasswordRequest(
client_id: string,
username: string,
response_type: string,
redirect_uri: string,
state: string,
request: express.Request): Promise<boolean> {
const client: Client = await this.model.findClient(client_id, request);
this.throwIfClientNull(client);
if (!client.allowForgotPassword) {
throw new OAuth2FrameworkError('forgot_password_not_enabled', 'The forgot password functionality is not enabled for this client.');
}
const returnUrl = `authorize?response_type=${response_type}&client_id=${client_id}&redirect_uri=${redirect_uri}&state=${state}`;
const resetPasswordToken = this.generateResetPasswordToken(client_id, username, returnUrl);
const resetPasswordUrl = `/reset-password?token=${resetPasswordToken}`;
const result = await this.model.sendForgotPasswordEmail(
client_id,
username,
resetPasswordUrl,
request);
return result;
}
public async emailVerificationRequest(token: string, request: express.Request): Promise<boolean> {
const decodedToken: any = await this.decodeEmailVerificationToken(token);
if (!decodedToken) {
throw new OAuth2FrameworkError('invalid_token', 'Invalid token');
}
const client: Client = await this.model.findClient(decodedToken.client_id, request);
this.throwIfClientNull(client);
if (!client.allowRegister) {
throw new OAuth2FrameworkError('register_not_enabled', 'The register functionality is not enabled for this client.');
}
const result = await this.model.verify(
decodedToken.client_id,
decodedToken.username,
request);
return result;
}
public async registerRequest(
client_id: string,
emailAddress: string,
username: string,
password: string,
response_type: string,
redirect_uri: string,
state: string,
request: express.Request): Promise<boolean> {
const client: Client = await this.model.findClient(client_id, request);
this.throwIfClientNull(client);
if (!client.allowRegister) {
throw new OAuth2FrameworkError('register_not_enabled', 'The register functionality is not enabled for this client.');
}
const returnUrl = `authorize?response_type=${response_type}&client_id=${client_id}&redirect_uri=${redirect_uri}&state=${state}`;
const emailVerificationToken = this.generateEmailVerificationToken(
client_id,
username,
returnUrl);
const emailVerificationUrl = `/email-verification?token=${emailVerificationToken}`;
const result = await this.model.register(client_id, emailAddress, username, password, request);
if (result) {
const emailResult = await this.model.sendVerificationEmail(
client_id, emailAddress,
username,
emailVerificationUrl,
request);
}
return result;
}
public async resetPasswordRequest(token: string,
password: string,
request: express.Request): Promise<boolean> {
const decodedToken: any = await this.decodeResetPasswordToken(token);
if (!decodedToken) {
throw new OAuth2FrameworkError('invalid_token', 'Invalid token');
}
const client: Client = await this.model.findClient(decodedToken.client_id, request);
this.throwIfClientNull(client);
if (!client.allowForgotPassword) {
throw new OAuth2FrameworkError('forgot_password_not_enabled', 'The forgot password functionality is not enabled for this client.');
}
const result = await this.model.resetPassword(
decodedToken.client_id,
decodedToken.username,
password,
request);
return result;
}
public async decodeResetPasswordToken(token: string): Promise<any> {
const decodedToken: any = await this.decodeJWT(token);
if (!decodedToken) {
return null;
}
if (decodedToken.type !== 'reset-password') {
return null;
}
return decodedToken;
}
public async decodeEmailVerificationToken(token: string): Promise<any> {
const decodedToken: any = await this.decodeJWT(token);
if (!decodedToken) {
return null;
}
if (decodedToken.type !== 'email-verification') {
return null;
}
return decodedToken;
}
private decodeJWT(jwt: string): Promise<string> {
return new Promise((resolve, reject) => {
jsonwebtoken.verify(jwt, this.secret, (err: Error, decodedCode: any) => {
if (err) {
resolve(null);
return;
}
resolve(decodedCode);
});
});
}
private async findClientAndValidate(client_id: string, redirect_uri: string, scopes: string[], request: express.Request): Promise<Client> {
const client: Client = await this.model.findClient(client_id, request);
this.throwIfClientNull(client);
this.throwIfClientDoesNotContainUri(client, redirect_uri);
this.throwIfClientDoesNotContainScope(client, scopes);
return client;
}
private generateEmailVerificationToken(
client_id: string,
username: string,
return_url: string): string {
return jsonwebtoken.sign({
client_id,
return_url,
type: 'email-verification',
username,
}, this.secret, {
expiresIn: '60m',
});
}
private generateResetPasswordToken(
client_id: string,
username: string,
return_url: string): string {
return jsonwebtoken.sign({
client_id,
return_url,
type: 'reset-password',
username,
}, this.secret, {
expiresIn: '60m',
});
}
private throwIfClientDoesNotContainScope(client: Client, scopes: string[]): void {
if (scopes.length !== 0 && scopes.filter((x) => client.allowedScopes.indexOf(x) === -1).length !== 0) {
throw new OAuth2FrameworkError('invalid_scopes', 'Invalid scopes');
}
}
private throwIfClientDoesNotContainUri(client: Client, uri: string): void {
if (client.redirectUris.indexOf(uri) === -1) {
throw new OAuth2FrameworkError('invalid_redirect_uri', 'Invalid redirect uri');
}
}
private throwIfClientDoesNotMatchClientSecret(client: Client, client_secret: string): void {
if (client.secret !== client_secret) {
throw new OAuth2FrameworkError('invalid_secret', 'Invalid client_secret');
}
}
private throwIfClientNull(client: Client): void {
Eif (!client) {
throw new OAuth2FrameworkError('invalid_client_id', 'Invalid client id');
}
}
private throwIfInvalidCredentials(validCredentials: boolean): void {
if (!validCredentials) {
throw new OAuth2FrameworkError('invalid_credentials', 'Invalid credentials');
}
}
private throwIfInvalidGrantType(grant_type: string): void {
if (grant_type !== 'authorization_code' && grant_type !== 'password') {
throw new OAuth2FrameworkError('invalid_grant_type', 'Invalid grant type');
}
}
private throwIfInvalidResponseType(response_type: string): void {
if (response_type !== 'code' && response_type !== 'token') {
throw new OAuth2FrameworkError('invalid_response_type', 'Invalid response type');
}
}
}
|