Skip to content

Commit d8fcb16

Browse files
committed
fix: refactor property access in JWT class for consistency and privacy
1 parent cfe2beb commit d8fcb16

File tree

1 file changed

+37
-34
lines changed

1 file changed

+37
-34
lines changed

Project/Sources/Classes/JWT.4dm

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ Largely inspired by Tech Note: "JSON Web Tokens in 4D" from Thomas Maul
33
See: https://kb.4d.com/assetid=79100
44
*/
55

6-
property header : Object
7-
property payload : Object
8-
property privateKey : Text
6+
property _header : Object
7+
property _payload : Object
8+
property _privateKey : Text
99

1010
Class constructor()
11-
This.header:={}
12-
This.payload:={}
13-
This.privateKey:=""
11+
12+
This._header:={}
13+
This._payload:={}
14+
This._privateKey:=""
1415

1516

1617
// Mark: - [Public]
@@ -28,7 +29,9 @@ Function decode($inToken : Text) : Object
2829
$signature:=$parts[2]
2930

3031
// Note: If JSON parsing fails, Try(JSON Parse(...)) will return Null for header or payload.
31-
return {header: Try(JSON Parse($header)); payload: Try(JSON Parse($payload)); signature: $signature}
32+
This._header:=Try(JSON Parse($header))
33+
This._payload:=Try(JSON Parse($payload))
34+
return {header: This._header; payload: This._payload; signature: $signature}
3235

3336
Else
3437
return {header: Null; payload: Null}
@@ -44,22 +47,22 @@ Function generate($inParams : Object) : Text
4447
var $typ : Text:=(Value type($inParams.header.typ)=Is text) ? $inParams.header.typ : "JWT"
4548
var $x5t : Text:=(Value type($inParams.header.x5t)=Is text) ? $inParams.header.x5t : ""
4649

47-
This.header:={alg: $alg; typ: $typ}
50+
This._header:={alg: $alg; typ: $typ}
4851
If (Length($x5t)>0)
49-
This.header.x5t:=$x5t
52+
This._header.x5t:=$x5t
5053
End if
5154

52-
This.payload:=(Value type($inParams.payload)=Is object) ? $inParams.payload : {}
53-
This.privateKey:=((Value type($inParams.privateKey)=Is text) && (Length($inParams.privateKey)>0)) ? $inParams.privateKey : ""
55+
This._payload:=(Value type($inParams.payload)=Is object) ? $inParams.payload : {}
56+
This._privateKey:=((Value type($inParams.privateKey)=Is text) && (Length($inParams.privateKey)>0)) ? $inParams.privateKey : ""
5457

5558
var $header; $payload; $signature : Text
5659

5760
// Encode the Header and Payload
58-
BASE64 ENCODE(JSON Stringify(This.header); $header; *)
59-
BASE64 ENCODE(JSON Stringify(This.payload); $payload; *)
61+
BASE64 ENCODE(JSON Stringify(This._header); $header; *)
62+
BASE64 ENCODE(JSON Stringify(This._payload); $payload; *)
6063

6164
// Parse Header for Algorithm Family
62-
var $algorithm : Text:=This.header.alg
65+
var $algorithm : Text:=This._header.alg
6366
If (($algorithm="HS256") || ($algorithm="HS512"))
6467
$algorithm:="HS"
6568
Else
@@ -88,15 +91,15 @@ Function validate($inJWT : Text; $inPrivateKey : Text) : Boolean
8891
If ($parts.length>2)
8992

9093
var $header; $payload; $signature : Text
91-
var $privateKey : Text:=((Value type($inPrivateKey)=Is text) && (Length($inPrivateKey)>0)) ? $inPrivateKey : This.privateKey
94+
var $privateKey : Text:=((Value type($inPrivateKey)=Is text) && (Length($inPrivateKey)>0)) ? $inPrivateKey : This._privateKey
9295

9396
// Decode Header and Payload into Objects
9497
BASE64 DECODE($parts[0]; $header; *)
9598
BASE64 DECODE($parts[1]; $payload; *)
96-
var $jwt : Object:={header: Try(JSON Parse($header)); payload: Try(JSON Parse($payload)); privateKey: String($privateKey)}
99+
var $jwt : Object:={_header: Try(JSON Parse($header)); _payload: Try(JSON Parse($payload)); _privateKey: String($privateKey)}
97100

98101
// Parse Header for Algorithm Family
99-
var $algorithm : Text:=Substring($jwt.header.alg; 1; 2)
102+
var $algorithm : Text:=Substring($jwt._header.alg; 1; 2)
100103

101104
// Generate Hashed Verify Signature
102105
If ($algorithm="HS")
@@ -105,11 +108,11 @@ Function validate($inJWT : Text; $inPrivateKey : Text) : Boolean
105108
$signature:=This._hashSign($jwt)
106109
End if
107110

108-
If (OB Is empty(This.header))
109-
This.header:=$jwt.header
111+
If (OB Is empty(This._header))
112+
This._header:=$jwt._header
110113
End if
111-
If (OB Is empty(This.payload))
112-
This.payload:=$jwt.payload
114+
If (OB Is empty(This._payload))
115+
This._payload:=$jwt._payload
113116
End if
114117

