2016-09-21 21:21:50 +02:00
|
|
|
function ApiService(tokenService) {
|
2016-09-03 06:38:27 +02:00
|
|
|
this.baseUrl = 'https://api.bitwarden.com';
|
|
|
|
this.tokenService = tokenService;
|
2016-09-04 06:34:24 +02:00
|
|
|
|
|
|
|
initApiService();
|
2016-09-03 06:38:27 +02:00
|
|
|
};
|
|
|
|
|
2016-09-04 06:34:24 +02:00
|
|
|
function initApiService() {
|
2016-09-03 21:44:32 +02:00
|
|
|
// Auth APIs
|
|
|
|
|
|
|
|
ApiService.prototype.postToken = function (tokenRequest, success, error) {
|
|
|
|
var self = this;
|
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: self.baseUrl + '/auth/token',
|
|
|
|
data: JSON.stringify(tokenRequest),
|
2016-09-21 01:57:24 +02:00
|
|
|
contentType: 'application/json; charset=utf-8',
|
2016-09-03 21:44:32 +02:00
|
|
|
dataType: 'json',
|
|
|
|
success: function (response) {
|
2016-09-22 20:16:24 +02:00
|
|
|
success(new TokenResponse(response));
|
2016-09-03 21:44:32 +02:00
|
|
|
},
|
|
|
|
error: function (jqXHR, textStatus, errorThrown) {
|
|
|
|
handleError(error, jqXHR, textStatus, errorThrown);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
ApiService.prototype.postTokenTwoFactor = function (twoFactorTokenRequest, success, error) {
|
|
|
|
var self = this;
|
2016-09-21 17:35:24 +02:00
|
|
|
this.tokenService.getToken(function (token) {
|
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: self.baseUrl + '/auth/token/two-factor?access_token=' + token,
|
|
|
|
data: JSON.stringify(twoFactorTokenRequest),
|
|
|
|
contentType: 'application/json; charset=utf-8',
|
|
|
|
dataType: 'json',
|
|
|
|
success: function (response) {
|
2016-09-22 20:16:24 +02:00
|
|
|
success(new TokenResponse(response));
|
2016-09-21 17:35:24 +02:00
|
|
|
},
|
|
|
|
error: function (jqXHR, textStatus, errorThrown) {
|
|
|
|
handleError(error, jqXHR, textStatus, errorThrown);
|
|
|
|
}
|
|
|
|
});
|
2016-09-03 21:44:32 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-09-03 07:13:09 +02:00
|
|
|
// Account APIs
|
2016-09-03 18:07:30 +02:00
|
|
|
|
2016-09-03 06:38:27 +02:00
|
|
|
ApiService.prototype.getProfile = function (success, error) {
|
|
|
|
var self = this;
|
2016-09-03 07:13:09 +02:00
|
|
|
this.tokenService.getToken(function (token) {
|
2016-09-03 06:38:27 +02:00
|
|
|
$.ajax({
|
|
|
|
type: 'GET',
|
2016-09-03 18:07:30 +02:00
|
|
|
url: self.baseUrl + '/accounts/profile?access_token=' + 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, textStatus, errorThrown);
|
|
|
|
}
|
2016-09-03 06:38:27 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2016-09-03 07:13:09 +02:00
|
|
|
|
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, textStatus, errorThrown);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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, textStatus, errorThrown);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-01-14 17:20:44 +01:00
|
|
|
// Settings APIs
|
|
|
|
|
|
|
|
ApiService.prototype.getIncludedDomains = function (success, error) {
|
|
|
|
var self = this;
|
|
|
|
this.tokenService.getToken(function (token) {
|
|
|
|
$.ajax({
|
|
|
|
type: 'GET',
|
|
|
|
url: self.baseUrl + '/settings/domains?excluded=false&access_token=' + token,
|
|
|
|
dataType: 'json',
|
|
|
|
success: function (response) {
|
|
|
|
success(new DomainsResponse(response));
|
|
|
|
},
|
|
|
|
error: function (jqXHR, textStatus, errorThrown) {
|
|
|
|
handleError(error, jqXHR, textStatus, errorThrown);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
this.tokenService.getToken(function (token) {
|
|
|
|
$.ajax({
|
|
|
|
type: 'GET',
|
|
|
|
url: self.baseUrl + '/sites/' + id + '?access_token=' + token,
|
|
|
|
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, textStatus, errorThrown);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
this.tokenService.getToken(function (token) {
|
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: self.baseUrl + '/sites?access_token=' + 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, textStatus, errorThrown);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
this.tokenService.getToken(function (token) {
|
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: self.baseUrl + '/sites/' + id + '?access_token=' + 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, textStatus, errorThrown);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
this.tokenService.getToken(function (token) {
|
|
|
|
$.ajax({
|
|
|
|
type: 'GET',
|
|
|
|
url: self.baseUrl + '/folders/' + id + '?access_token=' + token,
|
|
|
|
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, textStatus, errorThrown);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
ApiService.prototype.postFolder = function (folderRequest, success, error) {
|
|
|
|
var self = this;
|
|
|
|
this.tokenService.getToken(function (token) {
|
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: self.baseUrl + '/folders?access_token=' + token,
|
2016-09-03 21:44:32 +02:00
|
|
|
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, textStatus, errorThrown);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
ApiService.prototype.putFolder = function (id, folderRequest, success, error) {
|
|
|
|
var self = this;
|
|
|
|
this.tokenService.getToken(function (token) {
|
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: self.baseUrl + '/folders/' + id + '?access_token=' + token,
|
2016-09-03 21:44:32 +02:00
|
|
|
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, textStatus, errorThrown);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
this.tokenService.getToken(function (token) {
|
|
|
|
$.ajax({
|
|
|
|
type: 'GET',
|
2016-09-03 18:07:30 +02:00
|
|
|
url: self.baseUrl + '/ciphers/' + id + '?access_token=' + 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, textStatus, errorThrown);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
ApiService.prototype.getCiphers = function (success, error) {
|
|
|
|
var self = this;
|
|
|
|
this.tokenService.getToken(function (token) {
|
|
|
|
$.ajax({
|
|
|
|
type: 'GET',
|
2016-09-03 18:07:30 +02:00
|
|
|
url: self.baseUrl + '/ciphers?access_token=' + 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, textStatus, errorThrown);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-09-03 18:07:30 +02:00
|
|
|
ApiService.prototype.deleteCipher = function (id, success, error) {
|
|
|
|
var self = this;
|
|
|
|
this.tokenService.getToken(function (token) {
|
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: self.baseUrl + '/ciphers/' + id + '/delete?access_token=' + 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, textStatus, errorThrown);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Helpers
|
|
|
|
|
2016-09-03 07:13:09 +02:00
|
|
|
function handleError(errorCallback, jqXHR, textStatus, errorThrown) {
|
2016-09-21 21:21:50 +02:00
|
|
|
if (jqXHR.status === 401 || jqXHR.status === 403) {
|
2016-09-27 02:29:23 +02:00
|
|
|
chrome.runtime.sendMessage({ command: 'logout' });
|
2016-09-21 21:21:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-03 07:13:09 +02:00
|
|
|
errorCallback(new ErrorResponse(jqXHR));
|
|
|
|
}
|
2016-09-04 06:34:24 +02:00
|
|
|
};
|