Skip to content

Commit 2553c20

Browse files
committed
fix: enhance decode function to include signature extraction and improve error handling
1 parent 41988d9 commit 2553c20

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

Project/Sources/Classes/JWT.4dm

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Function generate() : Text
4040
$algorithm:="HS"
4141
Else
4242
$algorithm:="RS"
43-
End if
43+
End if
4444

4545
// Generate Verify Signature Hash based on Algorithm
4646
If ($algorithm="HS")
@@ -203,10 +203,16 @@ Function _hashSign($inJWT : Object) : Text
203203
Function decode($inToken : Text) : Object
204204

205205
var $parts : Collection:=Split string($inToken; ".")
206-
var $header; $payload : Text
207-
208-
BASE64 DECODE($parts[0]; $header; *)
209-
BASE64 DECODE($parts[1]; $payload; *)
210206

211-
// Note: If JSON parsing fails, Try(JSON Parse(...)) will return Null for header or payload.
212-
return {header: Try(JSON Parse($header)); payload: Try(JSON Parse($payload))}
207+
If ($parts.length>2)
208+
var $header; $payload; $signature : Text
209+
BASE64 DECODE($parts[0]; $header; *)
210+
BASE64 DECODE($parts[1]; $payload; *)
211+
$signature:=$parts[2]
212+
213+
// Note: If JSON parsing fails, Try(JSON Parse(...)) will return Null for header or payload.
214+
return {header: Try(JSON Parse($header)); payload: Try(JSON Parse($payload)); signature: $signature}
215+
216+
Else
217+
return {header: Null; payload: Null}
218+
End if

0 commit comments

Comments
 (0)