1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-12-04 13:44:00 +01:00
bitwarden-browser/src/app/services/authService.js

252 lines
9.2 KiB
JavaScript
Raw Normal View History

2015-12-09 04:35:05 +01:00
angular
.module('bit.services')
2017-06-23 16:41:57 +02:00
.factory('authService', function (cryptoService, apiService, tokenService, $q, jwtHelper, $rootScope, constants) {
2015-12-09 04:35:05 +01:00
var _service = {},
_userProfile = null;
2017-06-23 16:41:57 +02:00
_service.logIn = function (email, masterPassword, token, provider, remember) {
2016-10-11 04:40:44 +02:00
email = email.toLowerCase();
2015-12-09 04:35:05 +01:00
var deferred = $q.defer();
2015-12-09 04:35:05 +01:00
var makeResult;
cryptoService.makeKeyAndHash(email, masterPassword).then(function (result) {
makeResult = result;
2017-06-23 16:41:57 +02:00
var request = {
username: email,
password: result.hash,
grant_type: 'password',
scope: 'api offline_access',
client_id: 'web'
};
2015-12-09 04:35:05 +01:00
// TODO: device information one day?
2015-12-09 04:35:05 +01:00
if (token && typeof (provider) !== 'undefined' && provider !== null) {
remember = remember || remember !== false;
request.twoFactorToken = token;
request.twoFactorProvider = provider;
request.twoFactorRemember = remember ? '1' : '0';
}
else if (tokenService.getTwoFactorToken(email)) {
request.twoFactorToken = tokenService.getTwoFactorToken(email);
request.twoFactorProvider = constants.twoFactorProvider.remember;
request.twoFactorRemember = '0';
}
return apiService.identity.token(request).$promise;
}).then(function (response) {
if (!response || !response.access_token) {
2015-12-09 04:35:05 +01:00
return;
}
tokenService.setToken(response.access_token);
tokenService.setRefreshToken(response.refresh_token);
cryptoService.setKey(makeResult.key);
2017-06-23 16:41:57 +02:00
if (response.TwoFactorToken) {
tokenService.setTwoFactorToken(response.TwoFactorToken, email);
}
if (response.Key) {
cryptoService.setEncKey(response.Key, makeResult.key);
}
2017-03-25 16:03:11 +01:00
if (response.PrivateKey) {
2017-05-31 17:05:52 +02:00
cryptoService.setPrivateKey(response.PrivateKey);
return true;
}
else {
2017-05-31 17:05:52 +02:00
return cryptoService.makeKeyPair();
}
}).then(function (keyResults) {
if (keyResults === true) {
return;
2017-02-21 06:29:15 +01:00
}
2017-03-25 16:03:11 +01:00
2017-05-31 17:05:52 +02:00
cryptoService.setPrivateKey(keyResults.privateKeyEnc);
return apiService.accounts.putKeys({
publicKey: keyResults.publicKey,
encryptedPrivateKey: keyResults.privateKeyEnc
}).$promise;
}).then(function () {
return _service.setUserProfile();
}).then(function () {
deferred.resolve();
2015-12-09 04:35:05 +01:00
}, function (error) {
_service.logOut();
2017-06-20 23:06:14 +02:00
if (error.status === 400 && error.data.TwoFactorProviders2 &&
Object.keys(error.data.TwoFactorProviders2).length) {
2017-06-23 16:41:57 +02:00
tokenService.clearTwoFactorToken(email);
2017-06-20 23:06:14 +02:00
deferred.resolve(error.data.TwoFactorProviders2);
}
else {
deferred.reject(error);
}
2015-12-09 04:35:05 +01:00
});
return deferred.promise;
};
_service.logOut = function () {
2017-06-23 16:41:57 +02:00
tokenService.clearTokens();
cryptoService.clearKeys();
2017-10-07 04:01:17 +02:00
$rootScope.vaultFolders = $rootScope.vaultCiphers = null;
2015-12-09 04:35:05 +01:00
_userProfile = null;
};
_service.getUserProfile = function () {
if (!_userProfile) {
return _service.setUserProfile();
2015-12-09 04:35:05 +01:00
}
var deferred = $q.defer();
deferred.resolve(_userProfile);
return deferred.promise;
2015-12-09 04:35:05 +01:00
};
var _setDeferred = null;
_service.setUserProfile = function () {
if (_setDeferred && _setDeferred.promise.$$state.status === 0) {
return _setDeferred.promise;
}
_setDeferred = $q.defer();
2015-12-09 04:35:05 +01:00
var token = tokenService.getToken();
if (!token) {
_setDeferred.reject();
return _setDeferred.promise;
2015-12-09 04:35:05 +01:00
}
apiService.accounts.getProfile({}, function (profile) {
_userProfile = {
id: profile.Id,
email: profile.Email,
emailVerified: profile.EmailVerified,
premium: profile.Premium,
extended: {
name: profile.Name,
twoFactorEnabled: profile.TwoFactorEnabled,
culture: profile.Culture
}
};
2015-12-09 04:35:05 +01:00
if (profile.Organizations) {
2017-03-28 03:55:39 +02:00
var orgs = {};
for (var i = 0; i < profile.Organizations.length; i++) {
2017-03-28 03:55:39 +02:00
orgs[profile.Organizations[i].Id] = {
id: profile.Organizations[i].Id,
name: profile.Organizations[i].Name,
key: profile.Organizations[i].Key,
status: profile.Organizations[i].Status,
2017-04-11 21:56:57 +02:00
type: profile.Organizations[i].Type,
2017-05-08 21:28:40 +02:00
enabled: profile.Organizations[i].Enabled,
maxCollections: profile.Organizations[i].MaxCollections,
2017-07-07 18:12:08 +02:00
maxStorageGb: profile.Organizations[i].MaxStorageGb,
2017-05-08 21:28:40 +02:00
seats: profile.Organizations[i].Seats,
2017-07-07 18:12:08 +02:00
useGroups: profile.Organizations[i].UseGroups,
useTotp: profile.Organizations[i].UseTotp
2017-03-28 03:55:39 +02:00
};
}
_userProfile.organizations = orgs;
cryptoService.setOrgKeys(orgs);
_setDeferred.resolve(_userProfile);
2017-03-12 02:46:33 +01:00
}
2017-10-13 05:35:58 +02:00
}, function (error) {
_setDeferred.reject(error);
});
2017-03-12 02:46:33 +01:00
return _setDeferred.promise;
2017-03-12 02:46:33 +01:00
};
2017-04-19 04:28:49 +02:00
_service.addProfileOrganizationOwner = function (org, keyCt) {
return _service.getUserProfile().then(function (profile) {
if (profile) {
2017-03-28 03:55:39 +02:00
if (!profile.organizations) {
profile.organizations = {};
}
var o = {
id: org.Id,
name: org.Name,
2017-04-19 04:28:49 +02:00
key: keyCt,
status: 2, // 2 = Confirmed
2017-04-11 21:56:57 +02:00
type: 0, // 0 = Owner
2017-05-08 21:28:40 +02:00
enabled: true,
maxCollections: org.MaxCollections,
2017-07-07 18:12:08 +02:00
maxStorageGb: org.MaxStorageGb,
2017-05-08 21:28:40 +02:00
seats: org.Seats,
2017-07-07 18:12:08 +02:00
useGroups: org.UseGroups,
useTotp: org.UseTotp
};
2017-03-28 03:55:39 +02:00
profile.organizations[o.id] = o;
_userProfile = profile;
cryptoService.addOrgKey(o.id, o.key);
2017-03-04 03:53:02 +01:00
}
});
};
2015-12-09 04:35:05 +01:00
2017-04-11 16:52:16 +02:00
_service.removeProfileOrganization = function (orgId) {
return _service.getUserProfile().then(function (profile) {
if (profile) {
if (profile.organizations && profile.organizations.hasOwnProperty(orgId)) {
delete profile.organizations[orgId];
_userProfile = profile;
}
cryptoService.clearOrgKey(orgId);
}
});
};
2017-04-06 22:52:25 +02:00
_service.updateProfileOrganization = function (org) {
return _service.getUserProfile().then(function (profile) {
if (profile) {
if (profile.organizations && org.Id in profile.organizations) {
profile.organizations[org.Id].name = org.Name;
_userProfile = profile;
}
}
});
};
2017-07-11 16:24:46 +02:00
_service.updateProfilePremium = function (isPremium) {
return _service.getUserProfile().then(function (profile) {
if (profile) {
profile.premium = isPremium;
_userProfile = profile;
}
});
};
2015-12-09 04:35:05 +01:00
_service.isAuthenticated = function () {
return tokenService.getToken() !== null;
2015-12-09 04:35:05 +01:00
};
_service.refreshAccessToken = function () {
var refreshToken = tokenService.getRefreshToken();
if (!refreshToken) {
return null;
}
return apiService.identity.token({
grant_type: 'refresh_token',
client_id: 'web',
refresh_token: refreshToken
}).$promise.then(function (response) {
tokenService.setToken(response.access_token);
tokenService.setRefreshToken(response.refresh_token);
return response.access_token;
2017-05-10 17:47:53 +02:00
}, function (response) { });
2017-04-12 22:14:29 +02:00
};
2015-12-09 04:35:05 +01:00
return _service;
});