-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
210 lines (189 loc) · 5.45 KB
/
index.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
'use strict';
var bcrypt = require('bcryptjs');
var mongoose = require('mongoose');
var semver = require('semver');
module.exports = function(schema, options) {
options = options || {};
// Get array of encrypted field(s)
var fields = options.fields || options.field || [];
if (typeof fields === 'string') {
fields = [ fields ];
}
// Scan schema for fields marked as encrypted
schema.eachPath(function(name,type) {
if (type.options.bcrypt && fields.indexOf(name) < 0) {
fields.push(name);
}
});
// Use default 'password' field if no fields specified
if (fields.length === 0)
fields.push('password');
// Get encryption rounds or use defaults
var rounds = options.rounds || 0;
// Add properties and verifier functions to schema for each encrypted field
fields.forEach(function(field){
// Setup field name for camelcasing
var path = field.split('.');
var fieldName = path.map(function(word){
return word[0].toUpperCase() + word.slice(1);
}).join('');
// Define encryption function
schema.statics['encrypt' + fieldName] = function(value, cb) {
return encrypt(field, value, cb);
};
// Define async verification function
schema.methods['verify' + fieldName] = function(value, cb) {
if (Promise) {
var self = this;
return new Promise(function(resolve,reject) {
bcrypt.compare(value, self.get(field), function(err, valid) {
if (cb) {
cb(err, valid);
}
if (err) {
reject(err);
} else {
resolve(valid);
}
});
});
} else {
return bcrypt.compare(value, this.get(field), cb);
}
};
// Define sync verification function
schema.methods['verify' + fieldName + 'Sync'] = function(value) {
return bcrypt.compareSync(value, this.get(field));
};
// Add field to schema if not already defined
if (!schema.path(field)) {
var pwd = { };
var nested = pwd;
for (var i = 0; i < path.length-1; ++i) {
nested[path[i]] = {}
nested = nested[path[i]];
}
nested[path[path.length-1]] = { type: String };
schema.add(pwd);
}
});
// Hash all modified encrypted fields upon saving the model
schema.pre('save', function (next) {
var model = this;
var changed = [];
// Determine list of encrypted fields that have been modified
fields.forEach(function(field){
if (model.isModified(field)) {
changed.push(field);
}
});
// Create/update hash for each modified encrypted field
var count = changed.length;
if (count > 0) {
changed.forEach(function(field){
var value = model.get(field);
if (typeof value === 'string') {
encrypt(field, value, function(err, hash) {
if (err) return next(err);
model.set(field, hash);
if (--count === 0)
next();
});
} else {
model.set(field, '');
if (--count === 0)
next();
}
});
} else {
next();
}
});
function getUpdateField(update, name) {
if (update.hasOwnProperty(name)) {
return update[name];
}
if (update.$set && update.$set.hasOwnProperty(name)) {
return update.$set[name];
}
return undefined
}
function setUpdateField(update, name, value) {
if (update.hasOwnProperty(name)) {
update[name] = value;
} else if (update.$set && update.$set.hasOwnProperty(name)) {
update.$set[name] = value;
}
}
function preUpdate(next) {
var query = this;
var update = query.getUpdate();
var changed = [];
fields.forEach(function(field){
if (getUpdateField(update, field) !== undefined) {
changed.push(field);
}
});
var count = changed.length;
if (count > 0) {
changed.forEach(function(field){
var value = getUpdateField(update, field);
if (typeof value === 'string') {
encrypt(field, value, function(err, hash) {
if (err) return next(err);
setUpdateField(update, field, hash);
if (--count === 0) {
next();
}
});
} else {
setUpdateField(update, field, '');
if (--count === 0) {
next();
}
}
});
} else {
next();
}
}
if (semver.gte(mongoose.version, "4.1.3")) {
schema.pre('update', preUpdate);
schema.pre('updateOne', preUpdate);
schema.pre('updateMany', preUpdate);
schema.pre('findOneAndUpdate', preUpdate);
}
function encrypt(field, value, cb) {
if (Promise) {
return new Promise(function(resolve,reject) {
bcrypt.genSalt(schema.path(field).options.rounds || rounds, function(err, salt) {
if (cb && err) {
cb(err, salt);
}
if (err) {
reject(err);
} else {
bcrypt.hash(value, salt, function(err, result) {
if (cb) {
cb(err, result);
}
if (err) {
reject(err);
} else {
resolve(result);
}
});
}
});
})
} else {
bcrypt.genSalt(schema.path(field).options.rounds || rounds, function(err, salt) {
if (err) {
cb(err);
} else {
bcrypt.hash(value, salt, cb);
}
});
}
}
};