decide.nolog.cz/mirage/utils/encrypt.js
Jeldrik Hanschke 67caaad803
upgrade Ember CLI to 3.13 and Ember Source + Ember Data to 3.12 (#264)
* upgrade Ember CLI to 3.13 and Ember Source + Ember Data to 3.12

Ember Source upgrade to 3.13 is blocked by Ember CP Validations,
which does not support Ember Source 3.13 yet.

Ember Data upgrade to 3.13 is blocked by Ember Data Model Fragments,
which does not support Ember Data 3.13 yet.

Also removes autofocus mixin and inlines the code as mixins are
considered an antipattern in latest Ember versions.

Removes some left overs in `"resolutions"` key. But that shouldn't have a real impact.
2019-10-29 08:02:50 +01:00

29 lines
946 B
JavaScript

/*
* Encrypts all properties in mirage model (created by factory), encrypts them using
* sjcl and encryptionKey property of model as passphrase.
* Unsets encryptionKey property afterwards.
*/
import { assert } from '@ember/debug';
import { isPresent } from '@ember/utils';
import { isArray } from '@ember/array';
import { get } from '@ember/object';
import sjcl from 'sjcl';
export default function(propertiesToEncrypt, model) {
assert('first argument must be an array', isArray(propertiesToEncrypt));
assert('model must have an encryptionKey property which isn\'t empty', isPresent(get(model, 'encryptionKey')));
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);
}