decide.nolog.cz/app/serializers/application.js

93 lines
2.7 KiB
JavaScript
Raw Normal View History

2014-10-30 21:44:22 +01:00
import DS from "ember-data";
import Ember from "ember";
/* global sjcl */
2014-10-30 21:44:22 +01:00
/*
* extends DS.RESTSerializer to implement encryption
*
* 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.
*/
export default DS.RESTSerializer.extend({
encryption: Ember.inject.service(),
encryptionKey: Ember.computed.alias('encryption.key'),
/*
* implement decryption
*/
normalize: function(modelClass, resourceHash, prop) {
var decryptionKey = this.get('encryptionKey');
// run before serialization of attribute hash
modelClass.eachAttribute(function(key, attributes) {
if (
attributes.options.encrypted !== false
) {
2015-10-17 15:44:27 +02:00
if (typeof resourceHash[key] !== "undefined" && resourceHash[key] !== null) {
2015-10-16 17:41:24 +02:00
try {
resourceHash[key] = JSON.parse(
sjcl.decrypt(decryptionKey, resourceHash[key])
);
}
catch (err) {
throw {
type: "decryption-failed",
message: "decryption failed for " + key + " using key " + decryptionKey,
original: err
};
}
}
}
}, 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);
}
return this._super(modelClass, resourceHash, prop);
},
/*
* implement encryption
*/
serializeAttribute: function(snapshot, json, key, attribute) {
this._super(snapshot, json, key, attribute);
// get encryption key from snapshot which is model representation
var encryptionKey = this.get('encryptionKey');
// map includePlainOnCreate after serialization of attribute hash
// but before encryption so we can just use the serialized hash
if (
!Ember.isEmpty(attribute.options.includePlainOnCreate) &&
typeof attribute.options.includePlainOnCreate === 'string'
) {
json[attribute.options.includePlainOnCreate] = json[key];
}
// encrypt after serialization of attribute hash
if (
attribute.options.encrypted !== false
) {
try {
json[key] = sjcl.encrypt(encryptionKey, JSON.stringify(json[key]));
}
catch(err) {
throw {
type: 'encryption-failed',
message: "encryption failed with key: " + encryptionKey,
original: err
};
}
}
}
});