X Tutup
Skip to content

Commit 544398e

Browse files
author
Justin Dahmubed
committed
Add encoding for base16, base32, and default base64
1 parent 085a46f commit 544398e

16 files changed

+574
-3
lines changed

lib/src/main/java/com/auth0/jwt/creators/AccessJwtCreator.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,42 @@ public String sign(Algorithm algorithm) throws Exception {
221221
return JWS;
222222
}
223223

224+
/**
225+
* Creates a new JWT and signs it with the given algorithm.
226+
*
227+
* @param algorithm used to sign the JWT
228+
* @return a new JWT token
229+
* @throws IllegalAccessException if the developer didn't want NONE algorithm to be allowed and it was passed in
230+
* @throws IllegalArgumentException if the provided algorithm is null.
231+
* @throws JWTCreationException if the claims could not be converted to a valid JSON or there was a problem with the signing key.
232+
*/
233+
public String signBase16Encoding(Algorithm algorithm) throws Exception {
234+
if(!jwt.getIsNoneAlgorithmAllowed() && algorithm.equals(Algorithm.none())) {
235+
throw new IllegalAccessException("None algorithm isn't allowed");
236+
}
237+
String JWS = jwt.sign(algorithm, EncodeType.Base16);
238+
verifyClaims();
239+
return JWS;
240+
}
241+
242+
/**
243+
* Creates a new JWT and signs it with the given algorithm.
244+
*
245+
* @param algorithm used to sign the JWT
246+
* @return a new JWT token
247+
* @throws IllegalAccessException if the developer didn't want NONE algorithm to be allowed and it was passed in
248+
* @throws IllegalArgumentException if the provided algorithm is null.
249+
* @throws JWTCreationException if the claims could not be converted to a valid JSON or there was a problem with the signing key.
250+
*/
251+
public String signBase32Encoding(Algorithm algorithm) throws Exception {
252+
if(!jwt.getIsNoneAlgorithmAllowed() && algorithm.equals(Algorithm.none())) {
253+
throw new IllegalAccessException("None algorithm isn't allowed");
254+
}
255+
String JWS = jwt.sign(algorithm, EncodeType.Base32);
256+
verifyClaims();
257+
return JWS;
258+
}
259+
224260
/**
225261
* Verifies that all the standard claims were provided
226262
* @throws Exception if all the standard claims weren't provided
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.auth0.jwt.creators;
2+
3+
public enum EncodeType {
4+
Base16, Base32, Base64;
5+
}

lib/src/main/java/com/auth0/jwt/creators/ExtendedJwtCreator.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,42 @@ public String sign(Algorithm algorithm) throws Exception {
4343
return JWS;
4444
}
4545

46+
/**
47+
* Creates a new JWT and signs it with the given algorithm.
48+
*
49+
* @param algorithm used to sign the JWT
50+
* @return a new JWT token
51+
* @throws IllegalAccessException if the developer didn't want NONE algorithm to be allowed and it was passed in
52+
* @throws IllegalArgumentException if the provided algorithm is null.
53+
* @throws JWTCreationException if the claims could not be converted to a valid JSON or there was a problem with the signing key.
54+
*/
55+
public String signBase16Encoding(Algorithm algorithm) throws Exception {
56+
if(!jwt.getIsNoneAlgorithmAllowed() && algorithm.equals(Algorithm.none())) {
57+
throw new IllegalAccessException("None algorithm isn't allowed");
58+
}
59+
String JWS = jwt.sign(algorithm, EncodeType.Base16);
60+
verifyClaims();
61+
return JWS;
62+
}
63+
64+
/**
65+
* Creates a new JWT and signs it with the given algorithm.
66+
*
67+
* @param algorithm used to sign the JWT
68+
* @return a new JWT token
69+
* @throws IllegalAccessException if the developer didn't want NONE algorithm to be allowed and it was passed in
70+
* @throws IllegalArgumentException if the provided algorithm is null.
71+
* @throws JWTCreationException if the claims could not be converted to a valid JSON or there was a problem with the signing key.
72+
*/
73+
public String signBase32Encoding(Algorithm algorithm) throws Exception {
74+
if(!jwt.getIsNoneAlgorithmAllowed() && algorithm.equals(Algorithm.none())) {
75+
throw new IllegalAccessException("None algorithm isn't allowed");
76+
}
77+
String JWS = jwt.sign(algorithm, EncodeType.Base32);
78+
verifyClaims();
79+
return JWS;
80+
}
81+
4682
/**
4783
* Verifies that all the standard claims were provided
4884
* @throws Exception if all the standard claims weren't provided

lib/src/main/java/com/auth0/jwt/creators/FbJwtCreator.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,42 @@ public String sign(Algorithm algorithm) throws Exception {
202202
return JWS;
203203
}
204204

205+
/**
206+
* Creates a new JWT and signs it with the given algorithm.
207+
*
208+
* @param algorithm used to sign the JWT
209+
* @return a new JWT token
210+
* @throws IllegalAccessException if the developer didn't want NONE algorithm to be allowed and it was passed in
211+
* @throws IllegalArgumentException if the provided algorithm is null.
212+
* @throws JWTCreationException if the claims could not be converted to a valid JSON or there was a problem with the signing key.
213+
*/
214+
public String signBase16Encoding(Algorithm algorithm) throws Exception {
215+
if(!jwt.getIsNoneAlgorithmAllowed() && algorithm.equals(Algorithm.none())) {
216+
throw new IllegalAccessException("None algorithm isn't allowed");
217+
}
218+
String JWS = jwt.sign(algorithm, EncodeType.Base16);
219+
verifyClaims();
220+
return JWS;
221+
}
222+
223+
/**
224+
* Creates a new JWT and signs it with the given algorithm.
225+
*
226+
* @param algorithm used to sign the JWT
227+
* @return a new JWT token
228+
* @throws IllegalAccessException if the developer didn't want NONE algorithm to be allowed and it was passed in
229+
* @throws IllegalArgumentException if the provided algorithm is null.
230+
* @throws JWTCreationException if the claims could not be converted to a valid JSON or there was a problem with the signing key.
231+
*/
232+
public String signBase32Encoding(Algorithm algorithm) throws Exception {
233+
if(!jwt.getIsNoneAlgorithmAllowed() && algorithm.equals(Algorithm.none())) {
234+
throw new IllegalAccessException("None algorithm isn't allowed");
235+
}
236+
String JWS = jwt.sign(algorithm, EncodeType.Base32);
237+
verifyClaims();
238+
return JWS;
239+
}
240+
205241
/**
206242
* Verifies that all the standard claims were provided
207243
* @throws Exception if all the standard claims weren't provided

lib/src/main/java/com/auth0/jwt/creators/GoogleJwtCreator.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,43 @@ public String sign(Algorithm algorithm) throws Exception {
261261
return JWS;
262262
}
263263

264+
/**
265+
* Creates a new JWT and signs it with the given algorithm.
266+
*
267+
* @param algorithm used to sign the JWT
268+
* @return a new JWT token
269+
* @throws IllegalAccessException if the developer didn't want NONE algorithm to be allowed and it was passed in
270+
* @throws IllegalArgumentException if the provided algorithm is null.
271+
* @throws JWTCreationException if the claims could not be converted to a valid JSON or there was a problem with the signing key.
272+
*/
273+
public String signBase16Encoding(Algorithm algorithm) throws Exception {
274+
if(!jwt.getIsNoneAlgorithmAllowed() && algorithm.equals(Algorithm.none())) {
275+
throw new IllegalAccessException("None algorithm isn't allowed");
276+
}
277+
String JWS = jwt.sign(algorithm, EncodeType.Base16);
278+
verifyClaims();
279+
return JWS;
280+
}
281+
282+
/**
283+
* Creates a new JWT and signs it with the given algorithm.
284+
*
285+
* @param algorithm used to sign the JWT
286+
* @return a new JWT token
287+
* @throws IllegalAccessException if the developer didn't want NONE algorithm to be allowed and it was passed in
288+
* @throws IllegalArgumentException if the provided algorithm is null.
289+
* @throws JWTCreationException if the claims could not be converted to a valid JSON or there was a problem with the signing key.
290+
*/
291+
public String signBase32Encoding(Algorithm algorithm) throws Exception {
292+
if(!jwt.getIsNoneAlgorithmAllowed() && algorithm.equals(Algorithm.none())) {
293+
throw new IllegalAccessException("None algorithm isn't allowed");
294+
}
295+
String JWS = jwt.sign(algorithm, EncodeType.Base32);
296+
verifyClaims();
297+
return JWS;
298+
}
299+
300+
264301
/**
265302
* Verifies that all the standard claims were provided
266303
* @throws Exception if all the standard claims weren't provided

lib/src/main/java/com/auth0/jwt/creators/ImplicitJwtCreator.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,42 @@ public String sign(Algorithm algorithm) throws Exception {
207207
return JWS;
208208
}
209209

210+
/**
211+
* Creates a new JWT and signs it with the given algorithm.
212+
*
213+
* @param algorithm used to sign the JWT
214+
* @return a new JWT token
215+
* @throws IllegalAccessException if the developer didn't want NONE algorithm to be allowed and it was passed in
216+
* @throws IllegalArgumentException if the provided algorithm is null.
217+
* @throws JWTCreationException if the claims could not be converted to a valid JSON or there was a problem with the signing key.
218+
*/
219+
public String signBase16Encoding(Algorithm algorithm) throws Exception {
220+
if(!jwt.getIsNoneAlgorithmAllowed() && algorithm.equals(Algorithm.none())) {
221+
throw new IllegalAccessException("None algorithm isn't allowed");
222+
}
223+
String JWS = jwt.sign(algorithm, EncodeType.Base16);
224+
verifyClaims();
225+
return JWS;
226+
}
227+
228+
/**
229+
* Creates a new JWT and signs it with the given algorithm.
230+
*
231+
* @param algorithm used to sign the JWT
232+
* @return a new JWT token
233+
* @throws IllegalAccessException if the developer didn't want NONE algorithm to be allowed and it was passed in
234+
* @throws IllegalArgumentException if the provided algorithm is null.
235+
* @throws JWTCreationException if the claims could not be converted to a valid JSON or there was a problem with the signing key.
236+
*/
237+
public String signBase32Encoding(Algorithm algorithm) throws Exception {
238+
if(!jwt.getIsNoneAlgorithmAllowed() && algorithm.equals(Algorithm.none())) {
239+
throw new IllegalAccessException("None algorithm isn't allowed");
240+
}
241+
String JWS = jwt.sign(algorithm, EncodeType.Base32);
242+
verifyClaims();
243+
return JWS;
244+
}
245+
210246
/**
211247
* Verifies that all the standard claims were provided
212248
* @throws Exception if all the standard claims weren't provided

lib/src/main/java/com/auth0/jwt/creators/JWTCreator.java

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
import com.fasterxml.jackson.databind.MapperFeature;
1111
import com.fasterxml.jackson.databind.ObjectMapper;
1212
import com.fasterxml.jackson.databind.module.SimpleModule;
13+
import org.apache.commons.codec.binary.Base32;
1314
import org.apache.commons.codec.binary.Base64;
15+
import org.apache.commons.codec.binary.Hex;
1416

17+
import java.net.URLEncoder;
1518
import java.nio.charset.StandardCharsets;
1619
import java.util.Date;
1720
import java.util.HashMap;
@@ -312,24 +315,52 @@ public Builder withArrayClaim(String name, Long[] items) throws IllegalArgumentE
312315
}
313316

314317
/**
315-
* Creates a new JWT and signs is with the given algorithm
318+
* Creates a new JWT and signs it with the given algorithm
319+
* Defaults to Base64 encoding
316320
*
317321
* @param algorithm used to sign the JWT
318322
* @return a new JWT token
319323
* @throws IllegalArgumentException if the provided algorithm is null.
320324
* @throws JWTCreationException if the claims could not be converted to a valid JSON or there was a problem with the signing key.
321325
*/
322-
public String sign(Algorithm algorithm) throws Exception {
326+
public String sign(Algorithm algorithm) throws Exception{
327+
return sign(algorithm, EncodeType.Base64);
328+
}
329+
330+
/**
331+
* Creates a new JWT and signs it with the given algorithm
332+
*
333+
* @param algorithm used to sign the JWT
334+
* @param encodeType specifies which base encoding is required
335+
* @return a new JWT token
336+
* @throws IllegalArgumentException if the provided algorithm is null.
337+
* @throws JWTCreationException if the claims could not be converted to a valid JSON or there was a problem with the signing key.
338+
*/
339+
public String sign(Algorithm algorithm, EncodeType encodeType) throws Exception {
323340
if (algorithm == null) {
324341
throw new IllegalArgumentException("The Algorithm cannot be null.");
325342
}
343+
if(encodeType == null) {
344+
throw new IllegalArgumentException("Encodetype cannot be null.");
345+
}
326346
headerClaims.put(PublicClaims.ALGORITHM, algorithm.getName());
327347
headerClaims.put(PublicClaims.TYPE, "JWT");
328348
String signingKeyId = algorithm.getSigningKeyId();
329349
if (signingKeyId != null) {
330350
withKeyId(signingKeyId);
331351
}
332-
return new JWTCreator(algorithm, headerClaims, payloadClaims).sign();
352+
JWTCreator jwtCreator = new JWTCreator(algorithm, headerClaims, payloadClaims);
353+
String token = null;
354+
switch (encodeType) {
355+
case Base16:
356+
token = jwtCreator.signBase16Encoding();
357+
case Base32:
358+
token = jwtCreator.signBase32Encoding();
359+
case Base64:
360+
token = jwtCreator.sign();
361+
}
362+
363+
return token;
333364
}
334365

335366
protected void assertNonNull(String name) {
@@ -347,6 +378,29 @@ private void addClaim(String name, Object value) {
347378
}
348379
}
349380

381+
private String signBase16Encoding() {
382+
String header = Hex.encodeHexString(headerJson.getBytes(StandardCharsets.UTF_8));
383+
String payload = Hex.encodeHexString(payloadJson.getBytes(StandardCharsets.UTF_8));
384+
String content = String.format("%s.%s", header, payload);
385+
386+
byte[] signatureBytes = algorithm.sign(content.getBytes(StandardCharsets.UTF_8));
387+
String signature = Hex.encodeHexString((signatureBytes));
388+
389+
return String.format("%s.%s", content, signature);
390+
}
391+
392+
private String signBase32Encoding() {
393+
Base32 base32 = new Base32();
394+
String header = base32.encodeAsString(headerJson.getBytes(StandardCharsets.UTF_8));
395+
String payload = base32.encodeAsString(payloadJson.getBytes(StandardCharsets.UTF_8));
396+
String content = String.format("%s.%s", header, payload);
397+
398+
byte[] signatureBytes = algorithm.sign(content.getBytes(StandardCharsets.UTF_8));
399+
String signature = base32.encodeAsString((signatureBytes));
400+
401+
return String.format("%s.%s", content, signature);
402+
}
403+
350404
private String sign() throws SignatureGenerationException {
351405
String header = Base64.encodeBase64URLSafeString(headerJson.getBytes(StandardCharsets.UTF_8));
352406
String payload = Base64.encodeBase64URLSafeString(payloadJson.getBytes(StandardCharsets.UTF_8));

lib/src/main/java/com/auth0/jwt/creators/RiscJwtCreator.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,42 @@ public String sign(Algorithm algorithm) throws Exception {
246246
return JWS;
247247
}
248248

249+
/**
250+
* Creates a new JWT and signs it with the given algorithm.
251+
*
252+
* @param algorithm used to sign the JWT
253+
* @return a new JWT token
254+
* @throws IllegalAccessException if the developer didn't want NONE algorithm to be allowed and it was passed in
255+
* @throws IllegalArgumentException if the provided algorithm is null.
256+
* @throws JWTCreationException if the claims could not be converted to a valid JSON or there was a problem with the signing key.
257+
*/
258+
public String signBase16Encoding(Algorithm algorithm) throws Exception {
259+
if(!jwt.getIsNoneAlgorithmAllowed() && algorithm.equals(Algorithm.none())) {
260+
throw new IllegalAccessException("None algorithm isn't allowed");
261+
}
262+
String JWS = jwt.sign(algorithm, EncodeType.Base16);
263+
verifyClaims();
264+
return JWS;
265+
}
266+
267+
/**
268+
* Creates a new JWT and signs it with the given algorithm.
269+
*
270+
* @param algorithm used to sign the JWT
271+
* @return a new JWT token
272+
* @throws IllegalAccessException if the developer didn't want NONE algorithm to be allowed and it was passed in
273+
* @throws IllegalArgumentException if the provided algorithm is null.
274+
* @throws JWTCreationException if the claims could not be converted to a valid JSON or there was a problem with the signing key.
275+
*/
276+
public String signBase32Encoding(Algorithm algorithm) throws Exception {
277+
if(!jwt.getIsNoneAlgorithmAllowed() && algorithm.equals(Algorithm.none())) {
278+
throw new IllegalAccessException("None algorithm isn't allowed");
279+
}
280+
String JWS = jwt.sign(algorithm, EncodeType.Base32);
281+
verifyClaims();
282+
return JWS;
283+
}
284+
249285
/**
250286
* Verifies that all the standard claims were provided
251287
* @throws Exception if all the standard claims weren't provided

0 commit comments

Comments
 (0)
X Tutup