mirror of
https://github.com/bitwarden/browser
synced 2025-12-22 11:13:46 +00:00
Added two factor flow into identity login
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
function ApiService(tokenService) {
|
||||
this.baseUrl = 'https://api.bitwarden.com';
|
||||
this.baseUrl = 'http://localhost:4000';
|
||||
this.tokenService = tokenService;
|
||||
|
||||
initApiService();
|
||||
@@ -8,26 +8,7 @@ function ApiService(tokenService) {
|
||||
function initApiService() {
|
||||
// Auth APIs
|
||||
|
||||
ApiService.prototype.postTokenTwoFactor = function (twoFactorTokenRequest, success, error) {
|
||||
var self = this;
|
||||
this.tokenService.getToken(function (token) {
|
||||
$.ajax({
|
||||
type: 'POST',
|
||||
url: self.baseUrl + '/auth/token/two-factor?access_token2=' + token,
|
||||
data: JSON.stringify(twoFactorTokenRequest),
|
||||
contentType: 'application/json; charset=utf-8',
|
||||
dataType: 'json',
|
||||
success: function (response) {
|
||||
success(new TokenResponse(response));
|
||||
},
|
||||
error: function (jqXHR, textStatus, errorThrown) {
|
||||
handleError(error, jqXHR, textStatus, errorThrown);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
ApiService.prototype.postIdentityToken = function (tokenRequest, success, error) {
|
||||
ApiService.prototype.postIdentityToken = function (tokenRequest, success, successWithTwoFactor, error) {
|
||||
var self = this;
|
||||
|
||||
$.ajax({
|
||||
@@ -40,7 +21,13 @@ function initApiService() {
|
||||
success(new IdentityTokenResponse(response));
|
||||
},
|
||||
error: function (jqXHR, textStatus, errorThrown) {
|
||||
handleError(error, jqXHR, textStatus, errorThrown);
|
||||
if (jqXHR.responseJSON && jqXHR.responseJSON.TwoFactorRequired &&
|
||||
jqXHR.responseJSON.TwoFactorRequired === true) {
|
||||
successWithTwoFactor();
|
||||
}
|
||||
else {
|
||||
error(new ErrorResponse(jqXHR, true));
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@@ -13,16 +13,23 @@ function initUserService() {
|
||||
var _userId = null,
|
||||
_email = null;
|
||||
|
||||
UserService.prototype.setUserId = function (userId, callback) {
|
||||
UserService.prototype.setUserIdAndEmail = function (userId, email, callback) {
|
||||
if (!callback || typeof callback !== 'function') {
|
||||
throw 'callback function required';
|
||||
}
|
||||
|
||||
_email = email;
|
||||
var emailObj = {};
|
||||
emailObj[userEmailKey] = email;
|
||||
|
||||
_userId = userId;
|
||||
var obj = {};
|
||||
obj[userIdKey] = userId;
|
||||
chrome.storage.local.set(obj, function () {
|
||||
callback();
|
||||
var userIdObj = {};
|
||||
userIdObj[userIdKey] = userId;
|
||||
|
||||
chrome.storage.local.set(userIdObj, function () {
|
||||
chrome.storage.local.set(emailObj, function () {
|
||||
callback();
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
@@ -44,30 +51,6 @@ function initUserService() {
|
||||
});
|
||||
};
|
||||
|
||||
UserService.prototype.clearUserId = function (callback) {
|
||||
if (!callback || typeof callback !== 'function') {
|
||||
throw 'callback function required';
|
||||
}
|
||||
|
||||
_userId = null;
|
||||
chrome.storage.local.remove(userIdKey, function () {
|
||||
callback();
|
||||
});
|
||||
};
|
||||
|
||||
UserService.prototype.setEmail = function (email, callback) {
|
||||
if (!callback || typeof callback !== 'function') {
|
||||
throw 'callback function required';
|
||||
}
|
||||
|
||||
_email = email;
|
||||
var obj = {};
|
||||
obj[userEmailKey] = email;
|
||||
chrome.storage.local.set(obj, function () {
|
||||
callback();
|
||||
});
|
||||
};
|
||||
|
||||
UserService.prototype.getEmail = function (callback) {
|
||||
if (!callback || typeof callback !== 'function') {
|
||||
throw 'callback function required';
|
||||
@@ -86,14 +69,16 @@ function initUserService() {
|
||||
});
|
||||
};
|
||||
|
||||
UserService.prototype.clearEmail = function (callback) {
|
||||
UserService.prototype.clearUserIdAndEmail = function (callback) {
|
||||
if (!callback || typeof callback !== 'function') {
|
||||
throw 'callback function required';
|
||||
}
|
||||
|
||||
_email = null;
|
||||
chrome.storage.local.remove(userEmailKey, function () {
|
||||
callback();
|
||||
_userId = _email = null;
|
||||
chrome.storage.local.remove(userIdKey, function () {
|
||||
chrome.storage.local.remove(userEmailKey, function () {
|
||||
callback();
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
@@ -114,22 +99,4 @@ function initUserService() {
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
UserService.prototype.isTwoFactorAuthenticated = function (callback) {
|
||||
if (!callback || typeof callback !== 'function') {
|
||||
throw 'callback function required';
|
||||
}
|
||||
|
||||
var self = this;
|
||||
self.tokenService.getToken(function (token) {
|
||||
if (!token) {
|
||||
callback(false);
|
||||
}
|
||||
else {
|
||||
self.getUserId(function (userId) {
|
||||
callback(userId === null);
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user