passman/js/app/services/credentialservice.js

121 lines
3.6 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')
2016-09-12 01:45:20 +08:00
.service('CredentialService', ['$http', 'EncryptService', function ($http, EncryptService) {
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;
}
});
},
updateCredential: 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
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;
}
});
},
2016-09-14 05:03:12 +08:00
encryptCredential: function (credential) {
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));
}
return credential;
},
2016-09-12 02:47:29 +08:00
decryptCredential: function (credential) {
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-09-16 03:43:23 +08:00
try{
credential[field] = JSON.parse(EncryptService.decryptString(fieldValue));
} catch (e){
console.log('Field' + field + ' in '+ credential.label +' could not be parsed! Value:'+ fieldValue)
}
2016-09-12 02:47:29 +08:00
}
return credential;
2016-09-12 01:45:20 +08:00
}
}
}]);