This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
/
fxa-client.js
456 lines (392 loc) · 13.9 KB
/
fxa-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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// a very light wrapper around the real FxaClient to reduce boilerplate code
// and to allow us to develop to features that are not yet present in the real
// client.
'use strict';
define([
'fxaClient',
'jquery',
'lib/promise',
'lib/session',
'lib/auth-errors',
'lib/constants'
],
function (FxaClient, $, p, Session, AuthErrors, Constants) {
function trim(str) {
return $.trim(str);
}
function FxaClientWrapper(options) {
options = options || {};
this._client = options.client;
this._signUpResendCount = 0;
this._passwordResetResendCount = 0;
this._accountUnlockResendCount = 0;
if (! this._client && options.authServerUrl) {
this._client = new FxaClient(options.authServerUrl);
}
}
FxaClientWrapper.prototype = {
_getClient: function () {
return p(this._client);
},
/**
* Fetch some entropy from the server
*/
getRandomBytes: function () {
return this._getClient()
.then(function (client) {
return client.getRandomBytes();
});
},
/**
* Check the user's current password without affecting session state.
*/
checkPassword: function (email, password) {
var self = this;
return this._getClient()
.then(function (client) {
return client.signIn(email, password, {
reason: self.SIGNIN_REASON.PASSWORD_CHECK
})
.then(function (sessionInfo) {
// a session was created on the backend to check the user's
// password. Delete the newly created session immediately
// so that the session token is not left in the database.
if (sessionInfo && sessionInfo.sessionToken) {
return client.sessionDestroy(sessionInfo.sessionToken);
}
});
});
},
/**
* Check whether an account exists for the given uid.
*/
checkAccountExists: function (uid) {
return this._getClient()
.then(function (client) {
return client.accountStatus(uid)
.then(function (status) {
return status.exists;
});
});
},
_getUpdatedSessionData: function (email, relier, accountData, options) {
var sessionTokenContext = options.sessionTokenContext ||
relier.get('context');
var updatedSessionData = {
email: email,
uid: accountData.uid,
sessionToken: accountData.sessionToken,
sessionTokenContext: sessionTokenContext,
verified: accountData.verified || false
};
if (relier.wantsKeys()) {
updatedSessionData.unwrapBKey = accountData.unwrapBKey;
updatedSessionData.keyFetchToken = accountData.keyFetchToken;
}
if (relier.isSync()) {
updatedSessionData.customizeSync = options.customizeSync || false;
}
return updatedSessionData;
},
/**
* Signin reasons are indicators to the backend that the signin
* is to complete a particular action. The backend can choose
* to perform actions, e.g., send emails, based on the reason.
*/
SIGNIN_REASON: {
SIGN_IN: 'signin',
PASSWORD_CHECK: 'password_check',
PASSWORD_CHANGE: 'password_change',
PASSWORD_RESET: 'password_reset',
ACCOUNT_UNLOCK: 'account_unlock'
},
/**
* Authenticate a user.
*
* @method signIn
* @param {String} originalEmail
* @param {String} password
* @param {Releir} relier
* @param {Object} [options]
* @param {String} [options.reason] - reason for the sign in. Can be
* one of the values defined in SIGNIN_REASON.
* Defaults to SIGNIN_REASON.SIGN_IN.
* @param {Boolean} [options.customizeSync] - If the relier is Sync,
* whether the user wants to customize which items will
* be synced. Defaults to `false`
* @param {String} [options.sessionTokenContext] - The context for which
* the session token is being created. Defaults to the
* relier's context.
*/
signIn: function (originalEmail, password, relier, options) {
var email = trim(originalEmail);
var self = this;
options = options || {};
return self._getClient()
.then(function (client) {
var signInOptions = {
keys: relier.wantsKeys(),
reason: options.reason || self.SIGNIN_REASON.SIGN_IN
};
// `service` is sent on signIn to notify users when a new service
// has been attached to their account.
if (relier.has('service')) {
signInOptions.service = relier.get('service');
}
return client.signIn(email, password, signInOptions);
})
.then(function (accountData) {
return self._getUpdatedSessionData(email, relier, accountData, options);
});
},
signUp: function (originalEmail, password, relier, options) {
var email = trim(originalEmail);
var self = this;
options = options || {};
// ensure resend works again
this._signUpResendCount = 0;
return self._getClient()
.then(function (client) {
var signUpOptions = {
keys: relier.wantsKeys()
};
if (relier.has('service')) {
signUpOptions.service = relier.get('service');
}
if (relier.has('redirectTo')) {
signUpOptions.redirectTo = relier.get('redirectTo');
}
if (relier.has('preVerifyToken')) {
signUpOptions.preVerifyToken = relier.get('preVerifyToken');
}
if (options.preVerified) {
signUpOptions.preVerified = true;
}
signUpOptions.resume = self._createResumeToken(relier);
return client.signUp(email, password, signUpOptions)
.then(function (accountData) {
return self._getUpdatedSessionData(email, relier, accountData, options);
}, function (err) {
if (relier.has('preVerifyToken') &&
AuthErrors.is(err, 'INVALID_VERIFICATION_CODE')) {
// The token was invalid and the auth server could
// not pre-verify the user. Now, just create a new
// user and force them to verify their email.
relier.unset('preVerifyToken');
return self.signUp(email, password, relier, options);
}
throw err;
});
});
},
signUpResend: function (relier, sessionToken) {
var self = this;
return this._getClient()
.then(function (client) {
if (self._signUpResendCount >= Constants.SIGNUP_RESEND_MAX_TRIES) {
return p(true);
}
self._signUpResendCount++;
var clientOptions = {
service: relier.get('service'),
redirectTo: relier.get('redirectTo'),
resume: self._createResumeToken(relier)
};
return client.recoveryEmailResendCode(sessionToken, clientOptions);
});
},
signOut: function (sessionToken) {
return this._getClient()
.then(function (client) {
return client.sessionDestroy(sessionToken);
});
},
verifyCode: function (uid, code) {
return this._getClient()
.then(function (client) {
return client.verifyCode(uid, code);
});
},
passwordReset: function (originalEmail, relier) {
var self = this;
var email = trim(originalEmail);
// ensure resend works again
this._passwordResetResendCount = 0;
return this._getClient()
.then(function (client) {
var clientOptions = {
service: relier.get('service'),
redirectTo: relier.get('redirectTo'),
resume: self._createResumeToken(relier)
};
return client.passwordForgotSendCode(email, clientOptions);
})
.then(function (result) {
Session.clear();
return result;
});
},
passwordResetResend: function (originalEmail, passwordForgotToken, relier) {
var self = this;
var email = trim(originalEmail);
return this._getClient()
.then(function (client) {
if (self._passwordResetResendCount >= Constants.PASSWORD_RESET_RESEND_MAX_TRIES) {
return p(true);
}
self._passwordResetResendCount++;
// the linters complain if this is defined in the call to
// passwordForgotResendCode
var clientOptions = {
service: relier.get('service'),
redirectTo: relier.get('redirectTo'),
resume: self._createResumeToken(relier)
};
return client.passwordForgotResendCode(
email,
passwordForgotToken,
clientOptions
);
});
},
completePasswordReset: function (originalEmail, newPassword, token, code) {
var email = trim(originalEmail);
var client;
return this._getClient()
.then(function (_client) {
client = _client;
return client.passwordForgotVerifyCode(code, token);
})
.then(function (result) {
return client.accountReset(email,
newPassword,
result.accountResetToken);
});
},
isPasswordResetComplete: function (token) {
return this._getClient()
.then(function (client) {
return client.passwordForgotStatus(token);
})
.then(function () {
// if the request succeeds, the password reset hasn't completed
return false;
}, function (err) {
if (AuthErrors.is(err, 'INVALID_TOKEN')) {
return true;
}
throw err;
});
},
completeAccountUnlock: function (uid, code) {
return this._getClient()
.then(function (client) {
return client.accountUnlockVerifyCode(uid, code);
});
},
sendAccountUnlockEmail: function (email, relier) {
var self = this;
return self._getClient()
.then(function (client) {
if (self._accountUnlockResendCount >= Constants.ACCOUNT_UNLOCK_RESEND_MAX_TRIES) {
return p(true);
}
self._accountUnlockResendCount++;
var clientOptions = {};
if (relier) {
clientOptions = {
service: relier.get('service'),
redirectTo: relier.get('redirectTo'),
resume: self._createResumeToken(relier)
};
}
return client.accountUnlockResendCode(email, clientOptions);
});
},
changePassword: function (originalEmail, oldPassword, newPassword) {
var email = trim(originalEmail);
return this._getClient()
.then(function (client) {
return client.passwordChange(email, oldPassword, newPassword);
});
},
deleteAccount: function (originalEmail, password) {
var email = trim(originalEmail);
return this._getClient()
.then(function (client) {
return client.accountDestroy(email, password);
});
},
certificateSign: function (pubkey, duration, sessionToken) {
return this._getClient()
.then(function (client) {
return client.certificateSign(
sessionToken,
pubkey,
duration);
});
},
sessionStatus: function (sessionToken) {
return this._getClient()
.then(function (client) {
return client.sessionStatus(sessionToken);
});
},
isSignedIn: function (sessionToken) {
// Check if the user is signed in.
if (! sessionToken) {
return p(false);
}
// Validate session token
return this.sessionStatus(sessionToken)
.then(function () {
return true;
}, function (err) {
// the only error that we expect is INVALID_TOKEN,
// rethrow all others.
if (AuthErrors.is(err, 'INVALID_TOKEN')) {
return false;
}
throw err;
});
},
recoveryEmailStatus: function (sessionToken, uid) {
var self = this;
return self._getClient()
.then(function (client) {
return client.recoveryEmailStatus(sessionToken);
})
.then(null, function (err) {
// The user's email may have bounced because it's invalid. Check
// if the account still exists, if it doesn't, it means the email
// bounced. Show a message allowing the user to sign up again.
if (AuthErrors.is(err, 'INVALID_TOKEN')) {
return self.checkAccountExists(uid)
.then(function (accountExists) {
if (! accountExists) {
throw AuthErrors.toError('SIGNUP_EMAIL_BOUNCE');
}
throw err;
});
}
throw err;
});
},
accountKeys: function (keyFetchToken, unwrapBKey) {
return this._getClient()
.then(function (client) {
return client.accountKeys(keyFetchToken, unwrapBKey);
});
},
// The resume token is eventually for post-verification if the
// user verifies in a second client, with the goal of allowing
// users to continue back to the original RP.
_createResumeToken: function (relier) {
return relier.getResumeToken();
}
};
return FxaClientWrapper;
});