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;
|
2017-01-22 02:00:25 +01:00
|
|
|
this.logoutCallback = logoutCallback;
|
2017-01-26 04:57:32 +01:00
|
|
|
this.appIdService = appIdService;
|
|
|
|
this.utilsService = utilsService;
|
2017-08-22 18:00:59 +02:00
|
|
|
this.constantsService = constantsService;
|
2016-09-04 06:34:24 +02:00
|
|
|
|
2017-09-04 18:09:11 +02:00
|
|
|
this.urlsSet = false;
|
|
|
|
this.baseUrl = null;
|
|
|
|
this.identityBaseUrl = null;
|
|
|
|
|
2016-09-04 06:34:24 +02:00
|
|
|
initApiService();
|
2017-07-14 21:34:05 +02:00
|
|
|
}
|
2016-09-03 06:38:27 +02:00
|
|
|
|
2016-09-04 06:34:24 +02:00
|
|
|
function initApiService() {
|
2017-09-04 18:09:11 +02:00
|
|
|
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
|
|
|
}
|
2017-09-04 18:09:11 +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
|
2017-09-04 18:09:11 +02:00
|
|
|
//self.baseUrl = 'http://localhost:4000';
|
|
|
|
//self.identityBaseUrl = 'http://localhost:33656';
|
2017-08-22 18:00:59 +02:00
|
|
|
|
|
|
|
// Desktop HTTPS
|
2017-09-04 18:09:11 +02:00
|
|
|
//self.baseUrl = 'https://localhost:44377';
|
|
|
|
//self.identityBaseUrl = 'https://localhost:44392';
|
2017-08-22 18:00:59 +02:00
|
|
|
|
|
|
|
// Desktop external
|
2017-09-04 18:09:11 +02:00
|
|
|
//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
|
2017-09-04 18:09:11 +02:00
|
|
|
//self.baseUrl = 'https://preview-api.bitwarden.com';
|
|
|
|
//self.identityBaseUrl = 'https://preview-identity.bitwarden.com';
|
2017-08-22 18:00:59 +02:00
|
|
|
|
|
|
|
// Production
|
2017-09-04 18:09:11 +02:00
|
|
|
self.baseUrl = 'https://api.bitwarden.com';
|
|
|
|
self.identityBaseUrl = 'https://identity.bitwarden.com';
|
2017-08-22 18:00:59 +02:00
|
|
|
};
|
|
|
|
|
2016-09-03 21:44:32 +02:00
|
|
|
// Auth APIs
|
|
|
|
|
2017-01-19 04:14:51 +01:00
|
|
|
ApiService.prototype.postIdentityToken = function (tokenRequest, success, successWithTwoFactor, error) {
|
2017-01-18 05:07:46 +01:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
2017-05-06 03:54:49 +02:00
|
|
|
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) {
|
2017-06-26 21:37:15 +02:00
|
|
|
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);
|
|
|
|
});
|
2017-01-19 04:14:51 +01:00
|
|
|
}
|
|
|
|
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) {
|
|
|
|
refreshToken(this, function () {
|
|
|
|
success();
|
|
|
|
}, function (jqXHR) {
|
|
|
|
if (jqXHR) {
|
|
|
|
handleError(error, jqXHR, false, self);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
error();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-06-26 21:37:15 +02:00
|
|
|
// Two Factor APIs
|
|
|
|
|
2017-06-27 04:24:10 +02:00
|
|
|
ApiService.prototype.postTwoFactorEmail = function (request, success, error) {
|
2017-06-26 21:37:15 +02:00
|
|
|
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),
|
2017-06-26 21:37:15 +02:00
|
|
|
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
|
|
|
|
2017-01-14 18:47:11 +01:00
|
|
|
ApiService.prototype.getAccountRevisionDate = function (success, error) {
|
|
|
|
var self = this;
|
2017-01-19 06:21:20 +01:00
|
|
|
handleTokenState(self).then(function (token) {
|
2017-01-14 18:47:11 +01:00
|
|
|
$.ajax({
|
|
|
|
type: 'GET',
|
2017-05-06 05:19:38 +02:00
|
|
|
url: self.baseUrl + '/accounts/revision-date?' + token,
|
2017-01-14 18:47:11 +01:00
|
|
|
dataType: 'json',
|
|
|
|
success: function (response) {
|
|
|
|
success(response);
|
|
|
|
},
|
|
|
|
error: function (jqXHR, textStatus, errorThrown) {
|
2017-01-22 02:00:25 +01:00
|
|
|
handleError(error, jqXHR, false, self);
|
2017-01-14 18:47:11 +01:00
|
|
|
}
|
|
|
|
});
|
2017-01-19 06:21:20 +01:00
|
|
|
}, function (jqXHR) {
|
2017-01-22 02:00:25 +01:00
|
|
|
handleError(error, jqXHR, true, self);
|
2017-01-14 18:47:11 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-09-03 06:38:27 +02:00
|
|
|
ApiService.prototype.getProfile = function (success, error) {
|
|
|
|
var self = this;
|
2017-01-19 06:21:20 +01:00
|
|
|
handleTokenState(self).then(function (token) {
|
2016-09-03 06:38:27 +02:00
|
|
|
$.ajax({
|
|
|
|
type: 'GET',
|
2017-05-06 05:19:38 +02:00
|
|
|
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) {
|
2017-01-22 02:00:25 +01:00
|
|
|
handleError(error, jqXHR, false, self);
|
2016-09-03 07:13:09 +02:00
|
|
|
}
|
2016-09-03 06:38:27 +02:00
|
|
|
});
|
2017-01-19 06:21:20 +01:00
|
|
|
}, function (jqXHR) {
|
2017-01-22 02:00:25 +01:00
|
|
|
handleError(error, jqXHR, true, self);
|
2016-09-03 06:38:27 +02:00
|
|
|
});
|
|
|
|
};
|
2016-09-03 07:13:09 +02:00
|
|
|
|
2017-04-25 22:23:37 +02:00
|
|
|
ApiService.prototype.getKeys = function (success, error) {
|
|
|
|
var self = this;
|
|
|
|
handleTokenState(self).then(function (token) {
|
|
|
|
$.ajax({
|
|
|
|
type: 'GET',
|
2017-05-06 05:19:38 +02:00
|
|
|
url: self.baseUrl + '/accounts/keys?' + token,
|
2017-04-25 22:23:37 +02:00
|
|
|
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) {
|
2017-01-22 02:00:25 +01:00
|
|
|
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) {
|
2017-01-22 02:00:25 +01:00
|
|
|
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;
|
2017-01-19 06:21:20 +01:00
|
|
|
handleTokenState(self).then(function (token) {
|
2017-01-14 17:20:44 +01:00
|
|
|
$.ajax({
|
|
|
|
type: 'GET',
|
2017-05-06 05:19:38 +02:00
|
|
|
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) {
|
2017-01-22 02:00:25 +01:00
|
|
|
handleError(error, jqXHR, false, self);
|
2017-01-14 17:20:44 +01:00
|
|
|
}
|
|
|
|
});
|
2017-01-19 06:21:20 +01:00
|
|
|
}, function (jqXHR) {
|
2017-01-22 02:00:25 +01:00
|
|
|
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;
|
2017-01-19 06:21:20 +01:00
|
|
|
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) {
|
2017-01-22 02:00:25 +01:00
|
|
|
handleError(error, jqXHR, false, self);
|
2016-09-03 18:07:30 +02:00
|
|
|
}
|
|
|
|
});
|
2017-01-19 06:21:20 +01:00
|
|
|
}, function (jqXHR) {
|
2017-01-22 02:00:25 +01:00
|
|
|
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;
|
2017-01-19 06:21:20 +01:00
|
|
|
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) {
|
2017-01-22 02:00:25 +01:00
|
|
|
handleError(error, jqXHR, false, self);
|
2016-09-03 18:07:30 +02:00
|
|
|
}
|
|
|
|
});
|
2017-01-19 06:21:20 +01:00
|
|
|
}, function (jqXHR) {
|
2017-01-22 02:00:25 +01:00
|
|
|
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;
|
2017-01-19 06:21:20 +01:00
|
|
|
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) {
|
2017-01-22 02:00:25 +01:00
|
|
|
handleError(error, jqXHR, false, self);
|
2016-09-03 18:07:30 +02:00
|
|
|
}
|
|
|
|
});
|
2017-01-19 06:21:20 +01:00
|
|
|
}, function (jqXHR) {
|
2017-01-22 02:00:25 +01:00
|
|
|
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;
|
2017-01-19 06:21:20 +01:00
|
|
|
handleTokenState(self).then(function (token) {
|
2016-09-03 18:07:30 +02:00
|
|
|
$.ajax({
|
|
|
|
type: 'GET',
|
2017-05-06 05:19:38 +02:00
|
|
|
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) {
|
2017-01-22 02:00:25 +01:00
|
|
|
handleError(error, jqXHR, false, self);
|
2016-09-03 18:07:30 +02:00
|
|
|
}
|
|
|
|
});
|
2017-01-19 06:21:20 +01:00
|
|
|
}, function (jqXHR) {
|
2017-01-22 02:00:25 +01:00
|
|
|
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',
|
2017-05-06 05:19:38 +02:00
|
|
|
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;
|
2017-01-19 06:21:20 +01:00
|
|
|
handleTokenState(self).then(function (token) {
|
2016-09-03 18:07:30 +02:00
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
2017-05-06 05:19:38 +02:00
|
|
|
url: self.baseUrl + '/folders?' + 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) {
|
2017-01-22 02:00:25 +01:00
|
|
|
handleError(error, jqXHR, false, self);
|
2016-09-03 18:07:30 +02:00
|
|
|
}
|
|
|
|
});
|
2017-01-19 06:21:20 +01:00
|
|
|
}, function (jqXHR) {
|
2017-01-22 02:00:25 +01:00
|
|
|
handleError(error, jqXHR, true, self);
|
2016-09-03 18:07:30 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
ApiService.prototype.putFolder = function (id, folderRequest, success, error) {
|
|
|
|
var self = this;
|
2017-01-19 06:21:20 +01:00
|
|
|
handleTokenState(self).then(function (token) {
|
2016-09-03 18:07:30 +02:00
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
2017-05-06 05:19:38 +02:00
|
|
|
url: self.baseUrl + '/folders/' + id + '?' + 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) {
|
2017-01-22 02:00:25 +01:00
|
|
|
handleError(error, jqXHR, false, self);
|
2016-09-03 18:07:30 +02:00
|
|
|
}
|
|
|
|
});
|
2017-01-19 06:21:20 +01:00
|
|
|
}, function (jqXHR) {
|
2017-01-22 02:00:25 +01:00
|
|
|
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;
|
2017-01-19 06:21:20 +01:00
|
|
|
handleTokenState(self).then(function (token) {
|
2016-09-03 07:13:09 +02:00
|
|
|
$.ajax({
|
|
|
|
type: 'GET',
|
2017-05-06 05:19:38 +02:00
|
|
|
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) {
|
2017-01-22 02:00:25 +01:00
|
|
|
handleError(error, jqXHR, false, self);
|
2016-09-03 07:13:09 +02:00
|
|
|
}
|
|
|
|
});
|
2017-01-19 06:21:20 +01:00
|
|
|
}, function (jqXHR) {
|
2017-01-22 02:00:25 +01:00
|
|
|
handleError(error, jqXHR, true, self);
|
2016-09-03 07:13:09 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
ApiService.prototype.getCiphers = function (success, error) {
|
|
|
|
var self = this;
|
2017-01-19 06:21:20 +01:00
|
|
|
handleTokenState(self).then(function (token) {
|
2016-09-03 07:13:09 +02:00
|
|
|
$.ajax({
|
|
|
|
type: 'GET',
|
2017-05-06 05:19:38 +02:00
|
|
|
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) {
|
2017-01-22 02:00:25 +01:00
|
|
|
handleError(error, jqXHR, false, self);
|
2016-09-03 07:13:09 +02:00
|
|
|
}
|
|
|
|
});
|
2017-01-19 06:21:20 +01:00
|
|
|
}, function (jqXHR) {
|
2017-01-22 02:00:25 +01:00
|
|
|
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;
|
2017-01-19 06:21:20 +01:00
|
|
|
handleTokenState(self).then(function (token) {
|
2016-09-03 18:07:30 +02:00
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
2017-05-06 05:19:38 +02:00
|
|
|
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) {
|
2017-01-22 02:00:25 +01:00
|
|
|
handleError(error, jqXHR, false, self);
|
2016-09-03 18:07:30 +02:00
|
|
|
}
|
|
|
|
});
|
2017-01-19 06:21:20 +01:00
|
|
|
}, function (jqXHR) {
|
2017-01-22 02:00:25 +01:00
|
|
|
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
|
|
|
|
|
2017-01-22 02:00:25 +01:00
|
|
|
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) {
|
|
|
|
console.log('Logging out from apiService at ' + new Date() + '. Reason: Status ' + jqXHR.status + '.');
|
|
|
|
console.log(jqXHR);
|
2017-01-22 02:00:25 +01:00
|
|
|
if (self && self.logoutCallback) {
|
2017-07-14 21:34:05 +02:00
|
|
|
self.logoutCallback(true, function () { });
|
2017-01-22 02:00:25 +01:00
|
|
|
}
|
|
|
|
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));
|
|
|
|
}
|
2017-01-19 06:21:20 +01:00
|
|
|
|
|
|
|
function handleTokenState(self) {
|
|
|
|
var deferred = Q.defer();
|
2017-08-31 20:17:52 +02:00
|
|
|
self.tokenService.getToken(function (accessToken) {
|
|
|
|
if (self.tokenService.tokenNeedsRefresh()) {
|
|
|
|
refreshToken(self, function (accessToken) {
|
2017-05-06 05:19:38 +02:00
|
|
|
resolveTokenQs(accessToken, self, deferred);
|
2017-08-31 20:17:52 +02:00
|
|
|
}, function (err) {
|
|
|
|
deferred.reject(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
resolveTokenQs(accessToken, self, deferred);
|
|
|
|
}
|
2017-01-19 06:21:20 +01:00
|
|
|
});
|
|
|
|
|
2017-07-14 21:34:05 +02:00
|
|
|
return deferred.promise;
|
2017-01-19 06:21:20 +01:00
|
|
|
}
|
2017-01-22 04:00:02 +01:00
|
|
|
|
2017-07-14 04:33:34 +02:00
|
|
|
function refreshToken(self, success, error) {
|
|
|
|
self.tokenService.getRefreshToken(function (refreshToken) {
|
|
|
|
if (!refreshToken || refreshToken === '') {
|
|
|
|
error();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
postConnectToken(self, {
|
|
|
|
grant_type: 'refresh_token',
|
|
|
|
client_id: 'browser',
|
|
|
|
refresh_token: refreshToken
|
|
|
|
}, function (token) {
|
|
|
|
self.tokenService.setTokens(token.accessToken, token.refreshToken, function () {
|
|
|
|
success(token.accessToken);
|
|
|
|
});
|
|
|
|
}, function (jqXHR) {
|
|
|
|
error(jqXHR);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-06 05:19:38 +02:00
|
|
|
function resolveTokenQs(token, self, deferred) {
|
2017-08-30 19:00:43 +02:00
|
|
|
deferred.resolve('access_token3=' + token);
|
2017-05-06 05:19:38 +02:00
|
|
|
}
|
|
|
|
|
2017-01-22 04:07:08 +01:00
|
|
|
function postConnectToken(self, data, success, error) {
|
2017-01-22 04:00:02 +01:00
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
2017-05-06 03:54:49 +02:00
|
|
|
url: self.identityBaseUrl + '/connect/token',
|
2017-01-22 04:00:02 +01:00
|
|
|
data: data,
|
|
|
|
contentType: 'application/x-www-form-urlencoded; charset=utf-8',
|
|
|
|
dataType: 'json',
|
|
|
|
success: function (response) {
|
|
|
|
success(new IdentityTokenResponse(response));
|
|
|
|
},
|
|
|
|
error: function (jqXHR, textStatus, errorThrown) {
|
|
|
|
error(jqXHR);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-07-14 21:34:05 +02:00
|
|
|
}
|