Skip to content

Commit 7273582

Browse files
committed
JCL-444: Improve support for RFC 9207
1 parent 6f47253 commit 7273582

File tree

5 files changed

+77
-3
lines changed

5 files changed

+77
-3
lines changed

openid/src/main/java/com/inrupt/client/openid/Metadata.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,9 @@ public class Metadata {
112112
* A list of DPoP signing algorithm values supported by the given OpenID Connect provider.
113113
*/
114114
public List<String> dpopSigningAlgValuesSupported;
115+
116+
/**
117+
* Indication of whether the OpenID Connect provider supports RFC-9207.
118+
*/
119+
public boolean authorizationResponseIssParameterSupported;
115120
}

openid/src/main/java/com/inrupt/client/openid/OpenIdProvider.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,13 @@ ErrorResponse tryParseError(final InputStream input) {
211211
}
212212

213213
private Request tokenRequest(final Metadata metadata, final TokenRequest request) {
214+
if (request.getIssuer() != null) {
215+
if (!request.getIssuer().equals(metadata.issuer)) {
216+
throw new OpenIdException("Issuer mismatch. " +
217+
"Please verify that the designated OpenID issuer is correct");
218+
}
219+
}
220+
214221
if (!metadata.grantTypesSupported.contains(request.getGrantType())) {
215222
throw new OpenIdException("Grant type [" + request.getGrantType() + "] is not supported by this provider.");
216223
}

openid/src/main/java/com/inrupt/client/openid/TokenRequest.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public final class TokenRequest {
3838
private final String clientSecret;
3939
private final String authMethod;
4040
private final URI redirectUri;
41+
private final URI issuer;
4142
private final List<String> scopes;
4243

4344
/**
@@ -58,6 +59,15 @@ public List<String> getScopes() {
5859
return scopes;
5960
}
6061

62+
/**
63+
* Get the issuer.
64+
*
65+
* @return the issuer, may be {@code null}
66+
*/
67+
public URI getIssuer() {
68+
return issuer;
69+
}
70+
6171
/**
6272
* Get the authentication method.
6373
*
@@ -123,7 +133,8 @@ public static Builder newBuilder() {
123133

124134
/* package-private */
125135
TokenRequest(final String clientId, final String clientSecret, final URI redirectUri, final String grantType,
126-
final String authMethod, final String code, final String codeVerifier, final List<String> scopes) {
136+
final String authMethod, final String code, final String codeVerifier, final URI issuer,
137+
final List<String> scopes) {
127138
this.clientId = clientId;
128139
this.clientSecret = clientSecret;
129140
this.redirectUri = redirectUri;
@@ -132,6 +143,7 @@ public static Builder newBuilder() {
132143
this.code = code;
133144
this.codeVerifier = codeVerifier;
134145
this.scopes = scopes;
146+
this.issuer = issuer;
135147
}
136148

137149
/**
@@ -146,6 +158,7 @@ public static class Builder {
146158
private String builderCode;
147159
private String builderCodeVerifier;
148160
private URI builderRedirectUri;
161+
private URI builderIssuer;
149162
private List<String> builderScopes = new ArrayList<>();
150163

151164
/**
@@ -181,6 +194,17 @@ public Builder scopes(final String... scopes) {
181194
return this;
182195
}
183196

197+
/**
198+
* Set the issuer URI.
199+
*
200+
* @param issuer the issuer value
201+
* @return this builder
202+
*/
203+
public Builder issuer(final URI issuer) {
204+
builderIssuer = issuer;
205+
return this;
206+
}
207+
184208
/**
185209
* Set the authentication method for the token endpoint.
186210
*
@@ -240,7 +264,7 @@ public TokenRequest build(final String grantType, final String clientId) {
240264
}
241265

242266
return new TokenRequest(clientId, builderClientSecret, builderRedirectUri, grant, builderAuthMethod,
243-
builderCode, builderCodeVerifier, builderScopes);
267+
builderCode, builderCodeVerifier, builderIssuer, builderScopes);
244268
}
245269
}
246270
}

openid/src/test/java/com/inrupt/client/openid/OpenIdProviderTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,43 @@ void tokenRequestIllegalArgumentsTest() {
114114
() -> builder.build("myGrantType", null));
115115
}
116116

117+
@Test
118+
void tokenIssuerMismatch() {
119+
final TokenRequest tokenReq = TokenRequest.newBuilder()
120+
.code("someCode")
121+
.codeVerifier("myCodeverifier")
122+
.issuer(URI.create("https://issuer.test"))
123+
.redirectUri(URI.create("https://example.test/redirectUri"))
124+
.build(
125+
"authorization_code",
126+
"myClientId"
127+
);
128+
129+
final CompletionException ex = assertThrows(CompletionException.class, openIdProvider.token(tokenReq)
130+
.toCompletableFuture()::join);
131+
assertTrue(ex.getCause() instanceof OpenIdException);
132+
final OpenIdException cause = (OpenIdException) ex.getCause();
133+
assertTrue(cause.getMessage().contains("Issuer mismatch"));
134+
}
135+
136+
@Test
137+
void tokenIssuerMatch() {
138+
final TokenRequest tokenReq = TokenRequest.newBuilder()
139+
.code("someCode")
140+
.codeVerifier("myCodeverifier")
141+
.issuer(URI.create("http://example.test"))
142+
.redirectUri(URI.create("https://example.test/redirectUri"))
143+
.build(
144+
"authorization_code",
145+
"myClientId"
146+
);
147+
final TokenResponse token = openIdProvider.token(tokenReq)
148+
.toCompletableFuture().join();
149+
assertEquals("123456", token.accessToken);
150+
assertNotNull(token.idToken);
151+
assertEquals("Bearer", token.tokenType);
152+
}
153+
117154
@Test
118155
void tokenNoClientSecretTest() {
119156
final TokenRequest tokenReq = TokenRequest.newBuilder()

openid/src/test/resources/metadata.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,6 @@
6969
],
7070
"dpop_signing_alg_values_supported":[
7171
"RS256", "ES256"
72-
]
72+
],
73+
"authorization_response_iss_parameter_supported": true
7374
}

0 commit comments

Comments
 (0)