passman/js/app/services/credentialservice.js

278 lines
9.2 KiB
JavaScript
Raw Normal View History

2016-09-11 05:30:17 +08:00
'use strict';
/**
* @ngdoc service
* @name passmanApp.CredentialService
* @description
* # CredentialService
* Service in the passmanApp.
*/
angular.module('passmanApp')
.service('CredentialService', ['$http', 'EncryptService', 'VaultService', function ($http, EncryptService, VaultService) {
2016-09-12 01:45:20 +08:00
var credential = {
'credential_id': null,
'guid': null,
'vault_id': null,
'label': null,
'description': null,
'created': null,
'changed': null,
2016-09-16 03:43:23 +08:00
'tags': [],
2016-09-12 01:45:20 +08:00
'email': null,
'username': null,
'password': null,
'url': null,
'favicon': null,
'renew_interval': null,
2016-09-15 03:12:10 +08:00
'expire_time': 0,
2016-09-13 22:31:07 +08:00
'delete_time': 0,
'files': [],
'custom_fields': [],
2016-09-14 05:03:12 +08:00
'otp': {},
2016-09-12 01:45:20 +08:00
'hidden': false
};
2016-09-15 03:12:10 +08:00
var _encryptedFields = ['description', 'username', 'password', 'files', 'custom_fields', 'otp', 'email', 'tags', 'url'];
2016-09-12 01:45:20 +08:00
return {
newCredential: function () {
return angular.copy(credential);
2016-09-12 01:45:20 +08:00
},
createCredential: function (credential) {
2016-09-15 03:12:10 +08:00
var _credential = angular.copy(credential);
for (var i = 0; i < _encryptedFields.length; i++) {
2016-09-12 01:45:20 +08:00
var field = _encryptedFields[i];
var fieldValue = angular.copy(credential[field]);
2016-09-15 03:12:10 +08:00
_credential[field] = EncryptService.encryptString(JSON.stringify(fieldValue));
2016-09-12 01:45:20 +08:00
}
_credential.expire_time = new Date( angular.copy(credential.expire_time) ).getTime() / 1000;
2016-09-12 01:45:20 +08:00
var queryUrl = OC.generateUrl('apps/passman/api/v2/credentials');
2016-09-15 03:12:10 +08:00
return $http.post(queryUrl, _credential).then(function (response) {
if (response.data) {
2016-09-12 01:45:20 +08:00
return response.data;
} else {
return response;
}
});
},
getEncryptedFields: function () {
return _encryptedFields;
},
updateCredential: function (credential, skipEncyption) {
2016-09-15 03:12:10 +08:00
var _credential = angular.copy(credential);
if(!skipEncyption){
for (var i = 0; i < _encryptedFields.length; i++) {
var field = _encryptedFields[i];
var fieldValue = angular.copy(credential[field]);
_credential[field] = EncryptService.encryptString(JSON.stringify(fieldValue));
}
2016-09-12 01:45:20 +08:00
}
_credential.expire_time = new Date( angular.copy(credential.expire_time) ).getTime() / 1000;
2016-09-12 01:45:20 +08:00
2016-09-15 03:12:10 +08:00
var queryUrl = OC.generateUrl('apps/passman/api/v2/credentials/' + credential.credential_id);
return $http.patch(queryUrl, _credential).then(function (response) {
if (response.data) {
2016-09-12 01:45:20 +08:00
return response.data;
} else {
return response;
}
});
2016-09-12 02:47:29 +08:00
},
getCredential: function(id){
var queryUrl = OC.generateUrl('apps/passman/api/v2/credentials/' + id);
return $http.get(queryUrl).then(function (response) {
if (response.data) {
return response.data;
} else {
return response;
}
});
},
destroyCredential: function(id){
var queryUrl = OC.generateUrl('apps/passman/api/v2/credentials/' + id);
return $http.delete(queryUrl).then(function (response) {
if (response.data) {
return response.data;
} else {
return response;
}
});
},
encryptCredential: function (credential, key) {
2016-09-15 03:12:10 +08:00
for (var i = 0; i < _encryptedFields.length; i++) {
2016-09-14 05:03:12 +08:00
var field = _encryptedFields[i];
var fieldValue = angular.copy(credential[field]);
credential[field] = EncryptService.encryptString(JSON.stringify(fieldValue), key);
2016-09-14 05:03:12 +08:00
}
return credential;
},
decryptCredential: function (credential, key) {
2016-09-15 03:12:10 +08:00
for (var i = 0; i < _encryptedFields.length; i++) {
2016-09-12 02:47:29 +08:00
var field = _encryptedFields[i];
var fieldValue = angular.copy(credential[field]);
2016-10-05 00:15:40 +08:00
try {
var field_decrypted_value = EncryptService.decryptString(fieldValue, key)
} catch (e){
console.log(e)
throw e
}
2016-09-16 03:43:23 +08:00
try{
credential[field] = JSON.parse(field_decrypted_value);
2016-09-16 03:43:23 +08:00
} catch (e){
console.log('Field' + field + ' in '+ credential.label +' could not be parsed! Value:'+ fieldValue)
2016-10-05 00:15:40 +08:00
2016-09-16 03:43:23 +08:00
}
2016-09-12 02:47:29 +08:00
}
return credential;
2016-09-24 17:40:15 +08:00
},
getRevisions: function(id){
var queryUrl = OC.generateUrl('apps/passman/api/v2/credentials/' + id + '/revision');
return $http.get(queryUrl).then(function (response) {
if (response.data) {
return response.data;
} else {
return response;
}
});
2016-09-24 18:59:19 +08:00
},
2016-10-05 20:36:02 +08:00
updateRevision: function(revision){
var _revision = angular.copy(revision);
2016-10-06 00:24:36 +08:00
_revision.credential_data = window.btoa(JSON.stringify(_revision.credential_data));
var queryUrl = OC.generateUrl('apps/passman/api/v2/credentials/' + revision.credential_data.credential_id + '/revision/' + revision.revision_id);
return $http.patch(queryUrl, _revision).then(function (response) {
2016-10-05 20:36:02 +08:00
if (response.data) {
return response.data;
} else {
return response;
}
});
},
2016-09-24 18:59:19 +08:00
deleteRevision: function(credential_id, revision_id){
var queryUrl = OC.generateUrl('apps/passman/api/v2/credentials/' + credential_id + '/revision/' + revision_id);
return $http.delete(queryUrl).then(function (response) {
if (response.data) {
return response.data;
} else {
return response;
}
});
},
reencryptCredential: function(credential_id, old_password, new_password){
var progress_datatype = function(current, total){
this.current = current;
this.total = total;
this.calculated = current / total * 100;
};
var promise_credential_update = (function(){
this.getCredential(credential_id).then((function (credential) {
this.plain_credential = this.decryptCredential(credential, this.old_password);
this.new_credential_cryptogram = this.encryptCredential(this.temp_data.plain_credential, this.new_password);
this.call_progress(new progress_datatype(1, 2));
// Save data
this.updateCredential(this.temp_data.new_credential_cryptogram, true).then((function(){
this.call_progress(new progress_datatype(2, 2));
this.call_then(this.plain_credential);
}).bind(this));
}).bind(this));
}).bind(this);
var promise_files_update = (function(){
// Add the double of the files so we take encryption phase and upload to the server into the math
this.total = this.plain_credential.files.length * 2; // Binded on credential finish upload
this.current = 0;
for (var i = 0; i < this.plain_credential.files.length; i++){
var _file = this.plain_credential.files[i];
FileService.getFile(_file).then((function (fileData) {
//Decrypt with old key
fileData.filename = EncryptService.decryptString(fileData.filename, this.old_password);
fileData.file_data = EncryptService.decryptString(fileData.file_data, this.old_password);
this.current ++;
this.call_progress(new progress_datatype(this.current, this.total));
FileService.updateFile(fileData, this.new_password).then((function(data){
this.current++;
this.call_progress(new progress_datatype(this.current, this.total));
if (this.current == this.total) {
this.call_then('All files has been updated');
}
}).bind(this));
}).bind(this));
}
}).bind(this);
var promise_revisions_update = (function(){
CredentialService.getRevisions(this.plain_credential.guid).then((function (revisions) {
// Double, so we include the actual upload of the data back to the server
this.total = revisions.length * 2;
this.upload = 0;
this.current = 0;
this.revisions = revisions;
var revision_workload = function(){
var _revision = revisions[this.current];
//Decrypt!
_revision.credential_data = this.decryptCredential(_revision.credential_data, this.old_password);
_revision.credential_data = ShareService.encryptSharedCredential(_revision.credential_data, this.new_password);
console.log('Used key for encrypting history ', this.new_password);
this.current ++;
this.call_progress(new progress_datatype(this.current + this.upload, this.total));
this.updateRevision(_revision).then((function(data){
this.upload ++;
this.call_progress(new progress_datatype(this.upload + this.current, this.total));
if (this.current + this.upload == this.total){
this.call_then("History updated");
}
}).bind(this));
setTimeout(revision_workload.bind(this), 1);
};
}).bind(this));
}).bind(this);
var promise_workload = (function(){
this.old_password = angular.copy(old_password);
this.new_password = angular.copy(new_password);
this.promises = 0;
(new C_Promise(promise_credential_update.bind(this))).progress(function(data){
this.call_progress(data);
}).then(function(data){
this.plain_credential = data;
this.promises ++;
(new C_Promise(promise_files_update.bind(this))).progress(function(data){
this.call_progress(data);
2016-10-06 18:34:29 +08:00
}).then(function(data){
this.promises --;
if (this.promises == 0){
this.call_then("All done");
}
2016-10-06 18:34:29 +08:00
});
this.promises ++;
(new C_Promise(promise_revisions_update.bind(this))).progress(function(data){
this.call_progress(data);
}).then(function(data){
this.promises --;
if (this.prmises == 0){
this.call_then("All done");
}
})
});
}).bind(this);
return new C_Promise(promise_workload);
2016-09-12 01:45:20 +08:00
}
}
}]);