1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-19 07:35:48 +02:00
bitwarden-browser/src/services/apiService.js

594 lines
20 KiB
JavaScript
Raw Normal View History

2017-08-22 18:00:59 +02:00
function ApiService(tokenService, appIdService, utilsService, constantsService, logoutCallback) {
2016-09-03 06:38:27 +02:00
this.tokenService = tokenService;
this.logoutCallback = logoutCallback;
this.appIdService = appIdService;
this.utilsService = utilsService;
2017-08-22 18:00:59 +02:00
this.constantsService = constantsService;
this.urlsSet = false;
this.baseUrl = null;
this.identityBaseUrl = null;
initApiService();
2017-07-14 21:34:05 +02:00
}
2016-09-03 06:38:27 +02:00
function initApiService() {
ApiService.prototype.setUrls = function (urls) {
var self = this;
self.urlsSet = true;
if (urls.base) {
self.baseUrl = urls.base + '/api';
self.identityBaseUrl = urls.base + '/identity';
return;
2017-08-22 18:00:59 +02:00
}
if (urls.api && urls.identity) {
self.baseUrl = urls.api;
self.identityBaseUrl = urls.identity;
return;
2017-08-22 18:00:59 +02:00
}
// Desktop
//self.baseUrl = 'http://localhost:4000';
//self.identityBaseUrl = 'http://localhost:33656';
2017-08-22 18:00:59 +02:00
// Desktop HTTPS
//self.baseUrl = 'https://localhost:44377';
//self.identityBaseUrl = 'https://localhost:44392';
2017-08-22 18:00:59 +02:00
// Desktop external
//self.baseUrl = 'http://192.168.1.4:4000';
//self.identityBaseUrl = 'http://192.168.1.4:33656';
2017-08-22 18:00:59 +02:00
// Preview
//self.baseUrl = 'https://preview-api.bitwarden.com';
//self.identityBaseUrl = 'https://preview-identity.bitwarden.com';
2017-08-22 18:00:59 +02:00
// Production
self.baseUrl = 'https://api.bitwarden.com';
self.identityBaseUrl = 'https://identity.bitwarden.com';
2017-08-22 18:00:59 +02:00
};
// Auth APIs
ApiService.prototype.postIdentityToken = function (tokenRequest, success, successWithTwoFactor, error) {
2017-01-18 05:07:46 +01:00
var self = this;
2017-09-06 21:52:06 +02:00
// Hack for Edge. For some reason tokenRequest loses proto. Rebuild it here.
tokenRequest = new TokenRequest(tokenRequest.email, tokenRequest.masterPasswordHash, tokenRequest.provider,
tokenRequest.token, tokenRequest.remeber, tokenRequest.device);
2017-01-18 05:07:46 +01:00
$.ajax({
type: 'POST',
url: self.identityBaseUrl + '/connect/token',
2017-01-18 05:07:46 +01:00
data: tokenRequest.toIdentityToken(),
contentType: 'application/x-www-form-urlencoded; charset=utf-8',
dataType: 'json',
success: function (response) {
success(new IdentityTokenResponse(response));
},
error: function (jqXHR, textStatus, errorThrown) {
if (jqXHR.responseJSON && jqXHR.responseJSON.TwoFactorProviders2 &&
Object.keys(jqXHR.responseJSON.TwoFactorProviders2).length) {
2017-06-27 04:24:10 +02:00
self.tokenService.clearTwoFactorToken(tokenRequest.email, function () {
successWithTwoFactor(jqXHR.responseJSON.TwoFactorProviders2);
});
}
else {
error(new ErrorResponse(jqXHR, true));
}
2017-01-18 05:07:46 +01:00
}
});
};
2017-07-14 04:33:34 +02:00
ApiService.prototype.refreshIdentityToken = function (success, error) {
2017-09-08 05:56:05 +02:00
doRefreshToken(this, function () {
2017-07-14 04:33:34 +02:00
success();
}, function (jqXHR) {
if (jqXHR) {
handleError(error, jqXHR, false, self);
return;
}
error();
});
};
// Two Factor APIs
2017-06-27 04:24:10 +02:00
ApiService.prototype.postTwoFactorEmail = function (request, success, error) {
var self = this;
$.ajax({
type: 'POST',
2017-06-27 04:24:10 +02:00
url: self.baseUrl + '/two-factor/send-email-login',
dataType: 'text',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(request),
success: function (response) {
success(response);
},
error: function (jqXHR, textStatus, errorThrown) {
handleError(error, jqXHR, false, self);
}
});
};
2016-09-03 07:13:09 +02:00
// Account APIs
2016-09-03 18:07:30 +02:00
ApiService.prototype.getAccountRevisionDate = function (success, error) {
log('getAccountRevisionDate invoked');
var self = this;
handleTokenState(self).then(function (token) {
log('Revision Date API Call');
$.ajax({
type: 'GET',
url: self.baseUrl + '/accounts/revision-date?' + token,
dataType: 'json',
success: function (response) {
success(response);
},
error: function (jqXHR, textStatus, errorThrown) {
handleError(error, jqXHR, false, self);
}
});
}, function (jqXHR) {
log('Error handling token state for Revision Date API Call');
handleError(error, jqXHR, true, self);
});
};
2016-09-03 06:38:27 +02:00
ApiService.prototype.getProfile = function (success, error) {
var self = this;
handleTokenState(self).then(function (token) {
2016-09-03 06:38:27 +02:00
$.ajax({
type: 'GET',
url: self.baseUrl + '/accounts/profile?' + token,
2016-09-03 06:38:27 +02:00
dataType: 'json',
2016-09-03 07:13:09 +02:00
success: function (response) {
2016-09-22 20:16:24 +02:00
success(new ProfileResponse(response));
2016-09-03 07:13:09 +02:00
},
error: function (jqXHR, textStatus, errorThrown) {
handleError(error, jqXHR, false, self);
2016-09-03 07:13:09 +02:00
}
2016-09-03 06:38:27 +02:00
});
}, function (jqXHR) {
handleError(error, jqXHR, true, self);
2016-09-03 06:38:27 +02:00
});
};
2016-09-03 07:13:09 +02:00
ApiService.prototype.getKeys = function (success, error) {
var self = this;
handleTokenState(self).then(function (token) {
$.ajax({
type: 'GET',
url: self.baseUrl + '/accounts/keys?' + token,
dataType: 'json',
success: function (response) {
success(new KeysResponse(response));
},
error: function (jqXHR, textStatus, errorThrown) {
handleError(error, jqXHR, false, self);
}
});
}, function (jqXHR) {
handleError(error, jqXHR, true, self);
});
};
2016-09-20 23:47:21 +02:00
ApiService.prototype.postPasswordHint = function (request, success, error) {
var self = this;
$.ajax({
type: 'POST',
url: self.baseUrl + '/accounts/password-hint',
2016-12-29 22:09:28 +01:00
dataType: 'text',
2016-09-20 23:47:21 +02:00
data: JSON.stringify(request),
2016-09-21 01:57:24 +02:00
contentType: 'application/json; charset=utf-8',
2016-09-20 23:47:21 +02:00
success: function (response) {
success();
},
error: function (jqXHR, textStatus, errorThrown) {
handleError(error, jqXHR, false, self);
2016-09-20 23:47:21 +02:00
}
});
};
2016-09-21 01:57:24 +02:00
ApiService.prototype.postRegister = function (request, success, error) {
2016-09-20 23:47:21 +02:00
var self = this;
$.ajax({
type: 'POST',
url: self.baseUrl + '/accounts/register',
data: JSON.stringify(request),
2016-12-29 22:09:28 +01:00
dataType: 'text',
2016-09-21 01:57:24 +02:00
contentType: 'application/json; charset=utf-8',
2016-09-20 23:47:21 +02:00
success: function (response) {
success();
},
error: function (jqXHR, textStatus, errorThrown) {
handleError(error, jqXHR, false, self);
2016-09-20 23:47:21 +02:00
}
});
};
2017-01-14 17:20:44 +01:00
// Settings APIs
ApiService.prototype.getIncludedDomains = function (success, error) {
var self = this;
handleTokenState(self).then(function (token) {
2017-01-14 17:20:44 +01:00
$.ajax({
type: 'GET',
url: self.baseUrl + '/settings/domains?excluded=false&' + token,
2017-01-14 17:20:44 +01:00
dataType: 'json',
success: function (response) {
success(new DomainsResponse(response));
},
error: function (jqXHR, textStatus, errorThrown) {
handleError(error, jqXHR, false, self);
2017-01-14 17:20:44 +01:00
}
});
}, function (jqXHR) {
handleError(error, jqXHR, true, self);
2017-01-14 17:20:44 +01:00
});
};
2017-01-04 00:40:07 +01:00
// Login APIs
2016-09-03 07:13:09 +02:00
2017-01-04 00:40:07 +01:00
ApiService.prototype.getLogin = function (id, success, error) {
2016-09-03 18:07:30 +02:00
var self = this;
handleTokenState(self).then(function (token) {
2016-09-03 18:07:30 +02:00
$.ajax({
type: 'GET',
2017-06-16 18:35:46 +02:00
url: self.baseUrl + '/logins/' + id + '?' + token,
2016-09-03 18:07:30 +02:00
dataType: 'json',
success: function (response) {
2017-01-04 00:40:07 +01:00
success(new LoginResponse(response));
2016-09-03 18:07:30 +02:00
},
error: function (jqXHR, textStatus, errorThrown) {
handleError(error, jqXHR, false, self);
2016-09-03 18:07:30 +02:00
}
});
}, function (jqXHR) {
handleError(error, jqXHR, true, self);
2016-09-03 18:07:30 +02:00
});
};
2017-01-04 00:40:07 +01:00
ApiService.prototype.postLogin = function (loginRequest, success, error) {
2016-09-03 18:07:30 +02:00
var self = this;
handleTokenState(self).then(function (token) {
2016-09-03 18:07:30 +02:00
$.ajax({
type: 'POST',
2017-06-16 18:35:46 +02:00
url: self.baseUrl + '/logins?' + token,
2017-01-04 00:40:07 +01:00
data: JSON.stringify(loginRequest),
2016-09-21 01:57:24 +02:00
contentType: 'application/json; charset=utf-8',
2016-09-03 18:07:30 +02:00
dataType: 'json',
success: function (response) {
2017-01-04 00:40:07 +01:00
success(new LoginResponse(response));
2016-09-03 18:07:30 +02:00
},
error: function (jqXHR, textStatus, errorThrown) {
handleError(error, jqXHR, false, self);
2016-09-03 18:07:30 +02:00
}
});
}, function (jqXHR) {
handleError(error, jqXHR, true, self);
2016-09-03 18:07:30 +02:00
});
};
2017-01-04 00:40:07 +01:00
ApiService.prototype.putLogin = function (id, loginRequest, success, error) {
2016-09-03 18:07:30 +02:00
var self = this;
handleTokenState(self).then(function (token) {
2016-09-03 18:07:30 +02:00
$.ajax({
type: 'POST',
2017-06-16 18:35:46 +02:00
url: self.baseUrl + '/logins/' + id + '?' + token,
2017-01-04 00:40:07 +01:00
data: JSON.stringify(loginRequest),
2016-09-21 01:57:24 +02:00
contentType: 'application/json; charset=utf-8',
2016-09-03 18:07:30 +02:00
dataType: 'json',
success: function (response) {
2017-01-04 00:40:07 +01:00
success(new LoginResponse(response));
2016-09-03 18:07:30 +02:00
},
error: function (jqXHR, textStatus, errorThrown) {
handleError(error, jqXHR, false, self);
2016-09-03 18:07:30 +02:00
}
});
}, function (jqXHR) {
handleError(error, jqXHR, true, self);
2016-09-03 18:07:30 +02:00
});
};
2016-09-03 07:13:09 +02:00
// Folder APIs
2016-09-03 18:07:30 +02:00
ApiService.prototype.getFolder = function (id, success, error) {
var self = this;
handleTokenState(self).then(function (token) {
2016-09-03 18:07:30 +02:00
$.ajax({
type: 'GET',
url: self.baseUrl + '/folders/' + id + '?' + token,
2016-09-03 18:07:30 +02:00
dataType: 'json',
success: function (response) {
2016-09-22 20:16:24 +02:00
success(new FolderResponse(response));
2016-09-03 18:07:30 +02:00
},
error: function (jqXHR, textStatus, errorThrown) {
handleError(error, jqXHR, false, self);
2016-09-03 18:07:30 +02:00
}
});
}, function (jqXHR) {
handleError(error, jqXHR, true, self);
});
2016-09-03 18:07:30 +02:00
};
2017-04-24 17:08:32 +02:00
ApiService.prototype.getFolders = function (success, error) {
var self = this;
handleTokenState(self).then(function (token) {
$.ajax({
type: 'GET',
url: self.baseUrl + '/folders?' + token,
2017-04-24 17:08:32 +02:00
dataType: 'json',
success: function (response) {
var data = [];
for (var i = 0; i < response.Data.length; i++) {
data.push(new FolderResponse(response.Data[i]));
}
success(new ListResponse(data));
},
error: function (jqXHR, textStatus, errorThrown) {
handleError(error, jqXHR, false, self);
}
});
}, function (jqXHR) {
handleError(error, jqXHR, true, self);
});
};
2016-09-03 18:07:30 +02:00
ApiService.prototype.postFolder = function (folderRequest, success, error) {
var self = this;
handleTokenState(self).then(function (token) {
2016-09-03 18:07:30 +02:00
$.ajax({
type: 'POST',
url: self.baseUrl + '/folders?' + token,
data: JSON.stringify(folderRequest),
2016-09-21 01:57:24 +02:00
contentType: 'application/json; charset=utf-8',
2016-09-03 18:07:30 +02:00
dataType: 'json',
success: function (response) {
2016-09-22 20:16:24 +02:00
success(new FolderResponse(response));
2016-09-03 18:07:30 +02:00
},
error: function (jqXHR, textStatus, errorThrown) {
handleError(error, jqXHR, false, self);
2016-09-03 18:07:30 +02:00
}
});
}, function (jqXHR) {
handleError(error, jqXHR, true, self);
2016-09-03 18:07:30 +02:00
});
};
ApiService.prototype.putFolder = function (id, folderRequest, success, error) {
var self = this;
handleTokenState(self).then(function (token) {
2016-09-03 18:07:30 +02:00
$.ajax({
type: 'POST',
url: self.baseUrl + '/folders/' + id + '?' + token,
data: JSON.stringify(folderRequest),
2016-09-21 01:57:24 +02:00
contentType: 'application/json; charset=utf-8',
2016-09-03 18:07:30 +02:00
dataType: 'json',
success: function (response) {
2016-09-22 20:16:24 +02:00
success(new FolderResponse(response));
2016-09-03 18:07:30 +02:00
},
error: function (jqXHR, textStatus, errorThrown) {
handleError(error, jqXHR, false, self);
2016-09-03 18:07:30 +02:00
}
});
}, function (jqXHR) {
handleError(error, jqXHR, true, self);
2016-09-03 18:07:30 +02:00
});
};
2017-07-12 16:16:04 +02:00
ApiService.prototype.deleteFolder = function (id, success, error) {
var self = this;
handleTokenState(self).then(function (token) {
$.ajax({
type: 'POST',
url: self.baseUrl + '/folders/' + id + '/delete?' + token,
dataType: 'text',
success: function (response) {
success();
},
error: function (jqXHR, textStatus, errorThrown) {
handleError(error, jqXHR, false, self);
}
});
}, function (jqXHR) {
handleError(error, jqXHR, true, self);
});
};
2016-09-03 07:13:09 +02:00
// Cipher APIs
2016-09-03 18:07:30 +02:00
2016-09-03 07:13:09 +02:00
ApiService.prototype.getCipher = function (id, success, error) {
var self = this;
handleTokenState(self).then(function (token) {
2016-09-03 07:13:09 +02:00
$.ajax({
type: 'GET',
url: self.baseUrl + '/ciphers/' + id + '?' + token,
2016-09-03 07:13:09 +02:00
dataType: 'json',
success: function (response) {
2016-09-22 20:16:24 +02:00
success(new CipherResponse(response));
2016-09-03 07:13:09 +02:00
},
error: function (jqXHR, textStatus, errorThrown) {
handleError(error, jqXHR, false, self);
2016-09-03 07:13:09 +02:00
}
});
}, function (jqXHR) {
handleError(error, jqXHR, true, self);
2016-09-03 07:13:09 +02:00
});
};
ApiService.prototype.getCiphers = function (success, error) {
var self = this;
handleTokenState(self).then(function (token) {
2016-09-03 07:13:09 +02:00
$.ajax({
type: 'GET',
url: self.baseUrl + '/ciphers?includeFolders=false&includeShared=true&' + token,
2016-09-03 07:13:09 +02:00
dataType: 'json',
success: function (response) {
var data = [];
2016-09-07 05:30:49 +02:00
for (var i = 0; i < response.Data.length; i++) {
data.push(new CipherResponse(response.Data[i]));
2016-09-03 07:13:09 +02:00
}
2016-09-22 20:16:24 +02:00
success(new ListResponse(data));
2016-09-03 07:13:09 +02:00
},
error: function (jqXHR, textStatus, errorThrown) {
handleError(error, jqXHR, false, self);
2016-09-03 07:13:09 +02:00
}
});
}, function (jqXHR) {
handleError(error, jqXHR, true, self);
2016-09-03 07:13:09 +02:00
});
};
2016-09-03 18:07:30 +02:00
ApiService.prototype.deleteCipher = function (id, success, error) {
var self = this;
handleTokenState(self).then(function (token) {
2016-09-03 18:07:30 +02:00
$.ajax({
type: 'POST',
url: self.baseUrl + '/ciphers/' + id + '/delete?' + token,
2016-12-29 22:09:28 +01:00
dataType: 'text',
2016-09-22 20:16:24 +02:00
success: function (response) {
success();
},
2016-09-03 18:07:30 +02:00
error: function (jqXHR, textStatus, errorThrown) {
handleError(error, jqXHR, false, self);
2016-09-03 18:07:30 +02:00
}
});
}, function (jqXHR) {
handleError(error, jqXHR, true, self);
2016-09-03 18:07:30 +02:00
});
};
2017-07-12 15:57:08 +02:00
ApiService.prototype.postCipherAttachment = function (id, formData, success, error) {
var self = this;
handleTokenState(self).then(function (token) {
$.ajax({
type: 'POST',
url: self.baseUrl + '/ciphers/' + id + '/attachment?' + token,
data: formData,
processData: false,
contentType: false,
dataType: 'json',
success: function (response) {
success(new CipherResponse(response));
},
error: function (jqXHR, textStatus, errorThrown) {
handleError(error, jqXHR, false, self);
}
});
}, function (jqXHR) {
handleError(error, jqXHR, true, self);
});
};
ApiService.prototype.deleteCipherAttachment = function (id, attachmentId, success, error) {
var self = this;
handleTokenState(self).then(function (token) {
$.ajax({
type: 'POST',
url: self.baseUrl + '/ciphers/' + id + '/attachment/' + attachmentId + '/delete?' + token,
dataType: 'text',
success: function (response) {
success();
},
error: function (jqXHR, textStatus, errorThrown) {
handleError(error, jqXHR, false, self);
}
});
}, function (jqXHR) {
handleError(error, jqXHR, true, self);
});
};
2016-09-03 18:07:30 +02:00
// Helpers
function handleError(errorCallback, jqXHR, tokenError, self) {
2017-03-16 00:26:16 +01:00
if (jqXHR && (tokenError && jqXHR.status === 400) || jqXHR.status === 401 || jqXHR.status === 403) {
log('Logging out. Reason: Status ' + jqXHR.status + '.');
2017-03-16 00:26:16 +01:00
console.log(jqXHR);
if (self && self.logoutCallback) {
2017-07-14 21:34:05 +02:00
self.logoutCallback(true, function () { });
}
else {
chrome.runtime.sendMessage({ command: 'logout', expired: true });
}
2016-09-21 21:21:50 +02:00
return;
}
2016-09-03 07:13:09 +02:00
errorCallback(new ErrorResponse(jqXHR));
}
function handleTokenState(self) {
var deferred = Q.defer();
self.tokenService.getToken(function (accessToken) {
log('Got access token');
if (!self.tokenService.tokenNeedsRefresh()) {
log('Token doesn\'t need refreshing');
2017-09-08 05:56:05 +02:00
resolveTokenQs(accessToken, deferred);
return;
}
log('Token needs refresh');
2017-01-22 04:00:02 +01:00
2017-09-08 05:56:05 +02:00
doRefreshToken(self, function (response) {
var tokenResponse = new IdentityTokenResponse(response);
self.tokenService.setTokens(tokenResponse.accessToken, tokenResponse.refreshToken, function () {
log('New token set.');
resolveTokenQs(tokenResponse.accessToken, deferred);
2017-07-14 04:33:34 +02:00
});
2017-09-08 05:56:05 +02:00
}, function (jqXHR) {
deferred.reject(jqXHR);
2017-07-14 04:33:34 +02:00
});
});
return deferred.promise;
2017-07-14 04:33:34 +02:00
}
2017-09-08 05:56:05 +02:00
function doRefreshToken(self, success, error) {
self.tokenService.getRefreshToken(function (refreshToken) {
if (!refreshToken || refreshToken === '') {
log('No existing refresh token.');
error();
return;
}
log('Got existing refresh token. Do refresh call.');
$.ajax({
type: 'POST',
url: self.identityBaseUrl + '/connect/token',
data: {
grant_type: 'refresh_token',
client_id: 'browser',
refresh_token: refreshToken
},
contentType: 'application/x-www-form-urlencoded; charset=utf-8',
dataType: 'json',
success: function (response) {
log('Successfully refreshed.');
success(response);
},
error: function (jqXHR, textStatus, errorThrown) {
log('Error refreshing.');
error(jqXHR);
}
});
});
}
function resolveTokenQs(token, deferred) {
log('Resolving token.');
2017-08-30 19:00:43 +02:00
deferred.resolve('access_token3=' + token);
}
function log(msg) {
console.log(new Date() + ' - API Service: ' + msg);
2017-01-22 04:00:02 +01:00
}
2017-07-14 21:34:05 +02:00
}