passman/js/app/services/shareservice.js

226 lines
7 KiB
JavaScript
Raw Normal View History

'use strict';
/**
* @ngdoc service
* @name passmanApp.ShareService
* @description
* # ShareService
* Service in the passmanApp.
*/
angular.module('passmanApp')
.service('ShareService', ['$http', 'VaultService', 'EncryptService', 'CredentialService', function ($http, VaultService, EncryptService, CredentialService) {
// Setup sjcl random engine to max paranoia level and start collecting data
var paranoia_level = 10;
sjcl.random.setDefaultParanoia(paranoia_level);
sjcl.random.startCollectors();
return {
search: function (string) {
var queryUrl = OC.generateUrl('apps/passman/api/v2/sharing/search');
return $http.post(queryUrl, {search: string}).then(function (response) {
if (response.data) {
return response.data;
} else {
return response;
}
});
},
shareWithUser: function (credential, target_user_data) {
var queryUrl = OC.generateUrl('apps/passman/api/v2/sharing/share');
return $http.post(queryUrl,
{
item_id: credential.credential_id,
item_guid: credential.guid,
permissions: target_user_data.accessLevel,
vaults: target_user_data.vaults,
}
);
},
getVaultsByUser: function (userId) {
var queryUrl = OC.generateUrl('apps/passman/api/v2/sharing/vaults/' + userId);
2016-09-27 04:36:05 +08:00
return $http.get(queryUrl, {search: userId}).then(function (response) {
if (response.data) {
for (var i = 0; i < response.data.length; i++) {
2016-09-29 03:56:54 +08:00
response.data[i].public_sharing_key = forge.pki.publicKeyFromPem(response.data[i].public_sharing_key);
}
return response.data;
} else {
return response;
}
});
},
getPendingRequests: function () {
var queryUrl = OC.generateUrl('apps/passman/api/v2/sharing/pending');
return $http.get(queryUrl).then(function (response) {
if (response.data) {
return response.data;
}
});
},
saveSharingRequest: function (request, crypted_shared_key) {
var queryUrl = OC.generateUrl('apps/passman/api/v2/sharing/save');
return $http.post(queryUrl, {
item_guid: request.item_guid,
target_vault_guid: request.target_vault_guid,
final_shared_key: crypted_shared_key
2016-10-02 22:59:15 +08:00
}).then(function (response) {
return response.data;
})
},
2016-10-03 21:52:11 +08:00
unshareCredential: function (credential) {
var queryUrl = OC.generateUrl('apps/passman/api/v2/sharing/unshare/' + credential.guid);
2016-10-03 21:52:11 +08:00
return $http.delete(queryUrl).then(function (response) {
return response.data;
})
},
2016-10-04 04:28:30 +08:00
getPublicSharedCredential: function (credential_guid) {
2016-10-04 05:06:37 +08:00
var queryUrl = OC.generateUrl('apps/passman/api/v2/sharing/public/credential/' + credential_guid);
2016-10-03 17:42:19 +08:00
return $http.get(queryUrl).then(function (response) {
if (response.data) {
return response;
} else {
return response;
}
},
function (result) {
return result;
})
2016-10-03 17:42:19 +08:00
},
2016-10-03 01:14:30 +08:00
getCredendialsSharedWithUs: function (vault_guid) {
var queryUrl = OC.generateUrl('apps/passman/api/v2/sharing/vault/' + vault_guid + '/get');
2016-10-03 01:09:50 +08:00
return $http.get(queryUrl).then(function (response) {
if (response.data) {
2016-10-03 01:09:50 +08:00
return response.data;
}
});
},
encryptSharedCredential: function (credential, sharedKey) {
2016-10-02 19:00:52 +08:00
var _credential = angular.copy(credential);
_credential.shared_key = EncryptService.encryptString(sharedKey);
var encrypted_fields = CredentialService.getEncryptedFields();
for (var i = 0; i < encrypted_fields.length; i++) {
var field = encrypted_fields[i];
var fieldValue = angular.copy(credential[field]);
_credential[field] = EncryptService.encryptString(JSON.stringify(fieldValue), sharedKey);
}
2016-10-02 19:00:52 +08:00
return _credential;
},
decryptSharedCredential: function (credential, sharedKey) {
var _credential = angular.copy(credential);
var encrypted_fields = CredentialService.getEncryptedFields();
for (var i = 0; i < encrypted_fields.length; i++) {
var field = encrypted_fields[i];
var fieldValue = angular.copy(_credential[field]);
if (_credential.hasOwnProperty(field)) {
try {
var field_decrypted_value = EncryptService.decryptString(fieldValue, sharedKey);
} catch (e) {
console.log(e);
throw e
}
try {
_credential[field] = JSON.parse(field_decrypted_value);
} catch (e) {
console.log('Field' + field + ' in ' + _credential.label + ' could not be parsed! Value:' + fieldValue)
throw e
}
}
}
return _credential;
},
generateRSAKeys: function (key_length, progress, callback) {
var p = new C_Promise(function () {
var state = forge.pki.rsa.createKeyPairGenerationState(key_length, 0x10001);
var step = function () {
// run for 100 ms
if (!forge.pki.rsa.stepKeyPairGenerationState(state, 100)) {
// console.log(state);
if (state.p !== null) {
// progress(50);
this.call_progress(50);
}
else {
// progress(0);
this.call_progress(0);
}
setTimeout(step.bind(this), 1);
}
else {
// callback(state.keys);
this.call_then(state.keys);
}
};
setTimeout(step.bind(this), 100);
});
return p;
},
generateSharedKey: function (size) {
size = size || 20;
return new C_Promise(function () {
2016-09-27 04:36:05 +08:00
var t = this;
CRYPTO.PASSWORD.generate(size,
function (pass) {
2016-09-27 04:36:05 +08:00
t.call_then(pass);
},
function (progress) {
2016-09-27 04:36:05 +08:00
t.call_progress(progress);
}
);
})
},
rsaKeyPairToPEM: function (keypair) {
return {
'publicKey': forge.pki.publicKeyToPem(keypair.publicKey),
'privateKey': forge.pki.privateKeyToPem(keypair.privateKey)
};
2016-09-26 00:42:41 +08:00
},
getSharingKeys: function () {
2016-09-26 04:02:58 +08:00
var vault = VaultService.getActiveVault();
return {
2016-09-26 04:02:58 +08:00
'private_sharing_key': EncryptService.decryptString(angular.copy(vault.private_sharing_key)),
'public_sharing_key': vault.public_sharing_key
};
},
rsaPrivateKeyFromPEM: function (private_pem) {
2016-09-26 00:42:41 +08:00
return forge.pki.privateKeyFromPem(private_pem);
},
rsaPublicKeyFromPEM: function (public_pem) {
2016-09-26 00:42:41 +08:00
return forge.pki.publicKeyFromPem(public_pem);
2016-09-29 03:56:54 +08:00
},
/**
* Cyphers an array of string in a non-blocking way
* @param vaults[] An array of vaults with the processed public keys
* @param string The string to cypher
2016-09-29 03:56:54 +08:00
*/
cypherRSAStringWithPublicKeyBulkAsync: function (vaults, string) {
var workload = function () {
2016-09-29 03:56:54 +08:00
if (this.current_index < this.vaults.length > 0 && this.vaults.length > 0) {
var _vault = angular.copy(this.vaults[this.current_index]);
_vault.key = forge.util.encode64(
_vault.public_sharing_key.encrypt(this.string)
);
2016-09-29 03:56:54 +08:00
this.data.push(
_vault
2016-09-29 03:56:54 +08:00
);
this.current_index++;
this.call_progress(this.current_index);
setTimeout(workload.bind(this), 1);
}
else {
2016-09-29 03:56:54 +08:00
this.call_then(this.data);
}
};
return new C_Promise(function () {
2016-09-29 03:56:54 +08:00
this.data = [];
this.vaults = vaults;
this.string = string;
this.current_index = 0;
setTimeout(workload.bind(this), 0);
});
}
}
}]);