-
Notifications
You must be signed in to change notification settings - Fork 4
/
mfa-client.js
342 lines (285 loc) · 9.83 KB
/
mfa-client.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import { Meteor } from 'meteor/meteor';
import { Accounts } from 'meteor/accounts-base';
import { solveRegistrationChallenge, solveLoginChallenge } from '@webauthn/client';
import {authorizeActionChallengeHandler, authorizeActionCompletionHandler, resetPasswordCheckMFARequired, registrationChallengeHandlerTOTP, registrationCompletionHandlerTOTP, resetPasswordChallengeHandler, registrationChallengeHandlerU2F, registerCompletionHandlerU2F, loginChallengeHandler, loginCompletionHandler } from './method-names';
let debug = false;
let enableDebug = function () {
debug = true;
};
let solveU2FChallenge = async function (c) {
let {challengeId, challengeSecret, assertionChallenge} = c;
if(debug) {
console.debug("solveU2FChallenge called with challenge", assertionChallenge);
}
let credentials;
try {
credentials = await solveLoginChallenge(assertionChallenge);
}
catch(e) {
if(e.name === "NotAllowedError") {
throw new Meteor.Error("user-cancelled", "The user cancelled the request or the request timed out");
}
else {
throw e;
}
}
return {challengeId, challengeSecret, credentials};
};
let registerMFA = (params) => new Promise((resolve, reject) => {
if(!params) {
params = {passwordless:false};
}
else {
if(typeof(params.password) === "string") {
params.password = Accounts._hashPassword(params.password);
}
}
Meteor.call(registrationChallengeHandlerU2F(), params, async (err, res) => {
if(err) {
return reject(err);
}
let credentials;
try {
res.authenticatorSelection = {authenticatorAttachment:"cross-platform"};
if(debug) {
console.debug("registerU2F called with challenge", res);
}
credentials = await solveRegistrationChallenge(res);
}
catch(e) {
if(e.name === "NotAllowedError") {
return reject(new Meteor.Error("user-cancelled", "The user cancelled the request or the request timed out"));
}
return reject(e);
}
Meteor.call(registerCompletionHandlerU2F(), credentials, (err, res) => {
if(err) {
reject(err);
}
else {
resolve();
}
});
});
});
let useU2FAuthorizationCode = function (code) {
if(typeof(code) !== "string" || code.length !== 6) {
throw new Error("Invalid Code");
}
return {U2FAuthorizationCode:code};
};
let supportsU2FLogin = function () {
return (window.navigator.credentials.get instanceof Function);
};
let assembleChallengeCompletionArguments = async function (finishLoginParams, code) {
let {res} = finishLoginParams;
let methodArguments = [];
if(res.method === "u2f") {
let assertion;
if(code && code.U2FAuthorizationCode) {
/*
We require that the MFA.useU2FAuthorizationCode method is used
even though we just pull the code out to make sure the code isn't
actually an OTP due to a coding error.
*/
let {challengeId, challengeSecret} = finishLoginParams.res;
assertion = {challengeId, challengeSecret, ...code};
}
else {
assertion = await solveU2FChallenge(res);
}
methodArguments.push(assertion);
}
if(res.method === "otp" || res.method === "totp") {
if(!code) {
throw new Meteor.Error("otp-required", "An OTP is required");
}
methodArguments.push({...res, code});
}
return methodArguments;
};
let solveChallenge = async function (challenge, code) {
let solvedChallenge = assembleChallengeCompletionArguments({res:challenge}, code)[0];
return solvedChallenge;
};
let finishResetPassword = (finishLoginParams, code) => new Promise(async (resolve, reject) => {
let {res, token, newPassword} = finishLoginParams;
if (!token instanceof String) {
return reject(new Meteor.Error(400, "Token must be a string"));
}
if (!newPassword instanceof String) {
return reject(new Meteor.Error(400, "Password must be a string"));
}
if (!newPassword) {
return reject(new Meteor.Error(400, "Password may not be empty"));
}
let methodArguments = [token, Accounts._hashPassword(newPassword)];
methodArguments = methodArguments.concat(await assembleChallengeCompletionArguments(finishLoginParams, code));
Accounts.callLoginMethod({
methodName: 'resetPassword',
methodArguments,
userCallback:(err) => {
if(err) {
console.error("resetPassword:Accounts.callLoginMethod", err);
reject(err);
}
else {
resolve();
}
}
});
});
let resetPasswordWithMFA = (token, newPassword) => new Promise((resolve, reject) => {
Meteor.call(resetPasswordChallengeHandler(), token, async (err, res) => {
console.log(res);
if(err) {
console.error("resetPasswordChallengeHandler", err);
return reject(err);
}
let finishLoginParams = {token, newPassword, res, _type:"resetPassword"};
resolve({method:res.method, finishLoginParams, finishParams:finishLoginParams});
});
});
let resetPassword = (token, newPassword) => new Promise((resolve, reject) => {
Meteor.call(resetPasswordCheckMFARequired(), token, (err, res) => {
if(err) {
return reject(err);
}
if(res === true) {
resetPasswordWithMFA(token, newPassword).then(resolve).catch(reject);
}
else {
Accounts.resetPassword(token, newPassword, err => {
if(err) {
reject(err);
}
else {
resolve(err);
}
});
}
});
});
let finishLogin = (finishLoginParams, code) => new Promise(async (resolve, reject) => {
let methodName = loginCompletionHandler();
let methodArguments = await assembleChallengeCompletionArguments(finishLoginParams, code);
Accounts.callLoginMethod({
methodName,
methodArguments,
userCallback:(err) => {
if(err) {
reject(err);
}
else {
resolve();
}
}
});
});
let loginWithPasswordless = (query) => new Promise((resolve, reject) => {
Meteor.call(loginChallengeHandler(), query, {passwordless:true}, async (err, res) => {
if(err) {
if(err.error === "not-passwordless" || err.error === "not-mfa") {
resolve(true);
}
else {
reject(err);
}
}
else {
let methodName = loginCompletionHandler();
let methodArguments = await assembleChallengeCompletionArguments({res});
Accounts.callLoginMethod({
methodName,
methodArguments,
userCallback:(err) => {
if(err) {
reject(err);
}
else {
resolve();
}
}
});
}
});
});
let loginWithMFA = (username, password) => new Promise((resolve, reject) => {
Meteor.call(loginChallengeHandler(), username, Accounts._hashPassword(password), async (err, res) => {
if(err) {
return reject(err);
}
let finishLoginParams = {res, _type:"login"};
let doesSupportU2FLogin = supportsU2FLogin();
resolve({supportsU2FLogin:doesSupportU2FLogin, method:res.method, finishLoginParams, finishParams:finishLoginParams});
});
});
let login = (username, password) => new Promise((resolve, reject) => {
Meteor.loginWithPassword(username, password, err => {
if(err) {
if(err.error === "mfa-required") {
loginWithMFA(username, password).then(resolve).catch(reject);
}
else {
reject(err);
}
}
else {
resolve({method:null});
}
});
});
let finishRegisterTOTP = (token, registrationId) => new Promise((resolve, reject) => {
Meteor.call(registrationCompletionHandlerTOTP(), {registrationId, token}, err => {
if(err) {
reject(err);
}
else {
resolve();
}
});
});
let registerTOTP = () => new Promise((resolve, reject) => {
Meteor.call(registrationChallengeHandlerTOTP(), (err, res) => {
if(err) {
reject(err);
}
else {
resolve(res);
}
});
});
let authorizeAction = (type) => new Promise((resolve, reject) => {
Meteor.call(authorizeActionChallengeHandler(), type, async (err, res) => {
if(err) {
return reject(err);
}
let solvedChallenge = await solveU2FChallenge(res.challenge);
Meteor.call(authorizeActionCompletionHandler(), res.authorizationId, solvedChallenge, async(err, res) => {
if(err) {
return reject(err);
}
resolve(res.code);
});
});
});
export default {
loginWithPasswordless,
authorizeAction,
useU2FAuthorizationCode,
supportsU2FLogin,
solveChallenge,
solveU2FChallenge,
registerMFA, registerU2F:registerMFA,
finishResetPassword,
resetPasswordWithMFA,
resetPassword,
finishLogin,
loginWithMFA,
login,
finishRegisterTOTP,
registerTOTP,
enableDebug,
// DEPRECATED in 0.0.3
solve:solveU2FChallenge,
};