forked from anvilresearch/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunstashParams.js
47 lines (35 loc) · 991 Bytes
/
unstashParams.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
/**
* Module dependencies
*/
var client = require('../boot/redis')
, MissingStateError = require('../errors/MissingStateError')
, ExpiredAuthorizationRequestError = require('../errors/ExpiredAuthorizationRequestError')
;
/**
* Unstash authorization params
*/
function unstashParams (req, res, next) {
// OAuth 2.0 callbacks should have a state param
// OAuth 1.0 must use the session to store the state value
var id = req.query.state || req.session.state
, key = 'authorization:' + id
;
if (!id) { // && request is OAuth 2.0
return next(new MissingStateError());
}
client.get(key, function (err, params) {
if (err) { return next(err); }
// This handles expired and mismatching state params
if (!params) { return next(new ExpiredAuthorizationRequestError()); }
try {
req.connectParams = JSON.parse(params);
} catch (err) {
next(err);
}
next();
});
}
/**
* Exports
*/
module.exports = unstashParams;