Skip to content

Commit 6ee8265

Browse files
committed
Githug:17805 (JWT)
JWT.validate now uses 4D.CryptoKey.verify()
1 parent 75264d2 commit 6ee8265

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

Project/Sources/Classes/JWT.4dm

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Function decode($inToken : Text) : Object
4242
Function generate($inParams : Object; $inPrivateKey : Text) : Text
4343

4444
var $result : Text:=""
45-
45+
4646
Case of
4747
: ((Value type($inParams.payload)#Is object) || (OB Is empty($inParams.payload)))
4848
This._throwError(9; {which: "\"$inParams.payload\""; function: "JWT.generate"})
@@ -94,14 +94,14 @@ Function generate($inParams : Object; $inPrivateKey : Text) : Text
9494
// ----------------------------------------------------
9595

9696

97-
Function validate($inJWT : Text; $inPrivateKey : Text) : Boolean
97+
Function validate($inJWT : Text; $inKey : Text) : Boolean
9898

9999
Case of
100100
: ((Value type($inJWT)#Is text) || (Length(String($inJWT))=0))
101101
This._throwError(9; {which: "\"$inJWT\""; function: "JWT.validate"})
102102

103-
: ((Value type($inPrivateKey)#Is text) || (Length(String($inPrivateKey))=0))
104-
This._throwError(9; {which: "\"$inPrivateKey\""; function: "JWT.validate"})
103+
: ((Value type($inKey)#Is text) || (Length(String($inKey))=0))
104+
This._throwError(9; {which: "\"$inKey\""; function: "JWT.validate"})
105105

106106
Else
107107
// Split Token into the three parts: Header, Payload, Verify Signature
@@ -110,32 +110,27 @@ Function validate($inJWT : Text; $inPrivateKey : Text) : Boolean
110110
If ($parts.length>2)
111111

112112
var $header; $payload; $signature : Text
113-
var $privateKey : Text:=((Value type($inPrivateKey)=Is text) && (Length($inPrivateKey)>0)) ? $inPrivateKey : ""
113+
var $key : Text:=((Value type($inKey)=Is text) && (Length($inKey)>0)) ? $inKey : ""
114114

115115
// Decode Header and Payload into Objects
116116
BASE64 DECODE($parts[0]; $header; *)
117117
BASE64 DECODE($parts[1]; $payload; *)
118118
var $jwt : Object:={_header: Try(JSON Parse($header)); _payload: Try(JSON Parse($payload))}
119119

120-
// Parse Header for Algorithm Family
121-
var $algorithm : Text:=Substring($jwt._header.alg; 1; 2)
122-
123-
// Generate Hashed Verify Signature
124-
If ($algorithm="HS")
125-
$signature:=This._hashHS($jwt; $privateKey)
126-
Else
127-
$signature:=This._hashSign($jwt; $privateKey)
128-
End if
129-
130120
If (OB Is empty(This._header))
131121
This._header:=$jwt._header
132122
End if
133123
If (OB Is empty(This._payload))
134124
This._payload:=$jwt._payload
135125
End if
136126

137-
//Compare Verify Signatures to return Result
138-
return ($signature=$parts[2])
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
139134

140135
End if
141136
End case

0 commit comments

Comments
 (0)