2018-12-29 01:27:37 +01:00
|
|
|
import { isEmpty } from '@ember/utils';
|
|
|
|
import { inject as service } from '@ember/service';
|
2016-01-28 23:48:14 +01:00
|
|
|
import DS from 'ember-data';
|
2014-10-30 21:44:22 +01:00
|
|
|
|
2015-08-23 16:13:52 +02:00
|
|
|
/*
|
|
|
|
* extends DS.RESTSerializer to implement encryption
|
2016-01-28 23:48:14 +01:00
|
|
|
*
|
2015-08-23 16:13:52 +02:00
|
|
|
* By default every attribute hash is encrypted using SJCL.
|
|
|
|
* This is configurable by options parameter of DS.attr().
|
|
|
|
*
|
|
|
|
* Options:
|
|
|
|
* - encrypted (boolean)
|
|
|
|
* If false the attribute won't be encrypted.
|
|
|
|
* - includePlainOnCreate (string)
|
|
|
|
* If set the attribute will be included plain (not encrypted) when
|
|
|
|
* recorde is created. Value is the attributes name used.
|
|
|
|
*/
|
2015-08-19 22:00:01 +02:00
|
|
|
export default DS.RESTSerializer.extend({
|
2016-08-12 13:33:03 +02:00
|
|
|
isNewSerializerAPI: true,
|
|
|
|
|
2018-12-29 01:27:37 +01:00
|
|
|
encryption: service(),
|
2015-10-29 12:50:00 +01:00
|
|
|
|
2015-08-23 16:13:52 +02:00
|
|
|
/*
|
|
|
|
* implement decryption
|
|
|
|
*/
|
2016-01-28 23:48:14 +01:00
|
|
|
normalize(modelClass, resourceHash, prop) {
|
2015-08-23 16:13:52 +02:00
|
|
|
|
|
|
|
// run before serialization of attribute hash
|
2015-08-19 22:00:01 +02:00
|
|
|
modelClass.eachAttribute(function(key, attributes) {
|
|
|
|
if (
|
|
|
|
attributes.options.encrypted !== false
|
|
|
|
) {
|
2016-01-28 23:48:14 +01:00
|
|
|
if (typeof resourceHash[key] !== 'undefined' && resourceHash[key] !== null) {
|
2015-10-29 12:50:00 +01:00
|
|
|
resourceHash[key] = this.get('encryption').decrypt(resourceHash[key]);
|
2015-08-19 22:00:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}, this);
|
2015-10-25 16:46:11 +01:00
|
|
|
|
|
|
|
// run legacy support transformation specified in model serializer
|
|
|
|
if (typeof this.legacySupport === 'function') {
|
|
|
|
resourceHash = this.legacySupport(resourceHash);
|
|
|
|
}
|
2016-01-28 23:48:14 +01:00
|
|
|
|
2015-08-19 22:00:01 +02:00
|
|
|
return this._super(modelClass, resourceHash, prop);
|
|
|
|
},
|
|
|
|
|
2015-08-23 16:13:52 +02:00
|
|
|
/*
|
|
|
|
* implement encryption
|
|
|
|
*/
|
2016-01-28 23:48:14 +01:00
|
|
|
serializeAttribute(snapshot, json, key, attribute) {
|
2015-08-23 16:13:52 +02:00
|
|
|
this._super(snapshot, json, key, attribute);
|
|
|
|
|
|
|
|
// map includePlainOnCreate after serialization of attribute hash
|
|
|
|
// but before encryption so we can just use the serialized hash
|
|
|
|
if (
|
2018-12-29 01:27:37 +01:00
|
|
|
!isEmpty(attribute.options.includePlainOnCreate) &&
|
2015-08-23 16:13:52 +02:00
|
|
|
typeof attribute.options.includePlainOnCreate === 'string'
|
|
|
|
) {
|
|
|
|
json[attribute.options.includePlainOnCreate] = json[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
// encrypt after serialization of attribute hash
|
2015-08-19 22:00:01 +02:00
|
|
|
if (
|
2015-08-23 16:13:52 +02:00
|
|
|
attribute.options.encrypted !== false
|
2015-08-19 22:00:01 +02:00
|
|
|
) {
|
2015-10-29 12:50:00 +01:00
|
|
|
json[key] = this.get('encryption').encrypt(json[key]);
|
2015-08-19 22:00:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|