/
obfuscate.js
103 lines (87 loc) · 3.05 KB
/
obfuscate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// (c) 2012 Beau Sorensen
// Contributors: Jacob Chapel
// MIT Licensed
// For all details and documentation:
// https://github.com/tblobaum/mongoose-troop
// Obfuscate
// ---------
// Dependencies
var crypto = require('crypto')
, nestedPath = require('../helpers').nestedPath
// Plugin
function obfuscate (schema, options) {
options || (options = {})
// Options
var encryptPath = options.encryptPath || 'obfuscate' // Getter path for encrypting
, decryptPath = options.decryptPath || 'deobfuscate' // Setter path for decrypting
, algorithm = options.algorithm || 'aes-256-cbc' // Encryption algorithm
, key = options.key || 'secret' // Encryption key
, from = options.from || 'utf8' // Original encoding
, to = options.to || 'hex' // Conversion encoding
// Encrypt a string with the options provided
function encrypt (str) {
var cipher = crypto.createCipher(algorithm, key)
, crypted = cipher.update(str, from, to) + cipher.final(to)
return crypted;
}
// Decrypt an encrypted string
function decrypt (str) {
var decipher = crypto.createDecipher(algorithm, key)
, dec = decipher.update(str, to, from) + decipher.final(from)
return dec;
}
// Recursively encode all ObjectID's in schema
function encode (schema, doc, toEncrypt) {
if (!doc) return false
var method = (toEncrypt) ? encrypt : decrypt
, obj = (doc.toObject) ? doc.toObject() : doc
// Traverse through all schema paths
schema.eachPath(function (name, path) {
var val = nestedPath(doc, name)
// ObjectID paths
if (path.instance === 'ObjectID' && val) {
nestedPath(obj, name, method(val.toString()))
}
if (path.casterConstructor) {
// Array of DBRefs
if (!!~path.casterConstructor.toString().indexOf('ObjectId')) {
nestedPath(obj, name).forEach(function (v, k) {
nestedPath(obj, name)[k] = method(val[k].toString())
})
// Array of embedded schemas
} else if (!!~path.casterConstructor.toString().indexOf('EmbeddedDocument')) {
nestedPath(obj, name).forEach(function (v, k) {
nestedPath(obj, name)[k] = encode(path.schema, val[k], toEncrypt)
})
}
}
})
return obj
}
// Add encryption methods to both instance and model
;['method'
, 'static'
].forEach(function(method) {
schema[method]({
encrypt: encrypt
, decrypt: decrypt
})
})
// Manually encode an object through the model
schema.static('encode', function (obj, toEncrypt) {
if (!obj) return false
return encode(schema, obj, toEncrypt)
})
// Encode an instance when the virtual is called
schema.virtual(encryptPath).get(function () {
return encode(schema, this, true)
})
// Decode an object and set it to the instance
schema.virtual(decryptPath).set(function (v) {
var doc = encode(schema, v, false)
for (var prop in doc) {
this[prop] = doc[prop]
}
})
}
module.exports = obfuscate