Skip to content

Commit 1bbe8fc

Browse files
committed
Githug:17805 (JWT: Fixes)
1 parent 36be218 commit 1bbe8fc

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

Project/Sources/Classes/JWT.4dm

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

@@ -70,14 +70,9 @@ Function generate($inParams : Object; $inPrivateKey : Text) : Text
7070

7171
// Parse Header for Algorithm Family
7272
var $algorithm : Text:=This._header.alg
73-
If (($algorithm="HS256") || ($algorithm="HS512"))
74-
$algorithm:="HS"
75-
Else
76-
$algorithm:="RS"
77-
End if
7873

7974
// Generate Verify Signature Hash based on Algorithm
80-
If ($algorithm="HS")
75+
If ($algorithm="HS@")
8176
$signature:=This._hashHS(This; $inPrivateKey) // HMAC Hash
8277
Else
8378
$signature:=This._hashSign(This; $inPrivateKey) // All other Hashes
@@ -124,14 +119,20 @@ Function validate($inJWT : Text; $inKey : Text) : Boolean
124119
This._payload:=$jwt._payload
125120
End if
126121

127-
// Prepare CryptoKey settings
128-
var $settings : Object:={type: "PEM"; pem: $key} // Use specified PEM format Key
129-
var $cryptoKey : 4D.CryptoKey:=4D.CryptoKey.new($settings)
130-
If ($cryptoKey#Null)
131-
var $result : Object:=$cryptoKey.verify(String($parts[0]+"."+$parts[1]); $parts[2]; {hash: (Substring($jwt._header.alg; 3)="256") ? SHA256 digest : SHA512 digest; pss: Bool($jwt._header.alg="PS@"); encoding: "Base64URL"})
132-
return Bool($result.success)
122+
var $algorithm : Text:=This._header.alg
123+
If ($algorithm="HS@")
124+
$signature:=This._hashHS($jwt; $key) // HMAC Hash
125+
return ($signature=$parts[2])
126+
Else
127+
// Prepare CryptoKey settings
128+
var $settings : Object:={type: "PEM"; pem: $key} // Use specified PEM format Key
129+
var $cryptoKey : 4D.CryptoKey:=4D.CryptoKey.new($settings)
130+
If ($cryptoKey#Null)
131+
var $result : Object:=$cryptoKey.verify(String($parts[0]+"."+$parts[1]); $parts[2]; {hash: (Substring($jwt._header.alg; 3)="256") ? SHA256 digest : SHA512 digest; pss: Bool($jwt._header.alg="PS@"); encoding: "Base64URL"})
132+
return Bool($result.success)
133+
End if
133134
End if
134-
135+
135136
End if
136137
End case
137138

0 commit comments

Comments
 (0)