Skip to content

Commit a2fb017

Browse files
committed
Use thinky events instead of hooks in sample code
1 parent 28d9c38 commit a2fb017

File tree

1 file changed

+16
-28
lines changed

1 file changed

+16
-28
lines changed

README.md

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Encrypted model attributes in your favourite ORM.
1313
* 128-bit authentication tag
1414
* Additional authenticated data:
1515
* Key id: use different keys for different attributes (or different users),
16-
rotate keys over time without re-encrypting all data
16+
rotate keys over time without re-encrypting
1717
* Object id: prevent substitution of encrypted values
1818

1919
All keys should be 32 bytes long, and cryptographically random. Manage these
@@ -28,10 +28,10 @@ node -p "require('crypto').randomBytes(32).toString('base64')"
2828
This is designed to protect you from leaking sensitive user data under very
2929
specific scenarios:
3030

31-
* Full database dump
31+
* Full data dump
3232
* Misplaced unencrypted backups
3333
* Compromised database host
34-
* Partial database dump
34+
* Partial data dump
3535
* Query injection via unsanitized input
3636

3737
Specifically, this does *not* provide any protection in cases of a compromised
@@ -60,7 +60,7 @@ const _ = require('lodash')
6060

6161
let Model = thinky.createModel('Model', {})
6262

63-
Model.encryptedAttributes = EncryptedAttributes(['secret', 'nested.secret'], {
63+
let encryptedAttributes = EncryptedAttributes(['secret', 'nested.secret'], {
6464
keys: {
6565
k1: crypto.randomBytes(32).toString('base64') // use an actual key here
6666
},
@@ -69,40 +69,28 @@ Model.encryptedAttributes = EncryptedAttributes(['secret', 'nested.secret'], {
6969
})
7070

7171
// Pre-save hook: encrypt model attributes that need to be encrypted.
72-
Model.pre('save', function (next) {
73-
try {
74-
Model.encryptedAttributes.encryptAll(this)
75-
process.nextTick(next)
76-
} catch (err) {
77-
process.nextTick(next, err)
78-
}
72+
Model.docOn('saving', function (doc) {
73+
encryptedAttributes.encryptAll(doc)
7974
})
8075

8176
// Post-save hook: decrypt model attributes that need to be decrypted.
82-
Model.post('save', function (next) {
83-
try {
84-
Model.encryptedAttributes.decryptAll(this)
85-
process.nextTick(next)
86-
} catch (err) {
87-
process.nextTick(next, err)
88-
}
77+
Model.docOn('saved', function (doc) {
78+
encryptedAttributes.decryptAll(doc)
8979
})
9080

9181
// Post-retrieve hook: ditto.
92-
Model.post('retrieve', function (next) {
93-
try {
94-
Model.encryptedAttributes.decryptAll(this)
95-
process.nextTick(next)
96-
} catch (err) {
97-
process.nextTick(next, err)
98-
}
82+
Model.on('retrieved', function (doc) {
83+
encryptedAttributes.decryptAll(doc)
9984
})
10085

10186
// Optionally, add some helpers in case we need to set or read the value
10287
// directly (such as an update query), without going through model hooks.
103-
for (let attr of Model.encryptedAttributes.attributes) {
104-
Model.define(_.camelCase(`encrypted ${attr}`), function (val) {
105-
return Model.encryptedAttributes.encryptAttribute(this, val)
88+
for (let attr of encryptedAttributes.attributes) {
89+
Model.define(_.camelCase(`encrypt ${attr}`), function (val) {
90+
return encryptedAttributes.encryptAttribute(this, val)
91+
})
92+
Model.define(_.camelCase(`decrypt ${attr}`), function (val) {
93+
return encryptedAttributes.decryptAttribute(this, val)
10694
})
10795
}
10896
```

0 commit comments

Comments
 (0)