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
|
|
|
|
2017-07-09 05:41:02 +02:00
|
|
|
var deferred = $q.defer();
|
2015-12-09 04:35:05 +01:00
|
|
|
|
2017-07-09 05:41:02 +02:00
|
|
|
var makeResult;
|
|
|
|
cryptoService.makeKeyAndHash(email, masterPassword).then(function (result) {
|
|
|
|
makeResult = result;
|
2017-06-23 16:41:57 +02:00
|
|
|
|
2017-07-09 05:41:02 +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
|
|
|
|
2017-07-09 05:41:02 +02:00
|
|
|
// TODO: device information one day?
|
2015-12-09 04:35:05 +01:00
|
|
|
|
2017-07-09 05:41:02 +02: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';
|
|
|
|
}
|
2017-04-14 00:18:32 +02:00
|
|
|
|
2017-07-09 05:41:02 +02:00
|
|
|
return apiService.identity.token(request).$promise;
|
|
|
|
}).then(function (response) {
|
2017-01-28 07:19:43 +01:00
|
|
|
if (!response || !response.access_token) {
|
2015-12-09 04:35:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-28 07:19:43 +01:00
|
|
|
tokenService.setToken(response.access_token);
|
|
|
|
tokenService.setRefreshToken(response.refresh_token);
|
2017-07-09 05:41:02 +02:00
|
|
|
cryptoService.setKey(makeResult.key);
|
2017-04-14 00:18:32 +02:00
|
|
|
|
2017-06-23 16:41:57 +02:00
|
|
|
if (response.TwoFactorToken) {
|
|
|
|
tokenService.setTwoFactorToken(response.TwoFactorToken, email);
|
|
|
|
}
|
|
|
|
|
2017-05-31 16:25:25 +02:00
|
|
|
if (response.Key) {
|
2017-07-09 05:41:02 +02:00
|
|
|
cryptoService.setEncKey(response.Key, makeResult.key);
|
2017-05-31 16:25:25 +02:00
|
|
|
}
|
|
|
|
|
2017-03-25 16:03:11 +01:00
|
|
|
if (response.PrivateKey) {
|
2017-05-31 17:05:52 +02:00
|
|
|
cryptoService.setPrivateKey(response.PrivateKey);
|
2017-04-14 00:18:32 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
2017-05-31 17:05:52 +02:00
|
|
|
return cryptoService.makeKeyPair();
|
2017-04-14 00:18:32 +02:00
|
|
|
}
|
|
|
|
}).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);
|
2017-04-14 00:18:32 +02:00
|
|
|
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) {
|
2017-04-14 00:18:32 +02:00
|
|
|
_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);
|
2017-01-28 07:19:43 +01:00
|
|
|
}
|
|
|
|
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();
|
2017-02-22 04:50:48 +01:00
|
|
|
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) {
|
2017-03-25 15:43:19 +01:00
|
|
|
return _service.setUserProfile();
|
2015-12-09 04:35:05 +01:00
|
|
|
}
|
|
|
|
|
2017-03-25 15:43:19 +01:00
|
|
|
var deferred = $q.defer();
|
|
|
|
deferred.resolve(_userProfile);
|
|
|
|
return deferred.promise;
|
2015-12-09 04:35:05 +01:00
|
|
|
};
|
|
|
|
|
2017-03-28 04:22:56 +02:00
|
|
|
var _setDeferred = null;
|
2017-01-28 07:19:43 +01:00
|
|
|
_service.setUserProfile = function () {
|
2017-03-28 04:22:56 +02:00
|
|
|
if (_setDeferred && _setDeferred.promise.$$state.status === 0) {
|
|
|
|
return _setDeferred.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
_setDeferred = $q.defer();
|
2017-03-25 15:43:19 +01:00
|
|
|
|
2015-12-09 04:35:05 +01:00
|
|
|
var token = tokenService.getToken();
|
|
|
|
if (!token) {
|
2017-03-28 04:22:56 +02:00
|
|
|
_setDeferred.reject();
|
|
|
|
return _setDeferred.promise;
|
2015-12-09 04:35:05 +01:00
|
|
|
}
|
|
|
|
|
2017-03-25 15:43:19 +01:00
|
|
|
apiService.accounts.getProfile({}, function (profile) {
|
2017-03-28 04:22:56 +02:00
|
|
|
_userProfile = {
|
2017-07-02 04:44:10 +02:00
|
|
|
id: profile.Id,
|
|
|
|
email: profile.Email,
|
|
|
|
emailVerified: profile.EmailVerified,
|
|
|
|
premium: profile.Premium,
|
2017-03-28 04:22:56 +02:00
|
|
|
extended: {
|
|
|
|
name: profile.Name,
|
|
|
|
twoFactorEnabled: profile.TwoFactorEnabled,
|
|
|
|
culture: profile.Culture
|
|
|
|
}
|
2017-03-25 15:43:19 +01:00
|
|
|
};
|
2015-12-09 04:35:05 +01:00
|
|
|
|
2017-03-25 15:43:19 +01:00
|
|
|
if (profile.Organizations) {
|
2017-03-28 03:55:39 +02:00
|
|
|
var orgs = {};
|
2017-03-25 15:43:19 +01:00
|
|
|
for (var i = 0; i < profile.Organizations.length; i++) {
|
2017-03-28 03:55:39 +02:00
|
|
|
orgs[profile.Organizations[i].Id] = {
|
2017-03-25 15:43:19 +01:00
|
|
|
id: profile.Organizations[i].Id,
|
|
|
|
name: profile.Organizations[i].Name,
|
|
|
|
key: profile.Organizations[i].Key,
|
2017-03-26 03:52:27 +02:00
|
|
|
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
|
|
|
};
|
2017-03-25 15:43:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_userProfile.organizations = orgs;
|
|
|
|
cryptoService.setOrgKeys(orgs);
|
2017-03-28 04:22:56 +02:00
|
|
|
_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-25 15:43:19 +01:00
|
|
|
});
|
2017-03-12 02:46:33 +01:00
|
|
|
|
2017-03-28 04:22:56 +02: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) {
|
2017-03-25 15:43:19 +01:00
|
|
|
return _service.getUserProfile().then(function (profile) {
|
|
|
|
if (profile) {
|
2017-03-28 03:55:39 +02:00
|
|
|
if (!profile.organizations) {
|
|
|
|
profile.organizations = {};
|
2017-03-25 15:43:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var o = {
|
|
|
|
id: org.Id,
|
|
|
|
name: org.Name,
|
2017-04-19 04:28:49 +02:00
|
|
|
key: keyCt,
|
2017-03-26 03:52:27 +02:00
|
|
|
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-25 15:43:19 +01:00
|
|
|
};
|
2017-03-28 03:55:39 +02:00
|
|
|
profile.organizations[o.id] = o;
|
2017-03-25 15:43:19 +01:00
|
|
|
|
|
|
|
_userProfile = profile;
|
2017-03-26 03:52:27 +02:00
|
|
|
cryptoService.addOrgKey(o.id, o.key);
|
2017-03-04 03:53:02 +01:00
|
|
|
}
|
2017-03-25 15:43:19 +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 () {
|
2017-01-28 07:19:43 +01:00
|
|
|
return tokenService.getToken() !== null;
|
2015-12-09 04:35:05 +01:00
|
|
|
};
|
|
|
|
|
2017-04-11 21:00:53 +02: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
|
|
|
};
|
2017-04-11 21:00:53 +02:00
|
|
|
|
2015-12-09 04:35:05 +01:00
|
|
|
return _service;
|
|
|
|
});
|