115118
//Compare Verify Signatures to return Result
@@ -124,19 +127,19 @@ Function validate($inJWT : Text; $inPrivateKey : Text) : Boolean
124127
// ----------------------------------------------------
125128

126129

127-
Function _hashHS($inJWT : Object) : Text
130+
Function _hashHS($inJWT : cs.NetKit.JWT) : Text
128131

129132
var $encodedHeader; $encodedPayload : Text
130133
var $headerBlob; $payloadBlob; $intermediateBlob; $privateBlob; $dataBlob : Blob
131134
var $blockSize; $i; $byte; $hashAlgorithm : Integer
132135

133136
// Encode Header and Payload to build Message in Blob format
134-
BASE64 ENCODE(JSON Stringify($inJWT.header); $encodedHeader; *)
135-
BASE64 ENCODE(JSON Stringify($inJWT.payload); $encodedPayload; *)
137+
BASE64 ENCODE(JSON Stringify($inJWT._header); $encodedHeader; *)
138+
BASE64 ENCODE(JSON Stringify($inJWT._payload); $encodedPayload; *)
136139
TEXT TO BLOB($encodedHeader+"."+$encodedPayload; $dataBlob; UTF8 text without length)
137140

138141
// Parse Hashing Algorithm From Header
139-
var $algorithm : Text:=Substring($inJWT.header.alg; 3)
142+
var $algorithm : Text:=Substring($inJWT._header.alg; 3)
140143
If ($algorithm="256")
141144
$hashAlgorithm:=SHA256 digest
142145
$blockSize:=64
@@ -146,7 +149,7 @@ Function _hashHS($inJWT : Object) : Text
146149
End if
147150

148151
// Format Secret Key as Blob
149-
TEXT TO BLOB($inJWT.privateKey; $privateBlob; UTF8 text without length)
152+
TEXT TO BLOB($inJWT._privateKey; $privateBlob; UTF8 text without length)
150153

151154
// If Key is larger than Block, Hash the Key to reduce size
152155
If (BLOB size($privateBlob)>$blockSize)
@@ -186,15 +189,15 @@ Function _hashHS($inJWT : Object) : Text
186189
// ----------------------------------------------------
187190

188191

189-
Function _hashSign($inJWT : Object) : Text
192+
Function _hashSign($inJWT : cs.NetKit.JWT) : Text
190193

191194
var $hash; $encodedHead; $encodedPayload : Text
192195
var $settings : Object
193-
var $privateKey : Text:=((Value type($inJWT.privateKey)=Is text) && (Length($inJWT.privateKey)>0)) ? $inJWT.privateKey : ""
196+
var $privateKey : Text:=((Value type($inJWT._privateKey)=Is text) && (Length($inJWT._privateKey)>0)) ? $inJWT._privateKey : ""
194197

195198
// Encode Header and Payload to build Message
196-
BASE64 ENCODE(JSON Stringify($inJWT.header); $encodedHead; *)
197-
BASE64 ENCODE(JSON Stringify($inJWT.payload); $encodedPayload; *)
199+
BASE64 ENCODE(JSON Stringify($inJWT._header); $encodedHead; *)
200+
BASE64 ENCODE(JSON Stringify($inJWT._payload); $encodedPayload; *)
198201

199202
// Prepare CryptoKey settings
200203
If (Length($privateKey)=0)
@@ -206,12 +209,12 @@ Function _hashSign($inJWT : Object) : Text
206209
// Create new CryptoKey
207210
var $cryptoKey : 4D.CryptoKey:=4D.CryptoKey.new($settings)
208211
If ($cryptoKey#Null)
209-
If (Length(This.privateKey)=0)
210-
This.privateKey:=$cryptoKey.getPrivateKey()
212+
If (Length(This._privateKey)=0)
213+
This._privateKey:=$cryptoKey.getPrivateKey()
211214
End if
212215

213216
// Parse Header for Algorithm Family
214-
var $algorithm : Text:=Substring($inJWT.header.alg; 3)
217+
var $algorithm : Text:=Substring($inJWT._header.alg; 3)
215218
var $hashAlgorithm : Integer
216219
If ($algorithm="256")
217220
$hashAlgorithm:=SHA256 digest
@@ -220,7 +223,7 @@ Function _hashSign($inJWT : Object) : Text
220223
End if
221224

222225
// Sign Message with CryptoKey to generate hashed verify signature
223-
$hash:=$cryptoKey.sign(String($encodedHead+"."+$encodedPayload); {hash: $hashAlgorithm; pss: Bool($inJWT.header.alg="PS@"); encoding: "Base64URL"})
226+
$hash:=$cryptoKey.sign(String($encodedHead+"."+$encodedPayload); {hash: $hashAlgorithm; pss: Bool($inJWT._header.alg="PS@"); encoding: "Base64URL"})
224227
End if
225228

226229
return $hash

0 commit comments

Comments
 (0)