Skip to content

Commit a8ef20e

Browse files
committed
refactor: use private instance fields where possible
1 parent 1159b0d commit a8ef20e

File tree

4 files changed

+70
-69
lines changed

4 files changed

+70
-69
lines changed

lib/errors.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,19 @@ class JOSEError extends Error {
3535
}
3636

3737
const isMulti = e => e instanceof JOSEMultiError
38-
const ERRORS = Symbol('ERRORS')
3938
class JOSEMultiError extends JOSEError {
39+
#errors
40+
4041
constructor (errors) {
4142
super()
4243
let i
4344
while ((i = errors.findIndex(isMulti)) && i !== -1) {
4445
errors.splice(i, 1, ...errors[i])
4546
}
46-
this[ERRORS] = errors
47+
this.#errors = errors
4748
}
4849
* [Symbol.iterator] () {
49-
for (const error of this[ERRORS]) {
50+
for (const error of this.#errors) {
5051
yield error
5152
}
5253
}

lib/jwe/encrypt.js

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@ const serializers = require('./serializers')
1515
const generateCEK = require('./generate_cek')
1616
const validateHeaders = require('./validate_headers')
1717

18-
const AAD = Symbol('AAD')
19-
const CEK = Symbol('CEK')
20-
const CLEARTEXT = Symbol('CLEARTEXT')
2118
const PROCESS_RECIPIENT = Symbol('PROCESS_RECIPIENT')
22-
const PROTECTED = Symbol('PROTECTED')
23-
const RECIPIENTS = Symbol('RECIPIENTS')
24-
const UNPROTECTED = Symbol('UNPROTECTED')
2519

2620
class Encrypt {
21+
#aad
22+
#cek
23+
#unprotected
24+
#protected
25+
#cleartext
26+
#recipients
27+
2728
constructor (cleartext, protectedHeader, unprotectedHeader, aad) {
2829
if (!Buffer.isBuffer(cleartext) && typeof cleartext !== 'string') {
2930
throw new TypeError('cleartext argument must be a Buffer or a string')
@@ -43,13 +44,11 @@ class Encrypt {
4344
throw new TypeError('unprotectedHeader argument must be a plain object when provided')
4445
}
4546

46-
Object.assign(this, {
47-
[CLEARTEXT]: cleartext,
48-
[RECIPIENTS]: [],
49-
[PROTECTED]: protectedHeader ? deepClone(protectedHeader) : undefined,
50-
[UNPROTECTED]: unprotectedHeader ? deepClone(unprotectedHeader) : undefined,
51-
[AAD]: aad
52-
})
47+
this.#recipients = []
48+
this.#cleartext = cleartext
49+
this.#aad = aad
50+
this.#unprotected = unprotectedHeader ? deepClone(unprotectedHeader) : undefined
51+
this.#protected = protectedHeader ? deepClone(protectedHeader) : undefined
5352
}
5453

5554
/*
@@ -64,7 +63,7 @@ class Encrypt {
6463
throw new TypeError('header argument must be a plain object when provided')
6564
}
6665

67-
this[RECIPIENTS].push({
66+
this.#recipients.push({
6867
key,
6968
header: header ? deepClone(header) : undefined
7069
})
@@ -76,7 +75,9 @@ class Encrypt {
7675
* @private
7776
*/
7877
[PROCESS_RECIPIENT] (recipient) {
79-
const { [PROTECTED]: protectedHeader, [UNPROTECTED]: unprotectedHeader, [RECIPIENTS]: { length: recipientCount } } = this
78+
const unprotectedHeader = this.#unprotected
79+
const protectedHeader = this.#protected
80+
const { length: recipientCount } = this.#recipients
8081

8182
const jweHeader = {
8283
...protectedHeader,
@@ -107,7 +108,7 @@ class Encrypt {
107108
if (protectedHeader) {
108109
protectedHeader.alg = alg
109110
} else {
110-
this[PROTECTED] = { alg }
111+
this.#protected = { alg }
111112
}
112113
} else {
113114
if (recipient.header) {
@@ -122,11 +123,11 @@ class Encrypt {
122123
let generatedHeader
123124

124125
if (key.kty === 'oct' && alg === 'dir') {
125-
this[CEK] = importKey(key[KEYOBJECT], { use: 'enc', alg: enc })
126+
this.#cek = importKey(key[KEYOBJECT], { use: 'enc', alg: enc })
126127
} else {
127-
({ wrapped, header: generatedHeader } = wrapKey(alg, key, this[CEK][KEYOBJECT].export(), { enc, alg }))
128+
({ wrapped, header: generatedHeader } = wrapKey(alg, key, this.#cek[KEYOBJECT].export(), { enc, alg }))
128129
if (alg === 'ECDH-ES') {
129-
this[CEK] = importKey(createSecretKey(wrapped), { use: 'enc', alg: enc })
130+
this.#cek = importKey(createSecretKey(wrapped), { use: 'enc', alg: enc })
130131
}
131132
}
132133

@@ -150,58 +151,58 @@ class Encrypt {
150151
throw new TypeError('serialization must be one of "compact", "flattened", "general"')
151152
}
152153

153-
if (!this[RECIPIENTS].length) {
154+
if (!this.#recipients.length) {
154155
throw new JWEInvalid('missing recipients')
155156
}
156157

157-
serializer.validate(this[PROTECTED], this[UNPROTECTED], this[AAD], this[RECIPIENTS])
158+
serializer.validate(this.#protected, this.#unprotected, this.#aad, this.#recipients)
158159

159-
let enc = validateHeaders(this[PROTECTED], this[UNPROTECTED], this[RECIPIENTS], false, this[PROTECTED] ? this[PROTECTED].crit : undefined)
160+
let enc = validateHeaders(this.#protected, this.#unprotected, this.#recipients, false, this.#protected ? this.#protected.crit : undefined)
160161
if (!enc) {
161162
enc = 'A128CBC-HS256'
162-
if (this[PROTECTED]) {
163-
this[PROTECTED].enc = enc
163+
if (this.#protected) {
164+
this.#protected.enc = enc
164165
} else {
165-
this[PROTECTED] = { enc }
166+
this.#protected = { enc }
166167
}
167168
}
168169
const final = {}
169-
this[CEK] = generateCEK(enc)
170+
this.#cek = generateCEK(enc)
170171

171-
this[RECIPIENTS].forEach(this[PROCESS_RECIPIENT].bind(this))
172+
this.#recipients.forEach(this[PROCESS_RECIPIENT].bind(this))
172173

173174
const iv = generateIV(enc)
174175
final.iv = base64url.encodeBuffer(iv)
175176

176-
if (this[RECIPIENTS].length === 1 && this[RECIPIENTS][0].generatedHeader) {
177-
const [{ generatedHeader }] = this[RECIPIENTS]
178-
delete this[RECIPIENTS][0].generatedHeader
179-
this[PROTECTED] = Object.assign({}, this[PROTECTED], generatedHeader)
177+
if (this.#recipients.length === 1 && this.#recipients[0].generatedHeader) {
178+
const [{ generatedHeader }] = this.#recipients
179+
delete this.#recipients[0].generatedHeader
180+
this.#protected = Object.assign({}, this.#protected, generatedHeader)
180181
}
181182

182-
if (this[PROTECTED]) {
183-
final.protected = base64url.JSON.encode(this[PROTECTED])
183+
if (this.#protected) {
184+
final.protected = base64url.JSON.encode(this.#protected)
184185
}
185-
final.unprotected = this[UNPROTECTED]
186+
final.unprotected = this.#unprotected
186187

187188
let aad
188-
if (this[AAD]) {
189-
final.aad = base64url.encode(this[AAD])
189+
if (this.#aad) {
190+
final.aad = base64url.encode(this.#aad)
190191
aad = Buffer.concat([Buffer.from(final.protected || ''), Buffer.from('.'), Buffer.from(final.aad)])
191192
} else {
192193
aad = Buffer.from(final.protected || '')
193194
}
194195

195-
let cleartext = this[CLEARTEXT]
196-
if (this[PROTECTED] && 'zip' in this[PROTECTED]) {
196+
let cleartext = this.#cleartext
197+
if (this.#protected && 'zip' in this.#protected) {
197198
cleartext = deflateRawSync(cleartext)
198199
}
199200

200-
const { ciphertext, tag } = encrypt(enc, this[CEK], cleartext, { iv, aad })
201+
const { ciphertext, tag } = encrypt(enc, this.#cek, cleartext, { iv, aad })
201202
final.tag = base64url.encodeBuffer(tag)
202203
final.ciphertext = base64url.encodeBuffer(ciphertext)
203204

204-
return serializer(final, this[RECIPIENTS])
205+
return serializer(final, this.#recipients)
205206
}
206207
}
207208

lib/jwks/keystore.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ const { generate, generateSync } = require('../jwk/generate')
33
const Key = require('../jwk/key/base')
44
const importKey = require('../jwk/import')
55

6-
const KEYS = Symbol('keys')
7-
86
const keyscore = (key, { alg, kid, use }) => {
97
let score = 0
108

@@ -24,6 +22,8 @@ const keyscore = (key, { alg, kid, use }) => {
2422
}
2523

2624
class KeyStore {
25+
#keys
26+
2727
constructor (...keys) {
2828
while (keys.some(Array.isArray)) {
2929
keys = keys.flat()
@@ -32,7 +32,7 @@ class KeyStore {
3232
throw new TypeError('all keys must be an instances of a key instantiated by JWK.importKey')
3333
}
3434

35-
Object.defineProperty(this, KEYS, { value: new Set(keys) })
35+
this.#keys = new Set(keys);
3636
}
3737

3838
static fromJWKS (jwks) {
@@ -46,7 +46,7 @@ class KeyStore {
4646
}
4747

4848
all ({ alg, kid, use, kty, operation } = {}) {
49-
return [...this[KEYS]]
49+
return [...this.#keys]
5050
.filter((key) => {
5151
let candidate = true
5252

@@ -80,31 +80,31 @@ class KeyStore {
8080
throw new TypeError('key must be an instance of a key instantiated by JWK.importKey')
8181
}
8282

83-
this[KEYS].add(key)
83+
this.#keys.add(key)
8484
}
8585

8686
remove (key) {
8787
if (!(key instanceof Key)) {
8888
throw new TypeError('key must be an instance of a key instantiated by JWK.importKey')
8989
}
9090

91-
this[KEYS].delete(key)
91+
this.#keys.delete(key)
9292
}
9393

9494
toJWKS (priv = false) {
95-
return { keys: [...this[KEYS].values()].map(key => key.toJWK(priv)) }
95+
return { keys: [...this.#keys.values()].map(key => key.toJWK(priv)) }
9696
}
9797

9898
async generate (...args) {
99-
this[KEYS].add(await generate(...args))
99+
this.#keys.add(await generate(...args))
100100
}
101101

102102
generateSync (...args) {
103-
this[KEYS].add(generateSync(...args))
103+
this.#keys.add(generateSync(...args))
104104
}
105105

106106
get size () {
107-
return this[KEYS].size
107+
return this.#keys.size
108108
}
109109
}
110110

lib/jws/sign.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ const { check, sign } = require('../jwa')
88

99
const serializers = require('./serializers')
1010

11-
const RECIPIENTS = Symbol('RECIPIENTS')
12-
const PAYLOAD = Symbol('PAYLOAD')
1311
const PROCESS_RECIPIENT = Symbol('PROCESS_RECIPIENT')
14-
const B64 = Symbol('b64')
1512

1613
class Sign {
14+
#b64
15+
#payload
16+
#recipients
17+
1718
constructor (payload) {
1819
if (typeof payload === 'string') {
1920
payload = base64url.encode(payload)
@@ -25,10 +26,8 @@ class Sign {
2526
throw new TypeError('payload argument must be a Buffer, string or an object')
2627
}
2728

28-
Object.assign(this, {
29-
[PAYLOAD]: payload,
30-
[RECIPIENTS]: []
31-
})
29+
this.#payload = payload
30+
this.#recipients = []
3231
}
3332

3433
/*
@@ -51,7 +50,7 @@ class Sign {
5150
throw new JWSInvalid('JWS Protected and JWS Unprotected Header Parameter names must be disjoint')
5251
}
5352

54-
this[RECIPIENTS].push({
53+
this.#recipients.push({
5554
key,
5655
protectedHeader: protectedHeader ? deepClone(protectedHeader) : undefined,
5756
unprotectedHeader: unprotectedHeader ? deepClone(unprotectedHeader) : undefined
@@ -89,19 +88,19 @@ class Sign {
8988
}
9089

9190
if (joseHeader.protected.crit && joseHeader.protected.crit.includes('b64')) {
92-
if (B64 in this && this[B64] !== joseHeader.protected.b64) {
91+
if (this.#b64 !== undefined && this.#b64 !== joseHeader.protected.b64) {
9392
throw new JWSInvalid('the "b64" Header Parameter value MUST be the same for all recipients')
9493
} else {
95-
this[B64] = joseHeader.protected.b64
94+
this.#b64 = joseHeader.protected.b64
9695
}
9796
if (!joseHeader.protected.b64) {
98-
this[PAYLOAD] = base64url.decode(this[PAYLOAD])
97+
this.#payload = base64url.decode(this.#payload)
9998
}
10099
}
101100

102101
recipient.header = unprotectedHeader
103102
recipient.protected = Object.keys(joseHeader.protected).length ? base64url.JSON.encode(joseHeader.protected) : ''
104-
recipient.signature = base64url.encodeBuffer(sign(alg, key, Buffer.from(`${recipient.protected}.${this[PAYLOAD]}`)))
103+
recipient.signature = base64url.encodeBuffer(sign(alg, key, Buffer.from(`${recipient.protected}.${this.#payload}`)))
105104
}
106105

107106
/*
@@ -113,15 +112,15 @@ class Sign {
113112
throw new TypeError('serialization must be one of "compact", "flattened", "general"')
114113
}
115114

116-
if (!this[RECIPIENTS].length) {
115+
if (!this.#recipients.length) {
117116
throw new JWSInvalid('missing recipients')
118117
}
119118

120-
serializer.validate(this, this[RECIPIENTS])
119+
serializer.validate(this, this.#recipients)
121120

122-
this[RECIPIENTS].forEach(this[PROCESS_RECIPIENT].bind(this))
121+
this.#recipients.forEach(this[PROCESS_RECIPIENT].bind(this))
123122

124-
return serializer(this[PAYLOAD], this[RECIPIENTS])
123+
return serializer(this.#payload, this.#recipients)
125124
}
126125
}
127126

0 commit comments

Comments
 (0)