forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth2-server.d.ts
424 lines (369 loc) · 12.8 KB
/
oauth2-server.d.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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// Type definitions for Node OAuth2 Server
// Project: https://github.com/thomseddon/node-oauth2-server
// Definitions by: Robbie Van Gorkom <https://github.com/vangorra>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/* =================== USAGE ===================
import * as oauthserver from "oauth2-server";
var oauth = oauthserver();
=============================================== */
/// <reference path="../express/express.d.ts" />
declare module "oauth2-server" {
import {RequestHandler} from "express";
import {Request} from "express";
function o(config : o.Config) : o.OAuth2Server;
namespace o {
interface OAuth2Server {
grant() : RequestHandler;
authorise() : any;
errorHandler() : any;
}
interface Config {
/**
* Model object
*/
model: {};
/**
* grant types you wish to support, currently the module supports authorization_code,
* password, refresh_token and client_credentials
*/
grants: string[];
/**
* If true errors will be logged to console. You may also pass a custom function, in
* which case that function will be called with the error as its first argument
* Default: false
*/
debug?: boolean;
/**
* Life of access tokens in seconds
* If null, tokens will considered to never expire
* Default: 3600
*/
accessTokenLifetime?: number;
/**
* Life of refresh tokens in seconds
* If null, tokens will considered to never expire
* Default: 1209600
*/
refreshTokenLifetime?: number;
/**
* Life of auth codes in seconds
* Default: 30
*/
authCodeLifetime?: number;
/**
* Regex to sanity check client id against before checking model. Note: the default
* just matches common client_id structures, change as needed
* Default: /^[a-z0-9-_]{3,40}$/i
*/
clientIdRegex?: RegExp;
/**
* If true, non grant errors will not be handled internally (so you can ensure a
* consistent format with the rest of your api)
*/
passthroughErrors?: boolean;
/**
* If true, next will be called even if a response has been sent (you probably don't want this)
*/
continueAfterResponse?: boolean;
}
interface BaseModel {
/**
*
* @param bearerToken - The bearer token (access token) that has been provided
* @param callback
*/
getAccessToken(bearerToken:string,
callback:GetAccessTokenCallback) : void;
/**
*
* @param clientId
* @param clientSecret - If null, omit from search query (only search by clientId)
* @param callback
*/
getClient(clientId:string,
clientSecret:string,
callback:GetClientCallback) : void;
/**
*
* @param clientId
* @param grantType
* @param callback
*/
grantTypeAllowed(clientId:string,
grantType:string,
callback:GrantTypeAllowedCallback) : void;
/**
*
* @param accessToken
* @param clientId
* @param expires
* @param user
* @param callback
*/
saveAccessToken(accessToken:string,
clientId:string,
expires:Date,
user:User,
callback:SaveAccessTokenCallback) : void;
}
interface AuthorizationCodeModel extends BaseModel {
/**
*
* @param authCode
* @param callback
*/
getAuthCode(authCode:string,
callback:GetAuthCodeCallback) : void;
/**
*
* @param authCode
* @param clientId
* @param expires
* @param user - Whatever was passed as user to the codeGrant function (see example)
* @param callback
*/
saveAuthCode(authCode:string,
clientId:string,
expires:Date,
user:User|string,
callback:SaveAuthCodeCallback) : void;
}
interface PasswordModel extends BaseModel {
/**
*
* @param username
* @param password
* @param callback
*/
getUser(username:string,
password:string,
callback:GetUserCallback) : void;
}
interface RefreshTokenModel extends BaseModel {
/**
*
* @param refreshToken
* @param clientId
* @param expires
* @param user
* @param callback
*/
saveRefreshToken(refreshToken:string,
clientId:string,
expires:Date,
user:User,
callback:SaveRefreshTokenCallback) : void;
/**
*
* @param refreshToken - The bearer token (refresh token) that has been provided
* @param callback
*/
getRefreshToken(refreshToken:string,
callback:GetRefreshTokenCallback) : void;
/**
* The spec does not actually require that you revoke the old token - hence this is
* optional (Last paragraph: http://tools.ietf.org/html/rfc6749#section-6)
* @param refreshToken
* @param callback
*/
revokeRefreshToken?(refreshToken:string,
callback:RevokeRefreshTokenCallback) : void;
}
interface ExtensionModel extends BaseModel {
/**
*
* @param grantType
* @param req
* @param callback
*/
extendedGrant(grantType:string,
req:Request,
callback:ExtendedGrantCallback) : void;
}
interface ClientCredentialsModel extends BaseModel {
/**
*
* @param clientId
* @param clientSecret
* @param callback
*/
getUserFromClient(clientId:string,
clientSecret:string,
callback:GetUserFromClientCallback) : void;
/**
*
* @param type - accessToken or refreshToken
* @param req - The current express request
* @param callback
*/
generateToken?(type:string,
req:Request,
callback:GenerateTokenCallback) : void;
}
interface GenerateTokenCallback {
/**
*
* @param error - Truthy to indicate an error
* @param token - string indicates success
* null indicates to revert to the default token generator
* object indicates a reissue (i.e. will not be passed to saveAccessToken/saveRefreshToken)
* Must contain the following keys (if object):
* string accessToken OR refreshToken dependant on type
*/
(error:any, token:string|Object) : void;
}
interface GetUserFromClientCallback {
/**
*
* @param error - Truthy to indicate an error
* @param user - The user retrieved from storage or falsey to indicate an invalid user
* Saved in req.user
*/
(error:any, user:User) : void;
}
interface ExtendedGrantCallback {
/**
*
* @param error - Truthy to indicate an error
* @param supported - Whether you support the grant type
* @param user - The user retrieved from storage or falsey to indicate an invalid user
* Saved in req.user
*/
(error:any, supported:boolean, user:User) : void;
}
interface RevokeRefreshTokenCallback {
/**
* Truthy to indicate an error
* @param error
*/
(error:any) : void;
}
interface GetRefreshTokenCallback {
/**
*
* @param error - Truthy to indicate an error
* @param refreshToken - The refresh token retrieved form storage or falsey to indicate invalid refresh token
*/
(error:any, refreshToken:RefreshToken) : void;
}
interface SaveRefreshTokenCallback {
/**
*
* @param error - Truthy to indicate an error
*/
(error:any) : void;
}
interface GetUserCallback {
/**
*
* @param error - Truthy to indicate an error
* @param user - The user retrieved from storage or falsey to indicate an invalid user
* Saved in req.user
*/
(error:any, user:User) : void;
}
interface SaveAuthCodeCallback {
/**
*
* @param error - Truthy to indicate an error
*/
(error:any) : void;
}
interface GetAuthCodeCallback {
/**
*
* @param error - Truthy to indicate an error
* @param authCode - The authorization code retrieved form storage or falsey to indicate invalid code
*/
(error:String, authCode:AuthCode) : void
}
interface SaveAccessTokenCallback {
/**
*
* @param error - Truthy to indicate an error
*/
(error:any) : void;
}
interface GetAccessTokenCallback {
/**
*
* @param error - Truthy to indicate an error
* @param accessToken - The access token retrieved form storage or falsey to indicate invalid access token
*/
(error:any, accessToken:AccessToken) : void;
}
interface GetClientCallback {
/**
*
* @param error - Truthy to indicate an error
* @param client - The client retrieved from storage or falsey to indicate an invalid client
* Saved in req.client
*/
(error:any, client:Client) : void;
}
interface GrantTypeAllowedCallback {
/**
*
* @param error - Truthy to indicate an error
* @param allowed - Indicates whether the grantType is allowed for this clientId
*/
(error:any, allowed:boolean) : void;
}
interface RefreshToken {
/**
* client id associated with this token
*/
clientId : string;
/**
* The date when it expires
* null to indicate the token never expires
*/
expires : Date;
/**
*
*/
userId : string;
}
interface AuthCode {
/**
* client id associated with this auth code
*/
clientId : string;
/**
* The date when it expires
*/
expires : Date;
/**
* The userId
*/
userId : string;
}
interface User {
id: string;
}
interface Client {
clientId: string;
/**
* authorization_code grant type only
*/
redirectUri: string;
}
interface AccessToken {
/**
* The date when it expires
* null to indicate the token never expires
*/
expires: Date;
/**
* If a user key exists, this is saved as req.user
*/
user?: User;
/**
* If a user key exists, this is saved as req.user
* Otherwise a userId key must exist, which is saved in req.user.id
*/
userId?: string;
}
}
export = o;
}