@@ -7,26 +7,50 @@ property header : Object
77property payload : Object
88property privateKey : Text
99
10- Class constructor ($inParam : Object)
10+ Class constructor ()
11+ This .header := {}
12+ This .payload := {}
13+ This .privateKey := ""
1114
12- var $alg : Text:= (Value type ($inParam .header .alg )= Is text) ? $inParam .header .alg : "RS256"
13- var $typ : Text:= (Value type ($inParam .header .typ )= Is text) ? $inParam .header .typ : "JWT"
14- var $x5t : Text:= (Value type ($inParam .header .x5t )= Is text) ? $inParam .header .x5t : ""
1515
16- This .header := {alg: $alg ; typ: $typ }
17- If (Length ($x5t )> 0)
18- This .header .x5t := $x5t
19- End if
16+ // Mark: - [Public]
17+ // ----------------------------------------------------
18+
2019
21- This .payload := (Value type ($inParam .payload )= Is object) ? $inParam .payload : {}
22- This .privateKey := (Value type ($inParam .privateKey )= Is text) ? $inParam .privateKey : ""
20+ Function decode ($inToken : Text) : Object
21+
22+ var $parts : Collection:= Split string ($inToken ; "." )
23+
24+ If ($parts .length > 2)
25+ var $header ; $payload ; $signature : Text
26+ BASE64 DECODE ($parts [0 ]; $header ; * )
27+ BASE64 DECODE ($parts [1 ]; $payload ; * )
28+ $signature := $parts [2 ]
29+
30+ // 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+
33+ Else
34+ return {header: Null ; payload: Null }
35+ End if
2336
2437
25- // Mark: - [Public]
2638 // ----------------------------------------------------
2739
2840
29- Function generate () : Text
41+ Function generate ($inParams : Object) : Text
42+
43+ var $alg : Text:= (Value type ($inParams .header .alg )= Is text) ? $inParams .header .alg : "RS256"
44+ var $typ : Text:= (Value type ($inParams .header .typ )= Is text) ? $inParams .header .typ : "JWT"
45+ var $x5t : Text:= (Value type ($inParams .header .x5t )= Is text) ? $inParams .header .x5t : ""
46+
47+ This .header := {alg: $alg ; typ: $typ }
48+ If (Length ($x5t )> 0)
49+ This .header .x5t := $x5t
50+ End if
51+
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 : ""
3054
3155 var $header ; $payload ; $signature : Text
3256
@@ -64,11 +88,12 @@ Function validate($inJWT : Text; $inPrivateKey : Text) : Boolean
6488 If ($parts .length > 2)
6589
6690 var $header ; $payload ; $signature : Text
91+ var $privateKey : Text:= ((Value type ($inPrivateKey )= Is text) && (Length ($inPrivateKey )> 0)) ? $inPrivateKey : This .privateKey
6792
6893 // Decode Header and Payload into Objects
6994 BASE64 DECODE ($parts [0 ]; $header ; * )
7095 BASE64 DECODE ($parts [1 ]; $payload ; * )
71- var $jwt : Object:= {header: Try (JSON Parse ($header )); payload: Try (JSON Parse ($payload )); privateKey: String ($inPrivateKey )}
96+ var $jwt : Object:= {header: Try (JSON Parse ($header )); payload: Try (JSON Parse ($payload )); privateKey: String ($privateKey )}
7297
7398 // Parse Header for Algorithm Family
7499 var $algorithm : Text:= Substring ($jwt .header .alg ; 1 ; 2 )
@@ -80,8 +105,12 @@ Function validate($inJWT : Text; $inPrivateKey : Text) : Boolean
80105 $signature := This ._hashSign ($jwt )
81106 End if
82107
83- This .header := $jwt .header
84- This .payload := $jwt .payload
108+ If (OB Is empty (This .header ))
109+ This .header := $jwt .header
110+ End if
111+ If (OB Is empty (This .payload ))
112+ This .payload := $jwt .payload
113+ End if
85114
86115 // Compare Verify Signatures to return Result
87116 return ($signature= $parts[2 ])
@@ -161,14 +190,14 @@ Function _hashSign($inJWT : Object) : Text
161190
162191 var $hash ; $encodedHead ; $encodedPayload : Text
163192 var $settings : Object
164- var $privateKey : Text:= (String ( $inJWT .privateKey )# "") ? String ( $inJWT .privateKey ) : String ( This .privateKey )
193+ var $privateKey : Text:= (( Value type ( $inJWT .privateKey )= Is text) && ( Length ( $inJWT .privateKey )> 0)) ? $inJWT .privateKey : ""
165194
166195 // Encode Header and Payload to build Message
167196 BASE64 ENCODE (JSON Stringify ($inJWT .header ); $encodedHead ; * )
168197 BASE64 ENCODE (JSON Stringify ($inJWT .payload ); $encodedPayload ; * )
169198
170199 // Prepare CryptoKey settings
171- If ($privateKey= "" )
200+ If (Length ( $privateKey ) = 0 )
172201 $settings := {type: "RSA" } // 4D will automatically create RSA key pair
173202 Else
174203 $settings := {type: "PEM" ; pem: $privateKey } // Use specified PEM format Key
@@ -177,7 +206,7 @@ Function _hashSign($inJWT : Object) : Text
177206 // Create new CryptoKey
178207 var $cryptoKey : 4D.CryptoKey:= 4D.CryptoKey.new($settings)
179208 If ($cryptoKey# Null)
180- If (String (This .privateKey )= "" )
209+ If (Length (This .privateKey )= 0 )
181210 This .privateKey := $cryptoKey .getPrivateKey ()
182211 End if
183212
@@ -195,24 +224,3 @@ Function _hashSign($inJWT : Object) : Text
195224 End if
196225
197226 return $hash
198-
199-
200- // ----------------------------------------------------
201-
202-
203- Function decode ($inToken : Text) : Object
204-
205- var $parts : Collection:= Split string ($inToken ; "." )
206-
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