Skip to content
This repository was archived by the owner on Apr 10, 2025. It is now read-only.

Commit c24baf2

Browse files
authored
Create JWT from JSON payload implementation (#87)
1 parent 217a62b commit c24baf2

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

GeneXusJWT/src/main/java/com/genexus/JWT/JWTCreator.java

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ public JWTCreator() {
4646

4747
public String doCreate(String algorithm, PrivateClaims privateClaims, JWTOptions options) {
4848
this.error.cleanError();
49-
return create_Aux(algorithm, privateClaims, options);
49+
return create_Aux(algorithm, privateClaims, options, null, true);
50+
}
51+
52+
public String doCreateFromJSON(String algorithm, String json, JWTOptions options) {
53+
this.error.cleanError();
54+
return create_Aux(algorithm, null, options, json, false);
5055
}
5156

5257
public boolean doVerify(String token, String expectedAlgorithm, PrivateClaims privateClaims, JWTOptions options) {
@@ -104,9 +109,12 @@ public String getTokenID(String token) {
104109

105110
/******** EXTERNAL OBJECT PUBLIC METHODS - END ********/
106111

107-
private String create_Aux(String algorithm, PrivateClaims privateClaims, JWTOptions options) {
108-
if (options == null) {
109-
this.error.setError("JW004", "Options parameter is null");
112+
@SuppressWarnings("unchecked")
113+
private String create_Aux(String algorithm, PrivateClaims privateClaims, JWTOptions options, String payload,
114+
boolean hasClaims) {
115+
if (options == null)
116+
{
117+
this.error.setError("JW000", "Options parameter is null");
110118
return "";
111119
}
112120
JWTAlgorithm alg = JWTAlgorithm.getJWTAlgorithm(algorithm, this.error);
@@ -118,11 +126,24 @@ private String create_Aux(String algorithm, PrivateClaims privateClaims, JWTOpti
118126
HeaderParameters parameters = options.getHeaderParameters();
119127
tokenBuilder.withHeader(parameters.getMap());
120128
}
121-
if (privateClaims == null) {
122-
this.error.setError("JW005", "PrivateClaims parameter is null");
123-
return "";
129+
if (hasClaims) {
130+
if(privateClaims == null)
131+
{
132+
this.error.setError("JW000", "PrivateClaims parameter is null");
133+
return "";
134+
}
135+
tokenBuilder = doBuildPayload(tokenBuilder, privateClaims, options);
136+
} else {
137+
ObjectMapper objectMapper = new ObjectMapper();
138+
HashMap<String, Object> map = null;
139+
try {
140+
map = objectMapper.readValue(payload, HashMap.class);
141+
} catch (Exception e) {
142+
this.error.setError("", e.getMessage());
143+
return "";
144+
}
145+
tokenBuilder.withPayload(map);
124146
}
125-
tokenBuilder = doBuildPayload(tokenBuilder, privateClaims, options);
126147
if (this.hasError()) {
127148
return "";
128149
}
@@ -151,7 +172,7 @@ private String create_Aux(String algorithm, PrivateClaims privateClaims, JWTOpti
151172
try {
152173
signedJwt = tokenBuilder.sign(algorithmType);
153174
} catch (Exception e) {
154-
this.error.setError("JW006", e.getMessage());
175+
this.error.setError("JW003", e.getMessage());
155176
return "";
156177
}
157178

0 commit comments

Comments
 (0)