1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-12-21 16:18:28 +01:00

move api services to background page. wire up log in and log out.

This commit is contained in:
Kyle Spearrin 2016-09-03 15:44:32 -04:00
parent c3053ea3a7
commit 79860da28c
11 changed files with 90 additions and 61 deletions

View File

@ -12,9 +12,12 @@ var FolderRequest = function (folder) {
this.name = folder.name ? folder.name.encryptedString : null;
};
var TokenRequest = function () {
this.email = null;
this.masterPasswordHash = null;
var TokenRequest = function (email, masterPasswordHash, device) {
this.email = email;
this.masterPasswordHash = masterPasswordHash;
if (device) {
this.device = new DeviceRequest(device);
}
this.device = null;
};

View File

@ -51,8 +51,9 @@ var ListResponse = function (data) {
};
var ErrorResponse = function (response) {
this.message = response.Message;
this.validationErrors = response.ValidationErrors;
this.message = response.responseJSON.Message;
this.validationErrors = response.responseJSON.ValidationErrors;
this.statusCode = response.status;
};
var DeviceResponse = function (response) {

View File

@ -7,7 +7,7 @@
$scope.loginPromise.then(function () {
userService.getUserProfile(function (profile) {
if (profile.twoFactor) {
if (false && profile.twoFactor) {
$state.go('login.twoFactor');
}
else {

View File

@ -8,7 +8,7 @@
<ion-tab title="Tools" icon="ion-settings" ui-sref="tabs.tools">
<ion-nav-view name="tools-tab"></ion-nav-view>
</ion-tab>
<ion-tab title="Settings" icon="ion-gear-b" ui-sref="tabs.settings">
<ion-tab title="Settings" icon="ion-gear-a" ui-sref="tabs.settings">
<ion-nav-view name="settings-tab"></ion-nav-view>
</ion-tab>
</ion-tabs>

View File

@ -1,32 +0,0 @@
angular
.module('bit.services')
.factory('apiService', function ($resource, appSettings) {
var _service = {},
_apiUri = appSettings.apiUri;
_service.sites = $resource(_apiUri + '/sites/:id', {}, {
post: { method: 'POST', params: {} },
put: { method: 'POST', params: { id: '@id' } },
del: { url: _apiUri + '/sites/:id/delete', method: 'POST', params: { id: '@id' } }
});
_service.folders = $resource(_apiUri + '/folders/:id', {}, {
post: { method: 'POST', params: {} },
put: { method: 'POST', params: { id: '@id' } },
del: { url: _apiUri + '/folders/:id/delete', method: 'POST', params: { id: '@id' } }
});
_service.accounts = $resource(_apiUri + '/accounts', {}, {
register: { url: _apiUri + '/accounts/register', method: 'POST', params: {} },
getProfile: { url: _apiUri + '/accounts/profile', method: 'GET', params: {} },
postPasswordHint: { url: _apiUri + '/accounts/password-hint', method: 'POST', params: {} }
});
_service.auth = $resource(_apiUri + '/auth', {}, {
token: { url: _apiUri + '/auth/token', method: 'POST', params: {} },
tokenTwoFactor: { url: _apiUri + '/auth/token/two-factor', method: 'POST', params: {} }
});
return _service;
});

View File

@ -9,4 +9,7 @@
})
.factory('userService', function () {
return chrome.extension.getBackgroundPage().userService;
})
.factory('apiService', function () {
return chrome.extension.getBackgroundPage().apiService;
});

View File

@ -1,26 +1,22 @@
angular
.module('bit.services')
.factory('loginService', function (cryptoService, apiService, userService, tokenService, $q) {
.factory('loginService', function (cryptoService, apiService, apiService, userService, tokenService, $q) {
var _service = {};
_service.logIn = function (email, masterPassword) {
var key = cryptoService.makeKey(masterPassword, email);
var request = {
email: email,
masterPasswordHash: cryptoService.hashPassword(masterPassword, key)
};
var request = new TokenRequest(email, cryptoService.hashPassword(masterPassword, key));
var deferred = $q.defer();
apiService.auth.token(request, function (response) {
if (!response || !response.Token) {
apiService.postToken(request, function (response) {
if (!response || !response.token) {
return;
}
tokenService.setToken(response.Token, function () {
tokenService.setToken(response.token, function () {
cryptoService.setKey(key, function () {
userService.setUserProfile(response.Profile, function () {
userService.setUserProfile(response.profile, function () {
deferred.resolve(response);
});
});
@ -56,10 +52,11 @@
return deferred.promise;
};
_service.logOut = function () {
_service.logOut = function (callback) {
tokenService.clearToken(function () {
cryptoService.clearKey(function () {
userService.clearUserProfile();
callback();
});
});
};

View File

@ -1,6 +1,10 @@
angular
.module('bit.settings')
.controller('settingsController', function ($scope) {
.controller('settingsController', function ($scope, loginService, $state) {
$scope.logOut = function (model) {
loginService.logOut(function () {
$state.go('login');
});
};
});

View File

@ -1,7 +1,15 @@
<ion-view view-title="Settings">
<ion-content class="padding">
<p>
Some content for your settings.
</p>
<ion-content>
<div class="list">
<div class="item item-divider">
Current Session
</div>
<a class="item" href="#">
Lock
</a>
<a class="item" ng-click="logOut()">
Log out
</a>
</div>
</ion-content>
</ion-view>

View File

@ -14,9 +14,13 @@
<script src="app/settings.js"></script>
<script src="app/config.js"></script>
<script src="../models/api/requestModels.js"></script>
<script src="../models/api/responseModels.js"></script>
<script src="../models/dataModels.js"></script>
<script src="../models/domainModels.js"></script>
<script src="app/services/servicesModule.js"></script>
<script src="app/services/backgroundService.js"></script>
<script src="app/services/apiService.js"></script>
<script src="app/services/loginService.js"></script>
<script src="app/accounts/accountsModule.js"></script>

View File

@ -4,6 +4,42 @@
};
!function () {
// Auth APIs
ApiService.prototype.postToken = function (tokenRequest, success, error) {
var self = this;
$.ajax({
type: 'POST',
url: self.baseUrl + '/auth/token',
data: JSON.stringify(tokenRequest),
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.postTokenTwoFactor = function (twoFactorTokenRequest, success, error) {
var self = this;
$.ajax({
type: 'POST',
url: self.baseUrl + '/auth/token/two-factor',
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);
}
});
};
// Account APIs
ApiService.prototype.getProfile = function (success, error) {
@ -48,7 +84,8 @@
$.ajax({
type: 'POST',
url: self.baseUrl + '/sites?access_token=' + token,
data: siteRequest,
data: JSON.stringify(siteRequest),
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (response) {
success(new SiteResponse(response))
@ -66,7 +103,8 @@
$.ajax({
type: 'POST',
url: self.baseUrl + '/sites/' + id + '?access_token=' + token,
data: siteRequest,
data: JSON.stringify(siteRequest),
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (response) {
success(new SiteResponse(response))
@ -103,7 +141,8 @@
$.ajax({
type: 'POST',
url: self.baseUrl + '/folders?access_token=' + token,
data: folderRequest,
data: JSON.stringify(folderRequest),
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (response) {
success(new FolderResponse(response))
@ -121,7 +160,8 @@
$.ajax({
type: 'POST',
url: self.baseUrl + '/folders/' + id + '?access_token=' + token,
data: folderRequest,
data: JSON.stringify(folderRequest),
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (response) {
success(new FolderResponse(response))
@ -180,6 +220,7 @@
$.ajax({
type: 'POST',
url: self.baseUrl + '/ciphers/' + id + '/delete?access_token=' + token,
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: success,
error: function (jqXHR, textStatus, errorThrown) {