-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
103 lines (87 loc) · 2.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
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
var url = require('url');
var EventEmitter = require('events').EventEmitter;
var mailer = require('nodemailer');
var ent = require('ent');
module.exports = function (opts) {
if (typeof opts === 'string') {
opts = { uri : opts };
}
var transport;
if (opts.transportType && opts.transportOptions) {
transport = mailer.createTransport(opts.transportType, opts.transportOptions);
} else {
console.log("No transport type specified!");
}
var reset = new Forgot(opts);
var self = function (email, cb) {
var session = reset.generate();
if (!session) return;
var uri = session.uri = opts.uri + '?' + session.id;
transport.sendMail({
sender : opts.from || 'nodepasswordreset@localhost',
to : email,
subject : opts.subject || 'Password reset request',
text : opts.text || "",
html : opts.html || [
'Click this link to reset your password:\r\n',
'<br>',
'<a href="' + encodeURI(uri) + '">',
ent.encode(uri),
'</a>',
''
].join('\r\n')
}, function (error, success) {
if (error) {
if (cb.error) cb.error(error);
delete reset.sessions[session.id];
} else {
if(cb.success) cb.success(success)
}
});
return session;
};
self.middleware = reset.middleware.bind(reset);
self.expire = function (id) {
delete reset.sessions[id];
};
return self;
};
function Forgot (opts) {
this.sessions = opts.sessions || {};
this.mount = url.parse(opts.uri);
this.mount.port = this.mount.port || 80;
}
Forgot.prototype.generate = function () {
var buf = new Buffer(16);
for (var i = 0; i < buf.length; i++) {
buf[i] = Math.floor(Math.random() * 256);
}
var id = buf.toString('base64');
var session = this.sessions[id] = new EventEmitter;
session.id = id;
return session;
};
Forgot.prototype.middleware = function (req, res, next) {
if (!next) next = function (err) {
if (err) res.end(err)
}
var u = url.parse('http://' + req.headers.host + req.url);
u.port = u.port || 80;
var id = u.query;
if (u.hostname !== this.mount.hostname
|| parseInt(u.port, 10) !== parseInt(this.mount.port, 10)
|| u.pathname !== this.mount.pathname) {
next()
}
else if (!id) {
res.statusCode = 400;
next('No auth token specified.');
}
else if (!this.sessions[id]) {
res.statusCode = 410;
next('auth token expired');
}
else {
this.sessions[id].emit('request', req, res);
}
};