-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
169 lines (136 loc) · 4.26 KB
/
index.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
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
const authmakerVerify = require('authmaker-verify');
const winston = require('winston');
const Q = require('q');
// remove it so to add it with my settings
try {
winston.remove(winston.transports.Console);
} catch (e) { // do nothing
}
const winstonOptions = {
colorize: true,
timestamp: true,
handleExceptions: true,
prettyPrint: true,
};
if (process.env.LOG_LEVEL) {
winstonOptions.level = process.env.LOG_LEVEL;
} else if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') {
winstonOptions.level = 'debug';
} else {
winstonOptions.level = 'info';
}
if (!process.env.NO_LOG) {
winston.add(winston.transports.Console, winstonOptions);
}
function generateRateLimit(tag, defaultScope) {
return function rateLimit(req, res, next) {
if (!req.headers.authorization) {
return res.status(401).send('No Access token provided');
}
// take the accessToken as the last delimited entry in authorization
const accessToken = req.headers.authorization.split(/\s+/).pop();
// verify the access-token
return authmakerVerify.mongoRateLimited(accessToken, tag, defaultScope)
.then((oauthSession) => {
req.oauthSession = oauthSession;
// add the user to the req.user
return authmakerVerify.models.user.findOne({
_id: oauthSession.userId,
}).exec().then((user) => {
req.user = user;
});
})
.then(() => {
next();
})
.then(null, (err) => {
winston.error('Error while authorizing session', {
error: err.message,
stask: err.stack,
authorisation: req.headers.authorization,
});
if (err.message.indexOf('Not Authorized') >= 0) {
res.status(401);
} else if (err.message.indexOf('Too Many Requests') >= 0) {
res.status(429);
return res.send('Too Many Requests: Rate limit exceeded.');
} else {
res.status(500);
}
return res.send(err.message);
});
};
}
function generateVerify(tag, options) {
return function (req, res, next) {
// verify the access-token
return Q.fcall(() => {
if (!req.headers.authorization) {
throw new Error('Not Authorized: No Access token provided');
}
// take the accessToken as the last delimited entry in authorization
const accessToken = req.headers.authorization.split(/\s+/).pop();
return authmakerVerify.mongo(accessToken, tag);
})
.then((oauthSession) => {
req.oauthSession = oauthSession;
// add the user to the req.user
return authmakerVerify.models.user.findOne({
_id: oauthSession.userId,
}).exec().then((user) => {
req.user = user;
});
})
.then(() => {
next();
})
.then(null, (err) => {
winston.error('Error while authorizing session', {
error: err.message,
stask: err.stack,
authorisation: req.headers.authorization,
});
if (options && options.passError) {
return next(err);
}
if (err.message.indexOf('Not Authorized') >= 0) {
res.status(401);
} else {
res.status(500);
}
return res.send(err.message);
});
};
}
function getExternalIdentities(req, res, next) {
if (!req.user) {
return next('req.user not defined - must use mongo(), mongoRateLimited() or mongoRateLimitedDefault() before this middleware');
}
return authmakerVerify.models.externalIdentity.find({
_id: req.user.externalIdentities,
}).exec().then((externalIdentities) => {
req.externalIdentities = externalIdentities;
next();
}).then(null, (err) => {
next(err);
});
}
module.exports = {
mongoRateLimited(tag, defaultScope) {
return generateRateLimit(tag, defaultScope);
},
mongoRateLimitedDefault(tag, defaultScope) {
console.warn('This function is deprecated, just use mongoRateLimited(tag, defaultScope) instead');
return generateRateLimit(tag, defaultScope);
},
mongo(tag, options) {
return generateVerify(tag, options);
},
externalIdentities: getExternalIdentities,
init(nconf) {
// initialise the db
return authmakerVerify.init(nconf);
},
models: authmakerVerify.models,
authmakerVerify,
};