mirror of
https://github.com/bitwarden/browser
synced 2025-12-12 14:23:32 +00:00
move api services to background page. wire up log in and log out.
This commit is contained in:
@@ -12,9 +12,12 @@ var FolderRequest = function (folder) {
|
|||||||
this.name = folder.name ? folder.name.encryptedString : null;
|
this.name = folder.name ? folder.name.encryptedString : null;
|
||||||
};
|
};
|
||||||
|
|
||||||
var TokenRequest = function () {
|
var TokenRequest = function (email, masterPasswordHash, device) {
|
||||||
this.email = null;
|
this.email = email;
|
||||||
this.masterPasswordHash = null;
|
this.masterPasswordHash = masterPasswordHash;
|
||||||
|
if (device) {
|
||||||
|
this.device = new DeviceRequest(device);
|
||||||
|
}
|
||||||
this.device = null;
|
this.device = null;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -51,8 +51,9 @@ var ListResponse = function (data) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
var ErrorResponse = function (response) {
|
var ErrorResponse = function (response) {
|
||||||
this.message = response.Message;
|
this.message = response.responseJSON.Message;
|
||||||
this.validationErrors = response.ValidationErrors;
|
this.validationErrors = response.responseJSON.ValidationErrors;
|
||||||
|
this.statusCode = response.status;
|
||||||
};
|
};
|
||||||
|
|
||||||
var DeviceResponse = function (response) {
|
var DeviceResponse = function (response) {
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
$scope.loginPromise.then(function () {
|
$scope.loginPromise.then(function () {
|
||||||
userService.getUserProfile(function (profile) {
|
userService.getUserProfile(function (profile) {
|
||||||
if (profile.twoFactor) {
|
if (false && profile.twoFactor) {
|
||||||
$state.go('login.twoFactor');
|
$state.go('login.twoFactor');
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
<ion-tab title="Tools" icon="ion-settings" ui-sref="tabs.tools">
|
<ion-tab title="Tools" icon="ion-settings" ui-sref="tabs.tools">
|
||||||
<ion-nav-view name="tools-tab"></ion-nav-view>
|
<ion-nav-view name="tools-tab"></ion-nav-view>
|
||||||
</ion-tab>
|
</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-nav-view name="settings-tab"></ion-nav-view>
|
||||||
</ion-tab>
|
</ion-tab>
|
||||||
</ion-tabs>
|
</ion-tabs>
|
||||||
|
|||||||
@@ -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;
|
|
||||||
});
|
|
||||||
@@ -9,4 +9,7 @@
|
|||||||
})
|
})
|
||||||
.factory('userService', function () {
|
.factory('userService', function () {
|
||||||
return chrome.extension.getBackgroundPage().userService;
|
return chrome.extension.getBackgroundPage().userService;
|
||||||
|
})
|
||||||
|
.factory('apiService', function () {
|
||||||
|
return chrome.extension.getBackgroundPage().apiService;
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,26 +1,22 @@
|
|||||||
angular
|
angular
|
||||||
.module('bit.services')
|
.module('bit.services')
|
||||||
|
|
||||||
.factory('loginService', function (cryptoService, apiService, userService, tokenService, $q) {
|
.factory('loginService', function (cryptoService, apiService, apiService, userService, tokenService, $q) {
|
||||||
var _service = {};
|
var _service = {};
|
||||||
|
|
||||||
_service.logIn = function (email, masterPassword) {
|
_service.logIn = function (email, masterPassword) {
|
||||||
var key = cryptoService.makeKey(masterPassword, email);
|
var key = cryptoService.makeKey(masterPassword, email);
|
||||||
|
var request = new TokenRequest(email, cryptoService.hashPassword(masterPassword, key));
|
||||||
var request = {
|
|
||||||
email: email,
|
|
||||||
masterPasswordHash: cryptoService.hashPassword(masterPassword, key)
|
|
||||||
};
|
|
||||||
|
|
||||||
var deferred = $q.defer();
|
var deferred = $q.defer();
|
||||||
apiService.auth.token(request, function (response) {
|
apiService.postToken(request, function (response) {
|
||||||
if (!response || !response.Token) {
|
if (!response || !response.token) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
tokenService.setToken(response.Token, function () {
|
tokenService.setToken(response.token, function () {
|
||||||
cryptoService.setKey(key, function () {
|
cryptoService.setKey(key, function () {
|
||||||
userService.setUserProfile(response.Profile, function () {
|
userService.setUserProfile(response.profile, function () {
|
||||||
deferred.resolve(response);
|
deferred.resolve(response);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -56,10 +52,11 @@
|
|||||||
return deferred.promise;
|
return deferred.promise;
|
||||||
};
|
};
|
||||||
|
|
||||||
_service.logOut = function () {
|
_service.logOut = function (callback) {
|
||||||
tokenService.clearToken(function () {
|
tokenService.clearToken(function () {
|
||||||
cryptoService.clearKey(function () {
|
cryptoService.clearKey(function () {
|
||||||
userService.clearUserProfile();
|
userService.clearUserProfile();
|
||||||
|
callback();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
angular
|
angular
|
||||||
.module('bit.settings')
|
.module('bit.settings')
|
||||||
|
|
||||||
.controller('settingsController', function ($scope) {
|
.controller('settingsController', function ($scope, loginService, $state) {
|
||||||
|
$scope.logOut = function (model) {
|
||||||
|
loginService.logOut(function () {
|
||||||
|
$state.go('login');
|
||||||
|
});
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,7 +1,15 @@
|
|||||||
<ion-view view-title="Settings">
|
<ion-view view-title="Settings">
|
||||||
<ion-content class="padding">
|
<ion-content>
|
||||||
<p>
|
<div class="list">
|
||||||
Some content for your settings.
|
<div class="item item-divider">
|
||||||
</p>
|
Current Session
|
||||||
|
</div>
|
||||||
|
<a class="item" href="#">
|
||||||
|
Lock
|
||||||
|
</a>
|
||||||
|
<a class="item" ng-click="logOut()">
|
||||||
|
Log out
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
</ion-content>
|
</ion-content>
|
||||||
</ion-view>
|
</ion-view>
|
||||||
|
|||||||
@@ -14,9 +14,13 @@
|
|||||||
<script src="app/settings.js"></script>
|
<script src="app/settings.js"></script>
|
||||||
<script src="app/config.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/servicesModule.js"></script>
|
||||||
<script src="app/services/backgroundService.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/services/loginService.js"></script>
|
||||||
|
|
||||||
<script src="app/accounts/accountsModule.js"></script>
|
<script src="app/accounts/accountsModule.js"></script>
|
||||||
|
|||||||
@@ -4,6 +4,42 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
!function () {
|
!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
|
// Account APIs
|
||||||
|
|
||||||
ApiService.prototype.getProfile = function (success, error) {
|
ApiService.prototype.getProfile = function (success, error) {
|
||||||
@@ -48,7 +84,8 @@
|
|||||||
$.ajax({
|
$.ajax({
|
||||||
type: 'POST',
|
type: 'POST',
|
||||||
url: self.baseUrl + '/sites?access_token=' + token,
|
url: self.baseUrl + '/sites?access_token=' + token,
|
||||||
data: siteRequest,
|
data: JSON.stringify(siteRequest),
|
||||||
|
contentType: "application/json; charset=utf-8",
|
||||||
dataType: 'json',
|
dataType: 'json',
|
||||||
success: function (response) {
|
success: function (response) {
|
||||||
success(new SiteResponse(response))
|
success(new SiteResponse(response))
|
||||||
@@ -66,7 +103,8 @@
|
|||||||
$.ajax({
|
$.ajax({
|
||||||
type: 'POST',
|
type: 'POST',
|
||||||
url: self.baseUrl + '/sites/' + id + '?access_token=' + token,
|
url: self.baseUrl + '/sites/' + id + '?access_token=' + token,
|
||||||
data: siteRequest,
|
data: JSON.stringify(siteRequest),
|
||||||
|
contentType: "application/json; charset=utf-8",
|
||||||
dataType: 'json',
|
dataType: 'json',
|
||||||
success: function (response) {
|
success: function (response) {
|
||||||
success(new SiteResponse(response))
|
success(new SiteResponse(response))
|
||||||
@@ -103,7 +141,8 @@
|
|||||||
$.ajax({
|
$.ajax({
|
||||||
type: 'POST',
|
type: 'POST',
|
||||||
url: self.baseUrl + '/folders?access_token=' + token,
|
url: self.baseUrl + '/folders?access_token=' + token,
|
||||||
data: folderRequest,
|
data: JSON.stringify(folderRequest),
|
||||||
|
contentType: "application/json; charset=utf-8",
|
||||||
dataType: 'json',
|
dataType: 'json',
|
||||||
success: function (response) {
|
success: function (response) {
|
||||||
success(new FolderResponse(response))
|
success(new FolderResponse(response))
|
||||||
@@ -121,7 +160,8 @@
|
|||||||
$.ajax({
|
$.ajax({
|
||||||
type: 'POST',
|
type: 'POST',
|
||||||
url: self.baseUrl + '/folders/' + id + '?access_token=' + token,
|
url: self.baseUrl + '/folders/' + id + '?access_token=' + token,
|
||||||
data: folderRequest,
|
data: JSON.stringify(folderRequest),
|
||||||
|
contentType: "application/json; charset=utf-8",
|
||||||
dataType: 'json',
|
dataType: 'json',
|
||||||
success: function (response) {
|
success: function (response) {
|
||||||
success(new FolderResponse(response))
|
success(new FolderResponse(response))
|
||||||
@@ -180,6 +220,7 @@
|
|||||||
$.ajax({
|
$.ajax({
|
||||||
type: 'POST',
|
type: 'POST',
|
||||||
url: self.baseUrl + '/ciphers/' + id + '/delete?access_token=' + token,
|
url: self.baseUrl + '/ciphers/' + id + '/delete?access_token=' + token,
|
||||||
|
contentType: "application/json; charset=utf-8",
|
||||||
dataType: 'json',
|
dataType: 'json',
|
||||||
success: success,
|
success: success,
|
||||||
error: function (jqXHR, textStatus, errorThrown) {
|
error: function (jqXHR, textStatus, errorThrown) {
|
||||||
|
|||||||
Reference in New Issue
Block a user