-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (38 loc) · 1.03 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
var uuid = require('node-uuid');
var Captcha = require('./lib/captcha');
var SESSION_NAME = 'SESSIONID';
var SESSION_REG = /SESSIONID=([-\w]+)/;
var session_map = {
get: function() {
var id = uuid.v4();
setTimeout(function() {
delete session_map[id]
}, 10 * 60 * 1000);
return id;
},
remove: function(id) {
delete session_map[id];
}
};
function get(ctx, width, height, code) {
width = width || 120;
height = height || 44;
code = code || Math.random().toString().substr(-4);
var imgbase64 = new Captcha(width, height, code).get('base64');
var session_id = session_map.get();
ctx.cookies.set(SESSION_NAME, session_id);
session_map[session_id] = code;
return [imgbase64, session_id];
};
function check(ctx, code) {
var cookie = ctx.headers['cookie'] || '';
var session_id = (cookie.match(SESSION_REG) || [0, 0])[1];
if (!session_id) return false;
var result = session_map[session_id] === code;
session_map.remove(session_id);
return result;
}
module.exports = {
get: get,
check: check
};