Skip to content

Commit c862061

Browse files
committed
refactor algorithm parameter order in the create method
1 parent 92fa94f commit c862061

5 files changed

Lines changed: 54 additions & 58 deletions

File tree

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ If the token has an invalid syntax or the header or payload are not JSONs, a `JW
5050

5151
### Create and Sign a Token
5252

53-
You'll first need to create a `JWTCreator` instance by calling `JWT.create()` and passing the Algorithm instance. Use the builder to define the custom Claims your token needs to have. Finally to get the String token call `sign()`.
53+
You'll first need to create a `JWTCreator` instance by calling `JWT.create()`. Use the builder to define the custom Claims your token needs to have. Finally to get the String token call `sign()` and pass the Algorithm instance.
5454

5555
* Example using `HS256`
5656

5757
```java
5858
try {
59-
String token = JWT.create(Algorithm.HMAC256("secret"))
59+
String token = JWT.create()
6060
.withIssuer("auth0")
61-
.sign();
61+
.sign(Algorithm.HMAC256("secret"));
6262
} catch (JWTCreationException exception){
6363
//Invalid Signing configuration / Couldn't convert Claims.
6464
}
@@ -69,9 +69,9 @@ try {
6969
```java
7070
PrivateKey key = //Get the key instance
7171
try {
72-
String token = JWT.create(Algorithm.RSA256(key))
72+
String token = JWT.create()
7373
.withIssuer("auth0")
74-
.sign();
74+
.sign(Algorithm.RSA256(key));
7575
} catch (JWTCreationException exception){
7676
//Invalid Signing configuration / Couldn't convert Claims.
7777
}

lib/src/main/java/com/auth0/jwt/JWT.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,10 @@ public static JWTVerifier.Verification require(Algorithm algorithm) throws Illeg
4040
/**
4141
* Creates a Builder instance to configure and construct a Token using the given Algorithm.
4242
*
43-
* @param algorithm the Algorithm to use in JWT signing.
4443
* @return a Builder instance to configure.
45-
* @throws IllegalArgumentException if the provided algorithm is null.
4644
*/
47-
public static JWTCreator.Builder create(Algorithm algorithm) throws IllegalArgumentException {
48-
return JWTCreator.init(algorithm);
45+
public static JWTCreator.Builder create() {
46+
return JWTCreator.init();
4947
}
5048

5149
@Override

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,22 @@ private JWTCreator(Algorithm algorithm, Map<String, Object> headerClaims, Map<St
3232

3333

3434
/**
35-
* Initialize a JWTCreator instance using the given Algorithm.
35+
* Initialize a JWTCreator instance.
3636
*
37-
* @param algorithm the Algorithm to use on the JWT signing.
3837
* @return a JWTCreator.Builder instance to configure.
39-
* @throws IllegalArgumentException if the provided algorithm is null.
4038
*/
41-
static JWTCreator.Builder init(Algorithm algorithm) throws IllegalArgumentException {
42-
return new Builder(algorithm);
39+
static JWTCreator.Builder init() {
40+
return new Builder();
4341
}
4442

4543
/**
4644
* The Builder class holds the Claims that defines the JWT to be created.
4745
*/
4846
static class Builder {
49-
private final Algorithm algorithm;
5047
private final Map<String, Object> payloadClaims;
5148
private Map<String, Object> headerClaims;
5249

53-
Builder(Algorithm algorithm) throws IllegalArgumentException {
54-
if (algorithm == null) {
55-
throw new IllegalArgumentException("The Algorithm cannot be null.");
56-
}
57-
58-
this.algorithm = algorithm;
50+
Builder() {
5951
this.payloadClaims = new HashMap<>();
6052
this.headerClaims = new HashMap<>();
6153
}
@@ -148,10 +140,15 @@ public Builder withJWTId(String jwtId) {
148140
/**
149141
* Creates a new instance of the JWT with the specified payloadClaims.
150142
*
143+
* @param algorithm the Algorithm to use on the JWT signing.
151144
* @return a new JWT instance.
152-
* @throws JWTCreationException if the Claims coudln't be converted to a valid JSON or there was a problem with the signing key.
145+
* @throws IllegalArgumentException if the provided algorithm is null.
146+
* @throws JWTCreationException if the Claims coudln't be converted to a valid JSON or there was a problem with the signing key.
153147
*/
154-
public String sign() throws JWTCreationException {
148+
public String sign(Algorithm algorithm) throws IllegalArgumentException, JWTCreationException {
149+
if (algorithm == null) {
150+
throw new IllegalArgumentException("The Algorithm cannot be null.");
151+
}
155152
headerClaims.put(PublicClaims.ALGORITHM, algorithm.getName());
156153
return new JWTCreator(algorithm, headerClaims, payloadClaims).sign();
157154
}

lib/src/test/java/com/auth0/jwt/JWTCreatorTest.java

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,98 +19,99 @@ public class JWTCreatorTest {
1919
public ExpectedException exception = ExpectedException.none();
2020

2121
@Test
22-
public void shouldThrowWhenInitializedWithoutAlgorithm() throws Exception {
22+
public void shouldThrowWhenRequestingSignWithoutAlgorithm() throws Exception {
2323
exception.expect(IllegalArgumentException.class);
2424
exception.expectMessage("The Algorithm cannot be null");
25-
JWTCreator.init(null);
25+
JWTCreator.init()
26+
.sign(null);
2627
}
2728

2829
@SuppressWarnings("Convert2Diamond")
2930
@Test
3031
public void shouldAddHeader() throws Exception {
3132
Map<String, Object> header = new HashMap<String, Object>();
3233
header.put("asd", 123);
33-
String signed = JWTCreator.init(Algorithm.HMAC256("secret"))
34+
String signed = JWTCreator.init()
3435
.withHeader(header)
35-
.sign();
36+
.sign(Algorithm.HMAC256("secret"));
3637

3738
assertThat(signed, is(notNullValue()));
3839
assertThat(SignUtils.splitToken(signed)[0], is("eyJhbGciOiJIUzI1NiIsImFzZCI6MTIzfQ"));
3940
}
4041

4142
@Test
4243
public void shouldAddIssuer() throws Exception {
43-
String signed = JWTCreator.init(Algorithm.HMAC256("secret"))
44+
String signed = JWTCreator.init()
4445
.withIssuer("auth0")
45-
.sign();
46+
.sign(Algorithm.HMAC256("secret"));
4647

4748
assertThat(signed, is(notNullValue()));
4849
assertThat(SignUtils.splitToken(signed)[1], is("eyJpc3MiOiJhdXRoMCJ9"));
4950
}
5051

5152
@Test
5253
public void shouldAddSubject() throws Exception {
53-
String signed = JWTCreator.init(Algorithm.HMAC256("secret"))
54+
String signed = JWTCreator.init()
5455
.withSubject("1234567890")
55-
.sign();
56+
.sign(Algorithm.HMAC256("secret"));
5657

5758
assertThat(signed, is(notNullValue()));
5859
assertThat(SignUtils.splitToken(signed)[1], is("eyJzdWIiOiIxMjM0NTY3ODkwIn0"));
5960
}
6061

6162
@Test
6263
public void shouldAddAudience() throws Exception {
63-
String signed = JWTCreator.init(Algorithm.HMAC256("secret"))
64+
String signed = JWTCreator.init()
6465
.withAudience(new String[]{"Mark"})
65-
.sign();
66+
.sign(Algorithm.HMAC256("secret"));
6667

6768
assertThat(signed, is(notNullValue()));
6869
assertThat(SignUtils.splitToken(signed)[1], is("eyJhdWQiOiJNYXJrIn0"));
6970

7071

71-
String signedArr = JWTCreator.init(Algorithm.HMAC256("secret"))
72+
String signedArr = JWTCreator.init()
7273
.withAudience(new String[]{"Mark", "David"})
73-
.sign();
74+
.sign(Algorithm.HMAC256("secret"));
7475

7576
assertThat(signedArr, is(notNullValue()));
7677
assertThat(SignUtils.splitToken(signedArr)[1], is("eyJhdWQiOlsiTWFyayIsIkRhdmlkIl19"));
7778
}
7879

7980
@Test
8081
public void shouldAddExpiresAt() throws Exception {
81-
String signed = JWTCreator.init(Algorithm.HMAC256("secret"))
82+
String signed = JWTCreator.init()
8283
.withExpiresAt(new Date(1477592000))
83-
.sign();
84+
.sign(Algorithm.HMAC256("secret"));
8485

8586
assertThat(signed, is(notNullValue()));
8687
assertThat(SignUtils.splitToken(signed)[1], is("eyJleHAiOjE0Nzc1OTJ9"));
8788
}
8889

8990
@Test
9091
public void shouldAddNotBefore() throws Exception {
91-
String signed = JWTCreator.init(Algorithm.HMAC256("secret"))
92+
String signed = JWTCreator.init()
9293
.withNotBefore(new Date(1477592000))
93-
.sign();
94+
.sign(Algorithm.HMAC256("secret"));
9495

9596
assertThat(signed, is(notNullValue()));
9697
assertThat(SignUtils.splitToken(signed)[1], is("eyJuYmYiOjE0Nzc1OTJ9"));
9798
}
9899

99100
@Test
100101
public void shouldAddIssuedAt() throws Exception {
101-
String signed = JWTCreator.init(Algorithm.HMAC256("secret"))
102+
String signed = JWTCreator.init()
102103
.withIssuedAt(new Date(1477592000))
103-
.sign();
104+
.sign(Algorithm.HMAC256("secret"));
104105

105106
assertThat(signed, is(notNullValue()));
106107
assertThat(SignUtils.splitToken(signed)[1], is("eyJpYXQiOjE0Nzc1OTJ9"));
107108
}
108109

109110
@Test
110111
public void shouldAddJWTId() throws Exception {
111-
String signed = JWTCreator.init(Algorithm.HMAC256("secret"))
112+
String signed = JWTCreator.init()
112113
.withJWTId("jwt_id_123")
113-
.sign();
114+
.sign(Algorithm.HMAC256("secret"));
114115

115116
assertThat(signed, is(notNullValue()));
116117
assertThat(SignUtils.splitToken(signed)[1], is("eyJqdGkiOiJqd3RfaWRfMTIzIn0"));
@@ -119,28 +120,28 @@ public void shouldAddJWTId() throws Exception {
119120

120121
@Test
121122
public void shouldRemoveClaimWhenPassingNull() throws Exception {
122-
String signed = JWTCreator.init(Algorithm.HMAC256("secret"))
123+
String signed = JWTCreator.init()
123124
.withIssuer("iss")
124125
.withIssuer(null)
125-
.sign();
126+
.sign(Algorithm.HMAC256("secret"));
126127

127128
assertThat(signed, is(notNullValue()));
128129
assertThat(SignUtils.splitToken(signed)[1], is("e30"));
129130
}
130131

131132
@Test
132133
public void shouldSetCorrectAlgorithmInTheHeader() throws Exception {
133-
String signed = JWTCreator.init(Algorithm.HMAC256("secret"))
134-
.sign();
134+
String signed = JWTCreator.init()
135+
.sign(Algorithm.HMAC256("secret"));
135136

136137
assertThat(signed, is(notNullValue()));
137138
assertThat(SignUtils.splitToken(signed)[0], is("eyJhbGciOiJIUzI1NiJ9"));
138139
}
139140

140141
@Test
141142
public void shouldSetEmptySignatureIfAlgorithmIsNone() throws Exception {
142-
String signed = JWTCreator.init(Algorithm.none())
143-
.sign();
143+
String signed = JWTCreator.init()
144+
.sign(Algorithm.none());
144145
assertThat(signed, is(notNullValue()));
145146
assertThat(SignUtils.splitToken(signed)[2], is(""));
146147
}

lib/src/test/java/com/auth0/jwt/JWTTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ public void shouldGetType() throws Exception {
307307
public void shouldCreateAnEmptyHMAC256SignedToken() throws Exception {
308308
String headerAndPayload = "eyJhbGciOiJIUzI1NiJ9.e30.";
309309

310-
String signed = JWT.create(Algorithm.HMAC256("secret")).sign();
310+
String signed = JWT.create().sign(Algorithm.HMAC256("secret"));
311311
assertThat(signed, is(notNullValue()));
312312
assertThat(signed, startsWith(headerAndPayload));
313313

@@ -320,7 +320,7 @@ public void shouldCreateAnEmptyHMAC256SignedToken() throws Exception {
320320
public void shouldCreateAnEmptyHMAC384SignedToken() throws Exception {
321321
String headerAndPayload = "eyJhbGciOiJIUzM4NCJ9.e30.";
322322

323-
String signed = JWT.create(Algorithm.HMAC384("secret")).sign();
323+
String signed = JWT.create().sign(Algorithm.HMAC384("secret"));
324324
assertThat(signed, is(notNullValue()));
325325
assertThat(signed, startsWith(headerAndPayload));
326326

@@ -333,7 +333,7 @@ public void shouldCreateAnEmptyHMAC384SignedToken() throws Exception {
333333
public void shouldCreateAnEmptyHMAC512SignedToken() throws Exception {
334334
String headerAndPayload = "eyJhbGciOiJIUzUxMiJ9.e30.";
335335

336-
String signed = JWT.create(Algorithm.HMAC512("secret")).sign();
336+
String signed = JWT.create().sign(Algorithm.HMAC512("secret"));
337337
assertThat(signed, is(notNullValue()));
338338
assertThat(signed, startsWith(headerAndPayload));
339339

@@ -346,7 +346,7 @@ public void shouldCreateAnEmptyHMAC512SignedToken() throws Exception {
346346
public void shouldCreateAnEmptyRSA256SignedToken() throws Exception {
347347
String headerAndPayload = "eyJhbGciOiJSUzI1NiJ9.e30.";
348348

349-
String signed = JWT.create(Algorithm.RSA256((RSAKey) PemUtils.readPrivateKeyFromFile(PRIVATE_KEY_FILE_RSA, "RSA"))).sign();
349+
String signed = JWT.create().sign(Algorithm.RSA256((RSAKey) PemUtils.readPrivateKeyFromFile(PRIVATE_KEY_FILE_RSA, "RSA")));
350350
assertThat(signed, is(notNullValue()));
351351
assertThat(signed, startsWith(headerAndPayload));
352352

@@ -359,7 +359,7 @@ public void shouldCreateAnEmptyRSA256SignedToken() throws Exception {
359359
public void shouldCreateAnEmptyRSA384SignedToken() throws Exception {
360360
String headerAndPayload = "eyJhbGciOiJSUzM4NCJ9.e30.";
361361

362-
String signed = JWT.create(Algorithm.RSA384((RSAKey) PemUtils.readPrivateKeyFromFile(PRIVATE_KEY_FILE_RSA, "RSA"))).sign();
362+
String signed = JWT.create().sign(Algorithm.RSA384((RSAKey) PemUtils.readPrivateKeyFromFile(PRIVATE_KEY_FILE_RSA, "RSA")));
363363
assertThat(signed, is(notNullValue()));
364364
assertThat(signed, startsWith(headerAndPayload));
365365

@@ -372,7 +372,7 @@ public void shouldCreateAnEmptyRSA384SignedToken() throws Exception {
372372
public void shouldCreateAnEmptyRSA512SignedToken() throws Exception {
373373
String headerAndPayload = "eyJhbGciOiJSUzUxMiJ9.e30.";
374374

375-
String signed = JWT.create(Algorithm.RSA512((RSAKey) PemUtils.readPrivateKeyFromFile(PRIVATE_KEY_FILE_RSA, "RSA"))).sign();
375+
String signed = JWT.create().sign(Algorithm.RSA512((RSAKey) PemUtils.readPrivateKeyFromFile(PRIVATE_KEY_FILE_RSA, "RSA")));
376376
assertThat(signed, is(notNullValue()));
377377
assertThat(signed, startsWith(headerAndPayload));
378378

@@ -385,7 +385,7 @@ public void shouldCreateAnEmptyRSA512SignedToken() throws Exception {
385385
public void shouldCreateAnEmptyECDSA256SignedToken() throws Exception {
386386
String headerAndPayload = "eyJhbGciOiJFUzI1NiJ9.e30.";
387387

388-
String signed = JWT.create(Algorithm.ECDSA256((ECKey) PemUtils.readPrivateKeyFromFile(PRIVATE_KEY_FILE_EC_256, "EC"))).sign();
388+
String signed = JWT.create().sign(Algorithm.ECDSA256((ECKey) PemUtils.readPrivateKeyFromFile(PRIVATE_KEY_FILE_EC_256, "EC")));
389389
assertThat(signed, is(notNullValue()));
390390
assertThat(signed, startsWith(headerAndPayload));
391391

@@ -398,7 +398,7 @@ public void shouldCreateAnEmptyECDSA256SignedToken() throws Exception {
398398
public void shouldCreateAnEmptyECDSA384SignedToken() throws Exception {
399399
String headerAndPayload = "eyJhbGciOiJFUzM4NCJ9.e30.";
400400

401-
String signed = JWT.create(Algorithm.ECDSA384((ECKey) PemUtils.readPrivateKeyFromFile(PRIVATE_KEY_FILE_EC_384, "EC"))).sign();
401+
String signed = JWT.create().sign(Algorithm.ECDSA384((ECKey) PemUtils.readPrivateKeyFromFile(PRIVATE_KEY_FILE_EC_384, "EC")));
402402
assertThat(signed, is(notNullValue()));
403403
assertThat(signed, startsWith(headerAndPayload));
404404

@@ -411,7 +411,7 @@ public void shouldCreateAnEmptyECDSA384SignedToken() throws Exception {
411411
public void shouldCreateAnEmptyECDSA512SignedToken() throws Exception {
412412
String headerAndPayload = "eyJhbGciOiJFUzUxMiJ9.e30.";
413413

414-
String signed = JWT.create(Algorithm.ECDSA512((ECKey) PemUtils.readPrivateKeyFromFile(PRIVATE_KEY_FILE_EC_512, "EC"))).sign();
414+
String signed = JWT.create().sign(Algorithm.ECDSA512((ECKey) PemUtils.readPrivateKeyFromFile(PRIVATE_KEY_FILE_EC_512, "EC")));
415415
assertThat(signed, is(notNullValue()));
416416
assertThat(signed, startsWith(headerAndPayload));
417417

0 commit comments

Comments
 (0)