Skip to content

Commit cfe2beb

Browse files
committed
fix: refactor JWT instantiation and token generation in _getToken_Service for improved clarity and functionality
1 parent 2553c20 commit cfe2beb

File tree

2 files changed

+50
-44
lines changed

2 files changed

+50
-44
lines changed

Project/Sources/Classes/JWT.4dm

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,50 @@ property header : Object
77
property payload : Object
88
property 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

Project/Sources/Classes/OAuth2Provider.4dm

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ Function _getToken_Service() : Object
574574

575575
var $result : Object:=Null
576576
var $params : cs.URL:=cs.URL.new()
577-
var $jwt : cs.JWT
577+
var $jwt : cs.JWT:=cs.JWT.new()
578578
var $options : Object
579579
var $bearer : Text
580580

@@ -594,8 +594,7 @@ Function _getToken_Service() : Object
594594

595595
$options.privateKey:=This.privateKey
596596

597-
$jwt:=cs.JWT.new($options)
598-
$bearer:=$jwt.generate()
597+
$bearer:=$jwt.generate($options)
599598

600599
$params.addQueryParameter("grant_type"; cs.Tools.me.urlEncode(This.grantType))
601600
$params.addQueryParameter("assertion"; $bearer)
@@ -614,8 +613,7 @@ Function _getToken_Service() : Object
614613

615614
$options.privateKey:=This.privateKey
616615

617-
$jwt:=cs.JWT.new($options)
618-
$bearer:=$jwt.generate()
616+
$bearer:=$jwt.generate($options)
619617

620618
// See documentation of https://learn.microsoft.com/en-us/entra/identity-platform/v2-oauth2-client-creds-grant-flow#second-case-access-token-request-with-a-certificate
621619
$params.addQueryParameter("grant_type"; This.grantType)

0 commit comments

Comments
 (0)