forked from anvilresearch/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverifyAuthorizationCode.js
95 lines (77 loc) · 2.3 KB
/
verifyAuthorizationCode.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
91
92
93
94
95
/**
* Module dependencies
*/
var AuthorizationCode = require('../models/AuthorizationCode')
, AuthorizationError = require('../errors/AuthorizationError')
, nowSeconds = require('../lib/time-utils').nowSeconds
;
/**
* Verify authorization code
*/
function verifyAuthorizationCode (req, res, next) {
var params = req.connectParams;
if (params.grant_type === 'authorization_code') {
AuthorizationCode.getByCode(params.code, function (err, ac) {
if (err) { return next(err); }
// Can't find authorization code
if (!ac) {
return next(new AuthorizationError({
error: 'invalid_grant',
error_description: 'Authorization not found',
statusCode: 400
}));
}
// Authorization code has been previously used
if (ac.used === true) {
return next(new AuthorizationError({
error: 'invalid_grant',
error_description: 'Authorization code invalid',
statusCode: 400
}));
}
// Authorization code is expired
if (nowSeconds() > ac.expires_at) {
return next(new AuthorizationError({
error: 'invalid_grant',
error_description: 'Authorization code expired',
statusCode: 400
}));
}
// Mismatching redirect uri
if (ac.redirect_uri !== params.redirect_uri) {
return next(new AuthorizationError({
error: 'invalid_grant',
error_description: 'Mismatching redirect uri',
statusCode: 400
}));
}
// Mismatching client id
if (ac.client_id !== req.client._id) {
return next(new AuthorizationError({
error: 'invalid_grant',
error_description: 'Mismatching client id',
statusCode: 400
}));
}
// Mismatching user id
//if (ac.user_id !== req.user._id) {
// return next(new AuthorizationError({
// error: 'invalid_grant',
// error_description: 'Mismatching client id',
// statusCode: 400
// }));
//}
req.code = ac;
// Update the code to show that it's been used.
AuthorizationCode.patch(ac._id, { used: true }, function (err) {
next(err);
});
});
} else {
next();
}
}
/**
* Exports
*/
module.exports = verifyAuthorizationCode;