2016-12-19 17:04:09 +01:00
|
|
|
/*
|
|
|
|
* Encrypts all properties in mirage model (created by factory), encrypts them using
|
|
|
|
* sjcl and encryptionKey property of model as passphrase.
|
|
|
|
* Unsets encryptionKey property afterwards.
|
|
|
|
*/
|
2018-12-29 01:27:37 +01:00
|
|
|
import { assert } from '@ember/debug';
|
|
|
|
import { isPresent } from '@ember/utils';
|
|
|
|
import { isArray } from '@ember/array';
|
|
|
|
import { get } from '@ember/object';
|
2016-12-19 17:04:09 +01:00
|
|
|
import sjcl from 'sjcl';
|
|
|
|
|
|
|
|
export default function(propertiesToEncrypt, model) {
|
2019-10-29 08:02:50 +01:00
|
|
|
assert('first argument must be an array', isArray(propertiesToEncrypt));
|
|
|
|
assert('model must have an encryptionKey property which isn\'t empty', isPresent(get(model, 'encryptionKey')));
|
2016-12-19 17:04:09 +01:00
|
|
|
|
|
|
|
let passphrase = get(model, 'encryptionKey');
|
|
|
|
let data = {
|
|
|
|
encryptionKey: undefined
|
|
|
|
};
|
|
|
|
|
|
|
|
propertiesToEncrypt.forEach((propertyToEncrypt) => {
|
|
|
|
let value = JSON.stringify(
|
|
|
|
get(model, propertyToEncrypt)
|
|
|
|
);
|
|
|
|
data[propertyToEncrypt] = sjcl.encrypt(passphrase, value);
|
|
|
|
});
|
|
|
|
|
|
|
|
model.update(data);
|
|
|
|
}
|