mongoose-encryption
Advanced tools
Comparing version 0.8.2 to 0.9.0
66
index.js
@@ -39,7 +39,15 @@ (function() { | ||
schema.pre('init', function(next, data) { | ||
this.decrypt.call(data, function(err) { | ||
if (err) | ||
throw new Error(err); // throw because passing the error to next() in this hook causes it to get swallowed | ||
next(); | ||
}); | ||
if (this.constructor.name === 'EmbeddedDocument'){ | ||
this.decryptSync.call(data); | ||
this._doc = data; | ||
return this; // must return updated doc synchronously for EmbeddedDocuments | ||
} else { | ||
this.decrypt.call(data, function(err){ | ||
if (err) | ||
throw new Error(err); // throw because passing the error to next() in this hook causes it to get swallowed | ||
next(); | ||
}); | ||
} | ||
}); | ||
@@ -80,5 +88,13 @@ | ||
schema.methods.decrypt = function(cb) { | ||
schema.methods.decrypt = function(cb) { // callback style but actually synchronous to allow for decryptSync without copypasta or complication | ||
try { | ||
schema.methods.decryptSync.call(this); | ||
} catch(e){ | ||
return cb(e); | ||
} | ||
cb(); | ||
}; | ||
schema.methods.decryptSync = function() { | ||
var ct, ctWithIV, decipher, iv; | ||
var that = this; | ||
if (this._ct) { | ||
@@ -89,23 +105,17 @@ ctWithIV = this._ct.buffer || this._ct; | ||
decipher = crypto.createDecipheriv(ALGORITHM, key, iv); | ||
decipher.end(ct, function() { | ||
var decipheredVal, err, field, unencryptedObject; | ||
decipher.setEncoding('utf-8'); | ||
try { | ||
unencryptedObject = JSON.parse(decipher.read()); | ||
} catch (err) { | ||
if (that._id) | ||
idString = that._id.toString() | ||
else | ||
idString = 'unknown' | ||
return cb('Error parsing JSON during decrypt of ' + idString + ': ' + err); | ||
} | ||
for (field in unencryptedObject) { | ||
decipheredVal = unencryptedObject[field]; | ||
that[field] = decipheredVal; | ||
} | ||
that._ct = undefined; | ||
return cb(null); | ||
}); | ||
} else { | ||
cb(null); | ||
decryptedObjectJSON = decipher.update(ct, undefined, 'utf8') + decipher.final('utf8'); | ||
try { | ||
decryptedObject = JSON.parse(decryptedObjectJSON); | ||
} catch (err) { | ||
if (this._id) | ||
idString = this._id.toString(); | ||
else | ||
idString = 'unknown'; | ||
throw new Error('Error parsing JSON during decrypt of ' + idString + ': ' + err); | ||
} | ||
for (field in decryptedObject) { | ||
decipheredVal = decryptedObject[field]; | ||
this[field] = decipheredVal; | ||
} | ||
this._ct = undefined; | ||
} | ||
@@ -112,0 +122,0 @@ }; |
{ | ||
"name": "mongoose-encryption", | ||
"description": "Simple encryption plugin for Mongoose", | ||
"version": "0.8.2", | ||
"version": "0.9.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Joe Goldbeck" |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
34338
6
106