Skip to content

Commit b101a38

Browse files
committed
Feat: Updated Methods Signature
1 parent e757b69 commit b101a38

22 files changed

Lines changed: 222 additions & 198 deletions

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,21 @@ 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, HttpRequestInfo httpRequestInfo) throws BaseAuthException {
32-
AuthToken authToken = extractor.extractBearer(headers);
33-
return jwtValidator.validateToken(authToken.getAccessToken(), headers, httpRequestInfo);
31+
protected DecodedJWT validateBearerToken(HttpRequestInfo httpRequestInfo) throws BaseAuthException {
32+
AuthToken authToken = extractor.extractBearer(httpRequestInfo.getHeaders());
33+
return jwtValidator.validateToken(authToken.getAccessToken(), httpRequestInfo);
3434
}
3535

3636
/**
3737
* Concrete method to validate DPoP token headers, JWT claims, and proof.
3838
*/
39-
protected DecodedJWT validateDpopTokenAndProof(Map<String, String> headers, HttpRequestInfo requestInfo)
39+
protected DecodedJWT validateDpopTokenAndProof(HttpRequestInfo requestInfo)
4040
throws BaseAuthException {
4141

4242
AuthValidatorHelper.validateHttpMethodAndHttpUrl(requestInfo);
4343

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

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

@@ -52,9 +52,7 @@ protected DecodedJWT validateDpopTokenAndProof(Map<String, String> headers, Http
5252
/**
5353
* Main abstract method for each concrete strategy.
5454
*/
55-
public abstract AuthenticationContext authenticate(
56-
Map<String, String> headers,
57-
HttpRequestInfo requestInfo
55+
public abstract AuthenticationContext authenticate(HttpRequestInfo requestInfo
5856
) throws BaseAuthException;
5957

6058
/**

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,27 @@ public AllowedDPoPAuthentication(JWTValidator jwtValidator,
2020

2121
/**
2222
* Authenticates the request when DPoP Mode is Allowed (Accepts both DPoP and Bearer tokens) .
23-
* @param headers request headers
2423
* @param requestInfo HTTP request info
2524
* @return AuthenticationContext with JWT claims
2625
* @throws BaseAuthException if validation fails
2726
*/
2827
@Override
29-
public AuthenticationContext authenticate(Map<String, String> headers, HttpRequestInfo requestInfo)
28+
public AuthenticationContext authenticate(HttpRequestInfo requestInfo)
3029
throws BaseAuthException {
3130

3231
String scheme = "";
3332

3433
try{
35-
Map<String, String> normalizedHeader = normalize(headers);
36-
37-
scheme = extractor.getScheme(normalizedHeader);
34+
scheme = extractor.getScheme(requestInfo.getHeaders());
3835

3936
if (scheme.equalsIgnoreCase(AuthConstants.BEARER_SCHEME)) {
40-
DecodedJWT jwtToken = validateBearerToken(normalizedHeader, requestInfo);
41-
AuthValidatorHelper.validateNoDpopPresence(normalizedHeader, jwtToken);
37+
DecodedJWT jwtToken = validateBearerToken(requestInfo);
38+
AuthValidatorHelper.validateNoDpopPresence(requestInfo.getHeaders(), jwtToken);
4239
return buildContext(jwtToken);
4340
}
4441

4542
if (scheme.equalsIgnoreCase(AuthConstants.DPOP_SCHEME)) {
46-
DecodedJWT decodedJWT = validateDpopTokenAndProof(normalizedHeader, requestInfo);
43+
DecodedJWT decodedJWT = validateDpopTokenAndProof(requestInfo);
4744
return buildContext(decodedJWT);
4845
}
4946

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,11 @@ public static AuthClient from(AuthOptions options) {
4545

4646
/**
4747
* Verifies the incoming request headers and HTTP request info.
48-
* @param headers request headers
4948
* @param requestInfo HTTP request info
5049
* @return AuthenticationContext with JWT claims
5150
* @throws BaseAuthException if verification fails
5251
*/
53-
public AuthenticationContext verifyRequest(Map<String, String> headers, HttpRequestInfo requestInfo) throws BaseAuthException {
54-
return orchestrator.process(headers, requestInfo);
52+
public AuthenticationContext verifyRequest(HttpRequestInfo requestInfo) throws BaseAuthException {
53+
return orchestrator.process(requestInfo);
5554
}
5655
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public AuthenticationOrchestrator(AbstractAuthentication authStrategy) {
1616
this.authStrategy = authStrategy;
1717
}
1818

19-
public AuthenticationContext process(Map<String, String> headers, HttpRequestInfo requestInfo)
19+
public AuthenticationContext process(HttpRequestInfo requestInfo)
2020
throws BaseAuthException {
21-
return authStrategy.authenticate(headers, requestInfo);
21+
return authStrategy.authenticate(requestInfo);
2222
}
2323
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,17 @@ public DisabledDPoPAuthentication(JWTValidator jwtValidator, TokenExtractor extr
1717

1818
/**
1919
* Authenticates the request when DPoP Mode is Disabled (Accepts only Bearer tokens) .
20-
* @param headers request headers
2120
* @param requestInfo HTTP request info
2221
* @return AuthenticationContext with JWT claims
2322
* @throws BaseAuthException if validation fails
2423
*/
2524
@Override
26-
public AuthenticationContext authenticate(Map<String, String> headers, HttpRequestInfo requestInfo)
25+
public AuthenticationContext authenticate(HttpRequestInfo requestInfo)
2726
throws BaseAuthException {
2827

29-
Map<String, String> normalizedHeader = normalize(headers);
28+
// Map<String, String> normalizedHeader = normalize(requestInfo.getHeaders());
3029
try {
31-
DecodedJWT jwt = validateBearerToken(normalizedHeader, requestInfo);
30+
DecodedJWT jwt = validateBearerToken(requestInfo);
3231

3332
return buildContext(jwt);
3433
} catch (BaseAuthException ex){

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,18 @@ public RequiredDPoPAuthentication(JWTValidator jwtValidator,
2020

2121
/**
2222
* Authenticates the request when DPoP Mode is Allowed (Accepts only DPoP tokens) .
23-
* @param headers request headers
2423
* @param requestInfo HTTP request info
2524
* @return AuthenticationContext with JWT claims
2625
* @throws BaseAuthException if validation fails
2726
*/
2827
@Override
29-
public AuthenticationContext authenticate(Map<String, String> headers, HttpRequestInfo requestInfo)
28+
public AuthenticationContext authenticate(HttpRequestInfo requestInfo)
3029
throws BaseAuthException {
3130

32-
Map<String, String> normalizedHeader = normalize(headers);
31+
// Map<String, String> normalizedHeader = normalize(requestInfo.getHeaders());
3332

3433
try {
35-
DecodedJWT decodedJWT = validateDpopTokenAndProof(normalizedHeader, requestInfo);
34+
DecodedJWT decodedJWT = validateDpopTokenAndProof(requestInfo);
3635
return buildContext(decodedJWT);
3736
}
3837
catch (BaseAuthException ex){

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,21 @@ public void handle(HttpExchange exchange) throws IOException {
9797

9898

9999
// Build HttpRequestInfo (needed for DPoP htm + htu validation)
100-
HttpRequestInfo requestInfo = new HttpRequestInfo(
101-
exchange.getRequestMethod(),
102-
"http://localhost:8000" + exchange.getRequestURI().toString(), null
103-
);
100+
HttpRequestInfo requestInfo = null;
101+
try {
102+
requestInfo = new HttpRequestInfo(
103+
exchange.getRequestMethod(),
104+
"http://localhost:8000" + exchange.getRequestURI().toString(), headers
105+
);
106+
} catch (BaseAuthException e) {
107+
throw new RuntimeException(e);
108+
}
104109

105110
System.out.println("Incoming request to " + requestInfo.toString());
106111

107112
try {
108113
AuthenticationContext claims =
109-
authClient.verifyRequest(headers, requestInfo);
114+
authClient.verifyRequest(requestInfo);
110115

111116
String user = (String) claims.getClaims().get("sub");
112117

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
package com.auth0.models;
22

3-
import java.util.Collections;
3+
import com.auth0.exception.InvalidRequestException;
4+
import org.apache.http.util.Asserts;
5+
6+
import java.util.HashMap;
47
import java.util.Map;
58

69
public class HttpRequestInfo {
710
private final String httpMethod;
811
private final String httpUrl;
9-
private final Map<String, String> context;
12+
private final Map<String, String> headers;
13+
14+
public HttpRequestInfo(String httpMethod, String httpUrl, Map<String, String> headers) throws InvalidRequestException {
15+
Asserts.notNull(headers, "Headers map cannot be null");
1016

11-
public HttpRequestInfo(String httpMethod, String httpUrl, Map<String, String> context) {
12-
this.httpMethod = httpMethod.toUpperCase();
17+
this.httpMethod = httpMethod != null ? httpMethod.toUpperCase() : null;
1318
this.httpUrl = httpUrl;
14-
this.context = context != null ? Collections.unmodifiableMap(context) : Collections.emptyMap();
19+
this.headers = normalize(headers);
20+
}
21+
22+
public HttpRequestInfo(Map<String, String> headers) throws InvalidRequestException {
23+
this(null, null, headers);
1524
}
1625

1726
public String getHttpMethod() {
@@ -22,7 +31,20 @@ public String getHttpUrl() {
2231
return httpUrl;
2332
}
2433

25-
public Map<String, String> getContext() {
26-
return context;
34+
public Map<String, String> getHeaders() {
35+
return headers;
36+
}
37+
38+
private static Map<String, String> normalize(Map<String, String> headers) throws InvalidRequestException {
39+
Map<String, String> normalized = new HashMap<>(headers.size());
40+
41+
for (Map.Entry<String, String> entry : headers.entrySet()) {
42+
String key = entry.getKey().toLowerCase();
43+
if (normalized.containsKey(key)) {
44+
throw new InvalidRequestException("Duplicate HTTP header detected");
45+
}
46+
normalized.put(key, entry.getValue());
47+
}
48+
return normalized;
2749
}
2850
}

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public JWTValidator(AuthOptions authOptions, JwkProvider jwkProvider) {
6767
* @return the decoded and verified JWT
6868
* @throws BaseAuthException if validation fails
6969
*/
70-
public DecodedJWT validateToken(String token, Map<String, String> headers, HttpRequestInfo httpRequestInfo) throws BaseAuthException {
70+
public DecodedJWT validateToken(String token, HttpRequestInfo httpRequestInfo) throws BaseAuthException {
7171

7272
if (token == null || token.trim().isEmpty()) {
7373
throw new MissingRequiredArgumentException("access_token");
@@ -92,9 +92,9 @@ public DecodedJWT validateToken(String token, Map<String, String> headers, HttpR
9292
/**
9393
* Validates a JWT and ensures all required scopes are present.
9494
*/
95-
public DecodedJWT validateTokenWithRequiredScopes(String token, Map<String, String> headers, HttpRequestInfo httpRequestInfo, String... requiredScopes)
95+
public DecodedJWT validateTokenWithRequiredScopes(String token, HttpRequestInfo httpRequestInfo, String... requiredScopes)
9696
throws BaseAuthException {
97-
DecodedJWT jwt = validateToken(token, headers, httpRequestInfo);
97+
DecodedJWT jwt = validateToken(token,httpRequestInfo);
9898
try {
9999
ClaimValidator.checkRequiredScopes(jwt, requiredScopes);
100100
return jwt;
@@ -106,9 +106,9 @@ public DecodedJWT validateTokenWithRequiredScopes(String token, Map<String, Stri
106106
/**
107107
* Validates a JWT and ensures it has *any* of the provided scopes.
108108
*/
109-
public DecodedJWT validateTokenWithAnyScope(String token, Map<String, String> headers, HttpRequestInfo httpRequestInfo, String... scopes)
109+
public DecodedJWT validateTokenWithAnyScope(String token, HttpRequestInfo httpRequestInfo, String... scopes)
110110
throws BaseAuthException {
111-
DecodedJWT jwt = validateToken(token, headers, httpRequestInfo);
111+
DecodedJWT jwt = validateToken(token, httpRequestInfo);
112112
try {
113113
ClaimValidator.checkAnyScope(jwt, scopes);
114114
return jwt;
@@ -120,9 +120,9 @@ public DecodedJWT validateTokenWithAnyScope(String token, Map<String, String> he
120120
/**
121121
* Validates a JWT and ensures a claim equals the expected value.
122122
*/
123-
public DecodedJWT validateTokenWithClaimEquals(String token, Map<String, String> headers, HttpRequestInfo httpRequestInfo, String claim, Object expected)
123+
public DecodedJWT validateTokenWithClaimEquals(String token, HttpRequestInfo httpRequestInfo, String claim, Object expected)
124124
throws BaseAuthException {
125-
DecodedJWT jwt = validateToken(token, headers, httpRequestInfo);
125+
DecodedJWT jwt = validateToken(token, httpRequestInfo);
126126
try {
127127
ClaimValidator.checkClaimEquals(jwt, claim, expected);
128128
return jwt;
@@ -134,9 +134,9 @@ public DecodedJWT validateTokenWithClaimEquals(String token, Map<String, String>
134134
/**
135135
* Validates a JWT and ensures a claim includes all expected values.
136136
*/
137-
public DecodedJWT validateTokenWithClaimIncludes(String token, Map<String, String> headers, HttpRequestInfo httpRequestInfo, String claim, Object... expectedValues)
137+
public DecodedJWT validateTokenWithClaimIncludes(String token, HttpRequestInfo httpRequestInfo, String claim, Object... expectedValues)
138138
throws BaseAuthException {
139-
DecodedJWT jwt = validateToken(token, headers, httpRequestInfo);
139+
DecodedJWT jwt = validateToken(token, httpRequestInfo);
140140
try {
141141
ClaimValidator.checkClaimIncludes(jwt, claim, expectedValues);
142142
return jwt;
@@ -145,9 +145,9 @@ public DecodedJWT validateTokenWithClaimIncludes(String token, Map<String, Strin
145145
}
146146
}
147147

148-
public DecodedJWT validateTokenWithClaimIncludesAny(String token, Map<String, String> headers, HttpRequestInfo httpRequestInfo, String claim, Object... expectedValues)
148+
public DecodedJWT validateTokenWithClaimIncludesAny(String token, HttpRequestInfo httpRequestInfo, String claim, Object... expectedValues)
149149
throws BaseAuthException {
150-
DecodedJWT jwt = validateToken(token, headers, httpRequestInfo);
150+
DecodedJWT jwt = validateToken(token, httpRequestInfo);
151151
try {
152152
ClaimValidator.checkClaimIncludesAny(jwt, claim, expectedValues);
153153
return jwt;

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import java.util.Map;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20-
import static org.mockito.ArgumentMatchers.anyMap;
20+
import static org.mockito.ArgumentMatchers.*;
2121
import static org.mockito.Mockito.*;
2222

2323
public class AbstractAuthenticationTest {
@@ -38,7 +38,6 @@ private static class TestAuthImpl extends AbstractAuthentication {
3838

3939
@Override
4040
public AuthenticationContext authenticate(
41-
Map<String, String> headers,
4241
HttpRequestInfo requestInfo) {
4342
return null;
4443
}
@@ -80,12 +79,12 @@ public void validateBearerToken_shouldExtractAndValidate() throws Exception {
8079
DecodedJWT jwt = mock(DecodedJWT.class);
8180

8281
when(extractor.extractBearer(anyMap())).thenReturn(token);
83-
when(jwtValidator.validateToken(eq("access"), anyMap(), any())).thenReturn(jwt);
82+
when(jwtValidator.validateToken(eq("access"), any(HttpRequestInfo.class))).thenReturn(jwt);
8483

8584
Map<String, String> headers = new HashMap<>();
8685
headers.put("authorization", "Bearer access");
8786

88-
DecodedJWT result = authSystem.validateBearerToken(headers, null);
87+
DecodedJWT result = authSystem.validateBearerToken(new HttpRequestInfo("GET", "https://api.example.com", headers));
8988

9089
assertThat(result).isSameAs(jwt);
9190
}
@@ -94,17 +93,17 @@ public void validateBearerToken_shouldExtractAndValidate() throws Exception {
9493
public void validateDpopTokenAndProof_shouldValidateEverything() throws Exception {
9594
AuthToken token = new AuthToken("access", "proof", null);
9695
DecodedJWT jwt = mock(DecodedJWT.class);
97-
HttpRequestInfo request =
98-
new HttpRequestInfo("GET", "https://api.example.com", null);
99-
100-
when(extractor.extractDPoPProofAndDPoPToken(anyMap())).thenReturn(token);
101-
when(jwtValidator.validateToken(eq("access"), anyMap(), any())).thenReturn(jwt);
10296

10397
Map<String, String> headers = new HashMap<>();
10498
headers.put("authorization", "DPoP access");
10599
headers.put("dpop", "proof");
106100

107-
DecodedJWT result = authSystem.validateDpopTokenAndProof(headers, request);
101+
HttpRequestInfo request = new HttpRequestInfo("GET", "https://api.example.com", headers);
102+
103+
when(extractor.extractDPoPProofAndDPoPToken(anyMap())).thenReturn(token);
104+
when(jwtValidator.validateToken(eq("access"), any(HttpRequestInfo.class))).thenReturn(jwt);
105+
106+
DecodedJWT result = authSystem.validateDpopTokenAndProof(request);
108107

109108
verify(dpopProofValidator).validate("proof", jwt, request);
110109
assertThat(result).isSameAs(jwt);

0 commit comments

Comments
 (0)