@@ -46,7 +46,12 @@ public JWTCreator() {
46
46
47
47
public String doCreate (String algorithm , PrivateClaims privateClaims , JWTOptions options ) {
48
48
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 );
50
55
}
51
56
52
57
public boolean doVerify (String token , String expectedAlgorithm , PrivateClaims privateClaims , JWTOptions options ) {
@@ -104,9 +109,12 @@ public String getTokenID(String token) {
104
109
105
110
/******** EXTERNAL OBJECT PUBLIC METHODS - END ********/
106
111
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" );
110
118
return "" ;
111
119
}
112
120
JWTAlgorithm alg = JWTAlgorithm .getJWTAlgorithm (algorithm , this .error );
@@ -118,11 +126,24 @@ private String create_Aux(String algorithm, PrivateClaims privateClaims, JWTOpti
118
126
HeaderParameters parameters = options .getHeaderParameters ();
119
127
tokenBuilder .withHeader (parameters .getMap ());
120
128
}
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 );
124
146
}
125
- tokenBuilder = doBuildPayload (tokenBuilder , privateClaims , options );
126
147
if (this .hasError ()) {
127
148
return "" ;
128
149
}
@@ -151,7 +172,7 @@ private String create_Aux(String algorithm, PrivateClaims privateClaims, JWTOpti
151
172
try {
152
173
signedJwt = tokenBuilder .sign (algorithmType );
153
174
} catch (Exception e ) {
154
- this .error .setError ("JW006 " , e .getMessage ());
175
+ this .error .setError ("JW003 " , e .getMessage ());
155
176
return "" ;
156
177
}
157
178
0 commit comments