Skip to content

Commit 6c8d40f

Browse files
author
Justin Dahmubed
committed
Added access token
1 parent 68ea66c commit 6c8d40f

File tree

10 files changed

+638
-2
lines changed

10 files changed

+638
-2
lines changed
Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
package com.auth0.jwt.creators;
2+
3+
import com.auth0.jwt.algorithms.Algorithm;
4+
import com.auth0.jwt.exceptions.JWTCreationException;
5+
import com.auth0.jwt.impl.PublicClaims;
6+
import com.auth0.jwt.jwts.JWT;
7+
8+
import java.util.Date;
9+
import java.util.HashMap;
10+
import java.util.HashSet;
11+
import java.util.Set;
12+
13+
/**
14+
* The AccessJwtCreator class holds the sign method to generate a complete Access JWT (with Signature) from a given Header and Payload content.
15+
*/
16+
public class AccessJwtCreator {
17+
18+
protected JWTCreator.Builder jwt;
19+
protected HashMap<String, Boolean> addedClaims;
20+
protected Set<String> publicClaims;
21+
22+
public AccessJwtCreator() {
23+
jwt = JWT.create();
24+
addedClaims = new HashMap<String, Boolean>() {{
25+
put("Issuer", false);
26+
put("Subject", false);
27+
put("Iat", false);
28+
}};
29+
publicClaims = new HashSet<String>() {{
30+
add(PublicClaims.ISSUER);
31+
add(PublicClaims.SUBJECT);
32+
add(PublicClaims.EXPIRES_AT);
33+
add(PublicClaims.NOT_BEFORE);
34+
add(PublicClaims.ISSUED_AT);
35+
add(PublicClaims.JWT_ID);
36+
add(PublicClaims.AUDIENCE);
37+
}};
38+
}
39+
40+
/**
41+
* Add a specific Issuer ("issuer") claim to the Payload.
42+
* Allows for multiple issuers
43+
*
44+
* @param issuer the Issuer value.
45+
* @return this same Builder instance.
46+
*/
47+
public AccessJwtCreator withIssuer(String... issuer) {
48+
jwt.withIssuer(issuer);
49+
addedClaims.put("Issuer", true);
50+
return this;
51+
}
52+
53+
/**
54+
* Add a specific Subject ("subject") claim to the Payload.
55+
* Allows for multiple subjects
56+
*
57+
* @param subject the Subject value.
58+
* @return this same Builder instance.
59+
*/
60+
public AccessJwtCreator withSubject(String... subject) {
61+
jwt.withSubject(subject);
62+
addedClaims.put("Subject", true);
63+
return this;
64+
}
65+
66+
/**
67+
* Add a specific Audience ("audience") claim to the Payload.
68+
* Allows for multiple audience
69+
*
70+
* @param audience the Audience value.
71+
* @return this same Builder instance.
72+
*/
73+
public AccessJwtCreator withAudience(String... audience) {
74+
jwt.withAudience(audience);
75+
return this;
76+
}
77+
78+
/**
79+
* Add a specific Issued At ("iat") claim to the Payload.
80+
*
81+
* @param iat the Issued At value.
82+
* @return this same Builder instance.
83+
*/
84+
public AccessJwtCreator withIat(Date iat) {
85+
jwt.withIssuedAt(iat);
86+
addedClaims.put("Iat", true);
87+
return this;
88+
}
89+
90+
/**
91+
* Add a specific Expires At ("exp") claim to the Payload.
92+
*
93+
* @param exp the Expires At value.
94+
* @return this same Builder instance.
95+
*/
96+
public AccessJwtCreator withExp(Date exp) {
97+
jwt.withExpiresAt(exp);
98+
return this;
99+
}
100+
101+
/**
102+
* Require a specific Claim value.
103+
*
104+
* @param name the Claim's name.
105+
* @param value the Claim's value.
106+
* @return this same Verification instance.
107+
* @throws IllegalArgumentException if the name is null.
108+
*/
109+
public AccessJwtCreator withNonStandardClaim(String name, String value) {
110+
jwt.withNonStandardClaim(name, value);
111+
return this;
112+
}
113+
114+
/**
115+
* Add a custom Claim value.
116+
*
117+
* @param name the Claim's name.
118+
* @param value the Claim's value.
119+
* @return this same Builder instance.
120+
* @throws IllegalArgumentException if the name is null.
121+
*/
122+
public AccessJwtCreator withNonStandardClaim(String name, Boolean value) throws IllegalArgumentException {
123+
jwt.withNonStandardClaim(name, value);
124+
return this;
125+
}
126+
127+
/**
128+
* Add a custom Claim value.
129+
*
130+
* @param name the Claim's name.
131+
* @param value the Claim's value.
132+
* @return this same Builder instance.
133+
* @throws IllegalArgumentException if the name is null.
134+
*/
135+
public AccessJwtCreator withNonStandardClaim(String name, Integer value) throws IllegalArgumentException {
136+
jwt.withNonStandardClaim(name, value);
137+
return this;
138+
}
139+
140+
/**
141+
* Add a custom Claim value.
142+
*
143+
* @param name the Claim's name.
144+
* @param value the Claim's value.
145+
* @return this same Builder instance.
146+
* @throws IllegalArgumentException if the name is null.
147+
*/
148+
public AccessJwtCreator withNonStandardClaim(String name, Long value) throws IllegalArgumentException {
149+
jwt.withNonStandardClaim(name, value);
150+
return this;
151+
}
152+
153+
/**
154+
* Add a custom Claim value.
155+
*
156+
* @param name the Claim's name.
157+
* @param value the Claim's value.
158+
* @return this same Builder instance.
159+
* @throws IllegalArgumentException if the name is null.
160+
*/
161+
public AccessJwtCreator withNonStandardClaim(String name, Double value) throws IllegalArgumentException {
162+
jwt.withNonStandardClaim(name, value);
163+
return this;
164+
}
165+
166+
/**
167+
* Add a custom Claim value.
168+
*
169+
* @param name the Claim's name.
170+
* @param value the Claim's value.
171+
* @return this same Builder instance.
172+
* @throws IllegalArgumentException if the name is null.
173+
*/
174+
public AccessJwtCreator withNonStandardClaim(String name, Date value) throws IllegalArgumentException {
175+
jwt.withNonStandardClaim(name, value);
176+
return this;
177+
}
178+
179+
/**
180+
* Require a specific Array Claim to contain at least the given items.
181+
*
182+
* @param name the Claim's name.
183+
* @param items the items the Claim must contain.
184+
* @return this same Verification instance.
185+
* @throws IllegalArgumentException if the name is null.
186+
*/
187+
public AccessJwtCreator withArrayClaim(String name, String... items) throws IllegalArgumentException {
188+
jwt.withArrayClaim(name, items);
189+
if(publicClaims.contains(name))
190+
addedClaims.put(name, true);
191+
return this;
192+
}
193+
194+
/**
195+
* Developer explicitly specifies whether they want to accept
196+
* NONE algorithms or not.
197+
*
198+
* @param isNoneAlgorithmAllowed
199+
* @return
200+
*/
201+
public AccessJwtCreator setIsNoneAlgorithmAllowed(boolean isNoneAlgorithmAllowed) {
202+
jwt.setIsNoneAlgorithmAllowed(isNoneAlgorithmAllowed);
203+
return this;
204+
}
205+
206+
/**
207+
* Creates a new JWT and signs it with the given algorithm.
208+
*
209+
* @param algorithm used to sign the JWT
210+
* @return a new JWT token
211+
* @throws IllegalAccessException if the developer didn't want NONE algorithm to be allowed and it was passed in
212+
* @throws IllegalArgumentException if the provided algorithm is null.
213+
* @throws JWTCreationException if the claims could not be converted to a valid JSON or there was a problem with the signing key.
214+
*/
215+
public String sign(Algorithm algorithm) throws Exception {
216+
if(!jwt.getIsNoneAlgorithmAllowed() && algorithm.equals(Algorithm.none())) {
217+
throw new IllegalAccessException("None algorithm isn't allowed");
218+
}
219+
String JWS = jwt.sign(algorithm);
220+
verifyClaims();
221+
return JWS;
222+
}
223+
224+
/**
225+
* Verifies that all the standard claims were provided
226+
* @throws Exception if all the standard claims weren't provided
227+
*/
228+
private void verifyClaims() throws Exception {
229+
for(String claim : addedClaims.keySet())
230+
if(!addedClaims.get(claim))
231+
throw new Exception("Standard claim: " + claim + " has not been set");
232+
}
233+
234+
public static AccessJwtCreator build() {
235+
return new AccessJwtCreator();
236+
}
237+
}

