/
magicCode.js
180 lines (171 loc) · 5.17 KB
/
magicCode.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
170
171
172
173
174
175
176
177
178
179
180
const { validate: isEmail } = require('email-validator');
const HttpError = require('http-errors');
const date = require('date.js');
const { generate } = require('randomstring');
const { ObjectId } = require('mongodb');
const { pick, isFunction } = require('lodash');
const { getDefaultPost } = require('../index');
const template = require('../templates/magicToken');
const textTemplate = require('../templates/magicTokenText');
const getConfig = (obj = {}) => ({
emailField: 'email',
collection: 'moser-magic-codes',
max: 3,
length: 4,
exp: 'in one day',
redirectNotFound: null,
redirectAlreadyUsed: null,
redirectSuccess: null,
doVerify: null,
doGenerate: null,
default: null,
extraTokenFields: [], // fields to be extracted from first req.body and added to token
...obj,
email: Object.assign({}, {
from: 'email@email.com',
text: textTemplate,
html: template,
subject: 'Hey! use this link to login in our app',
...obj.email || {},
}),
});
const STATUS = {
VERIFIED: 'VERIFIED',
RETRIEVED: 'RETRIEVED',
};
exports.magicCodeCreateToken = (
config, db, magicResources, transport,
) => async (req, res, next) => {
/**
* Only resources with magic links
*/
const resourceName = req.params.resource;
if (!magicResources.includes(req.params.resource)) return next();
/**
* Get configuration
*/
const conf = getConfig(config.resources[resourceName].auth.magicCode);
const MagicTokens = db.collection(conf.collection);
const User = db.collection(resourceName);
/**
* Verify email and tokens
*/
const email = (req.body[conf.emailField] || '').toLowerCase();
if (!email) throw new HttpError.BadRequest('Missing Email');
if (!isEmail(email)) throw new HttpError.BadRequest('Invalid Email');
const tokens = await MagicTokens.find({ email, exp: { $gte: new Date() } }).toArray();
if (tokens.length >= conf.max) throw new HttpError.TooManyRequests('Token limit reached');
/**
* Handle custom logic
*/
const user = await User.findOne({ [conf.emailField]: email });
const _id = !user ? new ObjectId().toString():false;
if (conf.doGenerate) {
await conf.doGenerate({
req, res, next, user, HttpError, db, resourceName, newUserId: _id,
});
}
/**
* Create and store token, also create user if it didnt exist
*/
const token = generate({
length: 4,
charset: 'numeric',
});
await MagicTokens.insertOne({
...pick(req.body, conf.extraTokenFields),
email,
exp: date(conf.exp, null),
token,
});
let userCreated = false;
if (!user && !conf.default) {
userCreated = true;
await User.insertOne({ _id, email });
}
if (!user && conf.default) {
userCreated = true;
await User
.insertOne({
...await getDefaultPost(conf.default(req.body, user), user, req, db),
_id,
email,
});
}
/**
* Send email
*/
await transport.sendMail({
to: email,
from: conf.email.from,
text: conf.email.text(config, user, token),
html: conf.email.html(config, user, token),
title: conf.email.title,
subject: isFunction(conf.email.subject)
? conf.email.subject(config, user, token)
: conf.email.subject,
});
/**
* return token _id
*/
return res.send({ userCreated });
};
exports.magicCodeVerifyToken = (config, db, magicResources) => async (req, res, next) => {
const jwt = require('jsonwebtoken'); // eslint-disable-line global-require
/**
* Only resources with magic links
*/
const resourceName = req.params.resource;
const { token, email } = req.params;
if (!magicResources.includes(resourceName)) return next();
/**
* Validate token
*/
if (!token) throw new HttpError.BadRequest('Missing Token');
if (!email) throw new HttpError.BadRequest('Missing Email');
/**
* Get config
*/
const conf = getConfig(config.resources[resourceName].auth.magicCode);
const MagicTokens = db.collection(conf.collection);
const User = db.collection(resourceName);
/**
* Check that the token is not expired and wasn't used
*/
const tokenObj = await MagicTokens.findOne({ token, email });
if (!tokenObj) throw new HttpError.NotFound('Token not found');
if (tokenObj.exp.getTime() < new Date().getTime()) throw new HttpError.BadRequest('Token expired');
if (Object.keys(STATUS).includes(tokenObj.status)) throw new HttpError.BadRequest('Token already used');
const user = await User.findOne({ [conf.emailField]: tokenObj.email });
if (!user) throw new HttpError.BadRequest('User is not on db anymore');
/**
* handle custom user logic
*/
if (conf.doVerify) {
await conf.doVerify({
req, res, next, user, db, token: tokenObj, HttpError,
});
}
/**
* Add permission magicCode:verified on permissions
*/
await MagicTokens.updateOne({ _id: tokenObj.id }, { $set: { status: STATUS.VERIFIED } });
await User.updateOne({ _id: user._id }, { $addToSet: { permissions: 'email:verified' } });
/**
* create token and send
*/
const { permissions, _id } = user;
const $token = await jwt.sign(
{
_id,
resource: resourceName,
permissions,
},
config.jwtSecret,
);
return res.send({
state: 'success',
$token,
_id: user._id,
});
};