/
index.js
59 lines (55 loc) · 1.73 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
'use strict';
const handler = require('./lib/captchaHandler');
function captchaInit(options) {
options = options || {};
// the request header where we expect the jwt token
const tokenHeader = options.tokenHeader || 'Authorization';
// the request header where we expect the client nonce
const nonceHeader = options.nonceHeader || 'captcha-nonce';
const captchaHandler = handler({
tokenSecret: options.tokenSecret,
expiryMinutes: options.expiryMinutes,
languageMap: options.languageMap,
// SVG Captcha configs
captchaSize: options.captchaSize,
ignoreChars: options.ignoreChars,
noise: options.noise,
width: options.width,
height: options.height,
background: options.background,
color: options.color,
inverse: options.inverse,
fontSize: options.fontSize
});
return {
getCaptcha: function(req, res, next) {
const fullReponse = captchaHandler.createCaptcha(req.body.nonce);
res.send(fullReponse);
next();
},
verifyCaptcha: function(req, res, next) {
const ret = captchaHandler.verifyCaptcha(req.body);
res.send(ret);
next();
},
getCaptchaAudio: function(req, res, next) {
captchaHandler.getAudio(req.body, req).then(ret => {
res.send(ret);
next();
});
},
verifyJWTResponseMiddleware: function(req, res, next) {
let token = req.headers[tokenHeader.toLowerCase()] || '';
token = token.replace('Bearer ', '');
const nonce = req.headers[nonceHeader];
const ret = captchaHandler.verifyJWTResponse(token, nonce);
if (ret.valid) {
next();
} else {
res.send(401, 'Not Authorized');
next('Invalid Captcha Token');
}
}
};
}
module.exports = captchaInit;