lib/src/main/java/com/auth0/jwt/interfaces/GoogleVerification.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Verification createVerifierForGoogle(String picture, String email, List<String>
1414

1515
GoogleVerification withName(String name);
1616

17-
Verification createVerifierForExtended(String picture, String email, List<String> issuer,
17+
Verification createVerifierForExtended(String picture, String email, List<String> issuer,
1818
List<String> audience, String name, long nbf, long expLeeway, long iatLeeway);
1919

2020
}

lib/src/main/java/com/auth0/jwt/interfaces/Verification.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,8 @@ Verification createVerifierForImplicit(List<String> issuer,
5252

5353
Verification withAppId(String appId);
5454

55+
Verification createVerifierForAccess(List<String> issuer,
56+
List<String> audience, long expLeeway, long iatLeeway);
57+
5558
JWT build();
5659
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.auth0.jwt.jwts;
2+
3+
import com.auth0.jwt.ClockImpl;
4+
import com.auth0.jwt.algorithms.Algorithm;
5+
import com.auth0.jwt.interfaces.Clock;
6+
import com.auth0.jwt.interfaces.Verification;
7+
8+
import java.util.List;
9+
10+
public class AccessJWT extends JWT.BaseVerification implements Verification {
11+
12+
AccessJWT(Algorithm algorithm) throws IllegalArgumentException {
13+
super(algorithm);
14+
}
15+
16+
/**
17+
* Create Verification object for verification purposes
18+
* @param issuer
19+
* @param audience
20+
* @param expLeeway
21+
* @param iatLeeway
22+
* @return
23+
*/
24+
public Verification createVerifierForAccess(List<String> issuer,
25+
List<String> audience, long expLeeway, long iatLeeway) {
26+
return withIssuer(issuer.toArray(new String[issuer.size()])).withAudience(audience.toArray(new String[audience.size()]))
27+
.acceptExpiresAt(expLeeway).acceptIssuedAt(iatLeeway);
28+
}
29+
30+
/**
31+
* Returns a {Verification} to be used to validate token signature.
32+
*
33+
* @param algorithm that will be used to verify the token's signature.
34+
* @return Verification
35+
* @throws IllegalArgumentException if the provided algorithm is null.
36+
*/
37+
public static Verification require(Algorithm algorithm) {
38+
return AccessJWT.init(algorithm);
39+
}
40+
41+
/**
42+
* Initialize a Verification instance using the given Algorithm.
43+
*
44+
* @param algorithm the Algorithm to use on the JWT verification.
45+
* @return a AccessJWT instance to configure.
46+
* @throws IllegalArgumentException if the provided algorithm is null.
47+
*/
48+
static Verification init(Algorithm algorithm) throws IllegalArgumentException {
49+
return new AccessJWT(algorithm);
50+
}
51+
52+
/**
53+
* Creates a new and reusable instance of the JWT with the configuration already provided.
54+
*
55+
* @return a new JWT instance.
56+
*/
57+
@Override
58+
public JWT build() {
59+
return this.build(new ClockImpl());
60+
}
61+
62+
/**
63+
* Creates a new and reusable instance of the JWT the configuration already provided.
64+
* ONLY FOR TEST PURPOSES.
65+
*
66+
* @param clock the instance that will handle the current time.
67+
* @return a new JWT instance with a custom Clock.
68+
*/
69+
public JWT build(Clock clock) {
70+
addLeewayToDateClaims();
71+
return new JWT(algorithm, claims, clock);
72+
}
73+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ public Verification withAppId(String appId) {
127127
throw new UnsupportedOperationException("you shouldn't be calling this method");
128128
}
129129

130+
@Override
131+
public Verification createVerifierForAccess(List<String> issuer, List<String> audience, long expLeeway, long iatLeeway) {
132+
throw new UnsupportedOperationException("you shouldn't be calling this method");
133+
}
134+
130135
/**
131136
* Require a specific Issuer ("iss") claim.
132137
* Allows for multiple issuers

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ public void testCreateVerifierForFB() {
5454
JWT.require(Algorithm.none()).createVerifierForFb(null, null);
5555
}
5656

57+
@Test
58+
public void testCreateVerifierForAccess() {
59+
thrown.expect(UnsupportedOperationException.class);
60+
thrown.expectMessage("you shouldn't be calling this method");
61+
JWT.require(Algorithm.none()).createVerifierForAccess(null, null, 5, 5);
62+
}
63+
5764
@Test
5865
public void testWithUserId() {
5966
thrown.expect(UnsupportedOperationException.class);

0 commit comments

Comments
 (0)