1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-09 09:51:02 +01:00

promisify makekeypair and generate keys on login

This commit is contained in:
Kyle Spearrin 2017-04-13 18:18:32 -04:00
parent 2228263b9f
commit e4ffdf6815
4 changed files with 49 additions and 20 deletions

View File

@ -32,27 +32,30 @@ angular
var email = $scope.model.email.toLowerCase(); var email = $scope.model.email.toLowerCase();
var key = cryptoService.makeKey($scope.model.masterPassword, email); var key = cryptoService.makeKey($scope.model.masterPassword, email);
cryptoService.makeKeyPair(key, function (publicKey, privateKeyEnc, errors) {
if (errors) {
validationService.addError(form, null, 'Problem generating keys.', true);
return;
}
$scope.registerPromise = cryptoService.makeKeyPair(key).then(function (result) {
var request = { var request = {
name: $scope.model.name, name: $scope.model.name,
email: email, email: email,
masterPasswordHash: cryptoService.hashPassword($scope.model.masterPassword, key), masterPasswordHash: cryptoService.hashPassword($scope.model.masterPassword, key),
masterPasswordHint: $scope.model.masterPasswordHint, masterPasswordHint: $scope.model.masterPasswordHint,
keys: { keys: {
publicKey: publicKey, publicKey: result.publicKey,
encryptedPrivateKey: privateKeyEnc encryptedPrivateKey: result.privateKeyEnc
} }
}; };
$scope.registerPromise = apiService.accounts.register(request, function () { return apiService.accounts.register(request);
$scope.success = true; }, function (errors) {
$analytics.eventTrack('Registered'); validationService.addError(form, null, 'Problem generating keys.', true);
}).$promise; return false;
}).then(function (result) {
if (result === false) {
return;
}
$scope.success = true;
$analytics.eventTrack('Registered');
}); });
}; };
}); });

View File

@ -88,6 +88,7 @@
postTwoFactorRecover: { url: _apiUri + '/accounts/two-factor-recover', method: 'POST', params: {} }, postTwoFactorRecover: { url: _apiUri + '/accounts/two-factor-recover', method: 'POST', params: {} },
postPasswordHint: { url: _apiUri + '/accounts/password-hint', method: 'POST', params: {} }, postPasswordHint: { url: _apiUri + '/accounts/password-hint', method: 'POST', params: {} },
putSecurityStamp: { url: _apiUri + '/accounts/security-stamp', method: 'POST', params: {} }, putSecurityStamp: { url: _apiUri + '/accounts/security-stamp', method: 'POST', params: {} },
putKeys: { url: _apiUri + '/accounts/keys', method: 'POST', params: {} },
'import': { url: _apiUri + '/accounts/import', method: 'POST', params: {} }, 'import': { url: _apiUri + '/accounts/import', method: 'POST', params: {} },
postDelete: { url: _apiUri + '/accounts/delete', method: 'POST', params: {} } postDelete: { url: _apiUri + '/accounts/delete', method: 'POST', params: {} }
}); });

View File

@ -25,7 +25,8 @@ angular
// TODO: device information one day? // TODO: device information one day?
var deferred = $q.defer(); var deferred = $q.defer();
apiService.identity.token(request, function (response) {
apiService.identity.token(request).$promise.then(function (response) {
if (!response || !response.access_token) { if (!response || !response.access_token) {
return; return;
} }
@ -33,14 +34,31 @@ angular
tokenService.setToken(response.access_token); tokenService.setToken(response.access_token);
tokenService.setRefreshToken(response.refresh_token); tokenService.setRefreshToken(response.refresh_token);
cryptoService.setKey(key); cryptoService.setKey(key);
if (response.PrivateKey) { if (response.PrivateKey) {
cryptoService.setPrivateKey(response.PrivateKey, key); cryptoService.setPrivateKey(response.PrivateKey, key);
return true;
}
else {
return cryptoService.makeKeyPair(key);
}
}).then(function (keyResults) {
if (keyResults === true) {
return;
} }
_service.setUserProfile().then(function () { cryptoService.setPrivateKey(keyResults.privateKeyEnc, key);
deferred.resolve(); return apiService.accounts.putKeys({
}); publicKey: keyResults.publicKey,
encryptedPrivateKey: keyResults.privateKeyEnc
}).$promise;
}).then(function () {
return _service.setUserProfile();
}).then(function () {
deferred.resolve();
}, function (error) { }, function (error) {
_service.logOut();
if (error.status === 400 && error.data.TwoFactorProviders && error.data.TwoFactorProviders.length) { if (error.status === 400 && error.data.TwoFactorProviders && error.data.TwoFactorProviders.length) {
deferred.resolve(error.data.TwoFactorProviders); deferred.resolve(error.data.TwoFactorProviders);
} }

View File

@ -1,7 +1,7 @@
angular angular
.module('bit.services') .module('bit.services')
.factory('cryptoService', function ($sessionStorage, constants) { .factory('cryptoService', function ($sessionStorage, constants, $q) {
var _service = {}, var _service = {},
_key, _key,
_b64Key, _b64Key,
@ -220,23 +220,30 @@ angular
return key; return key;
}; };
_service.makeKeyPair = function (key, callback) { _service.makeKeyPair = function (key) {
var deferred = $q.defer();
forge.pki.rsa.generateKeyPair({ bits: 2048, workers: 2 }, function (error, keypair) { forge.pki.rsa.generateKeyPair({ bits: 2048, workers: 2 }, function (error, keypair) {
if (error) { if (error) {
callback(null, null, error); deferred.reject(error);
return; return;
} }
var privateKeyAsn1 = forge.pki.privateKeyToAsn1(keypair.privateKey); var privateKeyAsn1 = forge.pki.privateKeyToAsn1(keypair.privateKey);
var privateKeyPkcs8 = forge.pki.wrapRsaPrivateKey(privateKeyAsn1); var privateKeyPkcs8 = forge.pki.wrapRsaPrivateKey(privateKeyAsn1);
var privateKeyBytes = forge.asn1.toDer(privateKeyPkcs8).getBytes(); var privateKeyBytes = forge.asn1.toDer(privateKeyPkcs8).getBytes();
var privateKeyEncBytes = _service.encrypt(privateKeyBytes, key, 'raw'); var privateKeyEncCt = _service.encrypt(privateKeyBytes, key, 'raw');
var publicKeyAsn1 = forge.pki.publicKeyToAsn1(keypair.publicKey); var publicKeyAsn1 = forge.pki.publicKeyToAsn1(keypair.publicKey);
var publicKeyBytes = forge.asn1.toDer(publicKeyAsn1).getBytes(); var publicKeyBytes = forge.asn1.toDer(publicKeyAsn1).getBytes();
callback(forge.util.encode64(publicKeyBytes), privateKeyEncBytes, null); deferred.resolve({
publicKey: forge.util.encode64(publicKeyBytes),
privateKeyEnc: privateKeyEncCt
});
}); });
return deferred.promise;
}; };
_service.makeShareKey = function () { _service.makeShareKey = function () {