Skip to content

Commit e757b69

Browse files
authored
Merge pull request #1 from auth0/feat/modified-method-signature
Feat: Updated the "validateToken" method signature
2 parents f46750d + 881c21f commit e757b69

19 files changed

Lines changed: 156 additions & 257 deletions

.github/workflows/build-and-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88

99
jobs:
1010
gradle:
11-
runs-on: ubuntu-22.04-2cpu-8ram-75ssd
11+
runs-on: ubuntu-latest
1212
steps:
1313
- uses: actions/checkout@v5
1414
- uses: actions/setup-java@v5

.github/workflows/claude-code-review.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ on:
88

99
jobs:
1010
claude-review:
11-
uses: atko-cic/ai-pr-analyzer-gh-action/.github/workflows/claude-code-review.yml@main
11+
uses: auth0/ai-pr-analyzer-gh-action/.github/workflows/claude-code-review.yml@main

README.md

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -51,45 +51,7 @@ The core library (`auth0-api-java`) is currently an internal module used by the
5151
- JWT validation with Auth0 JWKS integration
5252
- DPoP proof validation per [RFC 9449](https://datatracker.ietf.org/doc/html/rfc9449)
5353
- Flexible authentication strategies
54-
- Comprehensive claim validation
5554

56-
## 🔧 Advanced Configuration
57-
58-
### Custom Claim Validation
59-
60-
While the Spring Boot integration provides automatic validation, developers can access the underlying `auth0-api-java` validation utilities for custom scenarios:
61-
62-
```java
63-
@RestController
64-
public class AdvancedController {
65-
66-
@Autowired
67-
private AuthClient authClient;
68-
69-
@GetMapping("/api/custom-validation")
70-
public ResponseEntity<String> customValidation(HttpServletRequest request) {
71-
try {
72-
String token = extractTokenFromRequest(request);
73-
JWTValidator validator = new JWTValidator(authClient.getAuthOptions());
74-
75-
DecodedJWT jwt = validator.validateTokenWithClaimEquals(token, "role", "admin");
76-
77-
return ResponseEntity.ok("Advanced validation passed");
78-
79-
} catch (BaseAuthException e) {
80-
return ResponseEntity.status(401).body("Validation failed: " + e.getMessage());
81-
}
82-
}
83-
84-
private String extractTokenFromRequest(HttpServletRequest request) {
85-
String authHeader = request.getHeader("Authorization");
86-
if (authHeader != null && authHeader.startsWith("Bearer ")) {
87-
return authHeader.substring(7);
88-
}
89-
throw new IllegalArgumentException("No Bearer token found");
90-
}
91-
}
92-
```
9355

9456
## 📚 Documentation
9557

@@ -135,7 +97,7 @@ The core library (`auth0-api-java`) is bundled as an internal dependency within
13597
4. Add tests for new functionality
13698
5. Ensure all tests pass: `./gradlew test`
13799
6. Ensure your commits are signed
138-
7. 7Submit a pull request
100+
7. Submit a pull request
139101

140102
## 📄 License
141103

auth0-api-java/src/main/java/com/auth0/AbstractAuthentication.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ protected AbstractAuthentication(JWTValidator jwtValidator, TokenExtractor extra
2828
/**
2929
* Concrete method to validate Bearer token headers and JWT claims.
3030
*/
31-
protected DecodedJWT validateBearerToken(Map<String, String> headers) throws BaseAuthException {
31+
protected DecodedJWT validateBearerToken(Map<String, String> headers, HttpRequestInfo httpRequestInfo) throws BaseAuthException {
3232
AuthToken authToken = extractor.extractBearer(headers);
33-
return jwtValidator.validateToken(authToken.getAccessToken());
33+
return jwtValidator.validateToken(authToken.getAccessToken(), headers, httpRequestInfo);
3434
}
3535

3636
/**
@@ -42,7 +42,7 @@ protected DecodedJWT validateDpopTokenAndProof(Map<String, String> headers, Http
4242
AuthValidatorHelper.validateHttpMethodAndHttpUrl(requestInfo);
4343

4444
AuthToken authToken = extractor.extractDPoPProofAndDPoPToken(headers);
45-
DecodedJWT decodedJwtToken = jwtValidator.validateToken(authToken.getAccessToken());
45+
DecodedJWT decodedJwtToken = jwtValidator.validateToken(authToken.getAccessToken(), headers, requestInfo);
4646

4747
dpopProofValidator.validate(authToken.getProof(), decodedJwtToken, requestInfo);
4848

auth0-api-java/src/main/java/com/auth0/AllowedDPoPAuthentication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public AuthenticationContext authenticate(Map<String, String> headers, HttpReque
3737
scheme = extractor.getScheme(normalizedHeader);
3838

3939
if (scheme.equalsIgnoreCase(AuthConstants.BEARER_SCHEME)) {
40-
DecodedJWT jwtToken = validateBearerToken(normalizedHeader);
40+
DecodedJWT jwtToken = validateBearerToken(normalizedHeader, requestInfo);
4141
AuthValidatorHelper.validateNoDpopPresence(normalizedHeader, jwtToken);
4242
return buildContext(jwtToken);
4343
}

auth0-api-java/src/main/java/com/auth0/AuthClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import com.auth0.exception.BaseAuthException;
44
import com.auth0.models.AuthenticationContext;
5-
import com.auth0.validators.DPoPProofValidator;
6-
import com.auth0.validators.JWTValidator;
75
import com.auth0.models.AuthOptions;
86
import com.auth0.models.HttpRequestInfo;
7+
import com.auth0.validators.DPoPProofValidator;
8+
import com.auth0.validators.JWTValidator;
99

1010
import java.util.Map;
1111

auth0-api-java/src/main/java/com/auth0/DisabledDPoPAuthentication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public AuthenticationContext authenticate(Map<String, String> headers, HttpReque
2828

2929
Map<String, String> normalizedHeader = normalize(headers);
3030
try {
31-
DecodedJWT jwt = validateBearerToken(normalizedHeader);
31+
DecodedJWT jwt = validateBearerToken(normalizedHeader, requestInfo);
3232

3333
return buildContext(jwt);
3434
} catch (BaseAuthException ex){

auth0-api-java/src/main/java/com/auth0/examples/Auth0ApiExample.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.auth0.models.AuthOptions;
77
import com.auth0.models.AuthenticationContext;
88
import com.auth0.models.HttpRequestInfo;
9-
import com.auth0.validators.JWTValidator;
109
import com.sun.net.httpserver.HttpExchange;
1110
import com.sun.net.httpserver.HttpHandler;
1211
import com.sun.net.httpserver.HttpServer;

auth0-api-java/src/main/java/com/auth0/validators/JWTValidator.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
import com.auth0.jwk.UrlJwkProvider;
1212
import com.auth0.jwt.algorithms.Algorithm;
1313
import com.auth0.jwt.interfaces.DecodedJWT;
14+
import com.auth0.models.HttpRequestInfo;
15+
1416
import java.security.interfaces.RSAPublicKey;
17+
import java.util.Map;
1518

1619
import static com.auth0.jwt.JWT.require;
1720

@@ -64,7 +67,7 @@ public JWTValidator(AuthOptions authOptions, JwkProvider jwkProvider) {
6467
* @return the decoded and verified JWT
6568
* @throws BaseAuthException if validation fails
6669
*/
67-
public DecodedJWT validateToken(String token) throws BaseAuthException {
70+
public DecodedJWT validateToken(String token, Map<String, String> headers, HttpRequestInfo httpRequestInfo) throws BaseAuthException {
6871

6972
if (token == null || token.trim().isEmpty()) {
7073
throw new MissingRequiredArgumentException("access_token");
@@ -89,9 +92,9 @@ public DecodedJWT validateToken(String token) throws BaseAuthException {
8992
/**
9093
* Validates a JWT and ensures all required scopes are present.
9194
*/
92-
public DecodedJWT validateTokenWithRequiredScopes(String token, String... requiredScopes)
95+
public DecodedJWT validateTokenWithRequiredScopes(String token, Map<String, String> headers, HttpRequestInfo httpRequestInfo, String... requiredScopes)
9396
throws BaseAuthException {
94-
DecodedJWT jwt = validateToken(token);
97+
DecodedJWT jwt = validateToken(token, headers, httpRequestInfo);
9598
try {
9699
ClaimValidator.checkRequiredScopes(jwt, requiredScopes);
97100
return jwt;
@@ -103,9 +106,9 @@ public DecodedJWT validateTokenWithRequiredScopes(String token, String... requir
103106
/**
104107
* Validates a JWT and ensures it has *any* of the provided scopes.
105108
*/
106-
public DecodedJWT validateTokenWithAnyScope(String token, String... scopes)
109+
public DecodedJWT validateTokenWithAnyScope(String token, Map<String, String> headers, HttpRequestInfo httpRequestInfo, String... scopes)
107110
throws BaseAuthException {
108-
DecodedJWT jwt = validateToken(token);
111+
DecodedJWT jwt = validateToken(token, headers, httpRequestInfo);
109112
try {
110113
ClaimValidator.checkAnyScope(jwt, scopes);
111114
return jwt;
@@ -117,9 +120,9 @@ public DecodedJWT validateTokenWithAnyScope(String token, String... scopes)
117120
/**
118121
* Validates a JWT and ensures a claim equals the expected value.
119122
*/
120-
public DecodedJWT validateTokenWithClaimEquals(String token, String claim, Object expected)
123+
public DecodedJWT validateTokenWithClaimEquals(String token, Map<String, String> headers, HttpRequestInfo httpRequestInfo, String claim, Object expected)
121124
throws BaseAuthException {
122-
DecodedJWT jwt = validateToken(token);
125+
DecodedJWT jwt = validateToken(token, headers, httpRequestInfo);
123126
try {
124127
ClaimValidator.checkClaimEquals(jwt, claim, expected);
125128
return jwt;
@@ -131,9 +134,9 @@ public DecodedJWT validateTokenWithClaimEquals(String token, String claim, Objec
131134
/**
132135
* Validates a JWT and ensures a claim includes all expected values.
133136
*/
134-
public DecodedJWT validateTokenWithClaimIncludes(String token, String claim, Object... expectedValues)
137+
public DecodedJWT validateTokenWithClaimIncludes(String token, Map<String, String> headers, HttpRequestInfo httpRequestInfo, String claim, Object... expectedValues)
135138
throws BaseAuthException {
136-
DecodedJWT jwt = validateToken(token);
139+
DecodedJWT jwt = validateToken(token, headers, httpRequestInfo);
137140
try {
138141
ClaimValidator.checkClaimIncludes(jwt, claim, expectedValues);
139142
return jwt;
@@ -142,9 +145,9 @@ public DecodedJWT validateTokenWithClaimIncludes(String token, String claim, Obj
142145
}
143146
}
144147

145-
public DecodedJWT validateTokenWithClaimIncludesAny(String token, String claim, Object... expectedValues)
148+
public DecodedJWT validateTokenWithClaimIncludesAny(String token, Map<String, String> headers, HttpRequestInfo httpRequestInfo, String claim, Object... expectedValues)
146149
throws BaseAuthException {
147-
DecodedJWT jwt = validateToken(token);
150+
DecodedJWT jwt = validateToken(token, headers, httpRequestInfo);
148151
try {
149152
ClaimValidator.checkClaimIncludesAny(jwt, claim, expectedValues);
150153
return jwt;

auth0-api-java/src/test/java/com/auth0/AbstractAuthenticationTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ public void validateBearerToken_shouldExtractAndValidate() throws Exception {
8080
DecodedJWT jwt = mock(DecodedJWT.class);
8181

8282
when(extractor.extractBearer(anyMap())).thenReturn(token);
83-
when(jwtValidator.validateToken("access")).thenReturn(jwt);
83+
when(jwtValidator.validateToken(eq("access"), anyMap(), any())).thenReturn(jwt);
8484

8585
Map<String, String> headers = new HashMap<>();
8686
headers.put("authorization", "Bearer access");
8787

88-
DecodedJWT result = authSystem.validateBearerToken(headers);
88+
DecodedJWT result = authSystem.validateBearerToken(headers, null);
8989

9090
assertThat(result).isSameAs(jwt);
9191
}
@@ -98,7 +98,7 @@ public void validateDpopTokenAndProof_shouldValidateEverything() throws Exceptio
9898
new HttpRequestInfo("GET", "https://api.example.com", null);
9999

100100
when(extractor.extractDPoPProofAndDPoPToken(anyMap())).thenReturn(token);
101-
when(jwtValidator.validateToken("access")).thenReturn(jwt);
101+
when(jwtValidator.validateToken(eq("access"), anyMap(), any())).thenReturn(jwt);
102102

103103
Map<String, String> headers = new HashMap<>();
104104
headers.put("authorization", "DPoP access");

0 commit comments

Comments
 (0)