Skip to content

Commit fa93c8f

Browse files
authored
JCL-469: Add support for scope, state and nonce values in the OpenIdProvider (#1376)
1 parent 0043c83 commit fa93c8f

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,16 @@
5454
*/
5555
public class OpenIdProvider {
5656

57+
// OAuth 2 and OpenID request parameters
5758
private static final String CLIENT_ID = "client_id";
59+
private static final String CODE_CHALLENGE = "code_challenge";
60+
private static final String CODE_CHALLENGE_METHOD = "code_challenge_method";
61+
private static final String NONCE = "nonce";
5862
private static final String REDIRECT_URI = "redirect_uri";
63+
private static final String RESPONSE_TYPE = "response_type";
64+
private static final String SCOPE = "scope";
65+
private static final String STATE = "state";
66+
5967
private static final String EQUALS = "=";
6068
private static final String ETC = "&";
6169

@@ -155,11 +163,20 @@ private URI authorize(final URI authorizationEndpoint, final AuthorizationReques
155163
final URIBuilder builder = URIBuilder.newBuilder(authorizationEndpoint)
156164
.queryParam(CLIENT_ID, request.getClientId())
157165
.queryParam(REDIRECT_URI, request.getRedirectUri().toString())
158-
.queryParam("response_type", request.getResponseType());
166+
.queryParam(RESPONSE_TYPE, request.getResponseType())
167+
.queryParam(SCOPE, request.getScope());
168+
169+
if (request.getState() != null) {
170+
builder.queryParam(STATE, request.getState());
171+
}
172+
173+
if (request.getNonce() != null) {
174+
builder.queryParam(NONCE, request.getNonce());
175+
}
159176

160177
if (request.getCodeChallenge() != null && request.getCodeChallengeMethod() != null) {
161-
builder.queryParam("code_challenge", request.getCodeChallenge());
162-
builder.queryParam("code_challenge_method", request.getCodeChallengeMethod());
178+
builder.queryParam(CODE_CHALLENGE, request.getCodeChallenge());
179+
builder.queryParam(CODE_CHALLENGE_METHOD, request.getCodeChallengeMethod());
163180
}
164181

165182
return builder.build();

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ void unknownMetadata() {
8383
@Test
8484
void authorizeAsyncTest() {
8585
final AuthorizationRequest authReq = AuthorizationRequest.newBuilder()
86+
.scope("openid")
87+
.scope("webid")
8688
.codeChallenge("myCodeChallenge")
8789
.codeChallengeMethod("method")
8890
.build(
@@ -91,7 +93,27 @@ void authorizeAsyncTest() {
9193
);
9294
assertEquals(
9395
"http://example.test/auth?client_id=myClientId&redirect_uri=myRedirectUri&" +
94-
"response_type=code&code_challenge=myCodeChallenge&code_challenge_method=method",
96+
"response_type=code&scope=openid%20webid&code_challenge=myCodeChallenge&code_challenge_method=method",
97+
openIdProvider.authorize(authReq).toCompletableFuture().join().toString()
98+
);
99+
}
100+
101+
@Test
102+
void authorizeAsyncStateNonceTest() {
103+
final String state = UUID.randomUUID().toString();
104+
final String nonce = UUID.randomUUID().toString();
105+
final AuthorizationRequest authReq = AuthorizationRequest.newBuilder()
106+
.scope("openid")
107+
.scope("webid")
108+
.state(state)
109+
.nonce(nonce)
110+
.build(
111+
"myClientId",
112+
URI.create("myRedirectUri")
113+
);
114+
assertEquals(
115+
"http://example.test/auth?client_id=myClientId&redirect_uri=myRedirectUri&" +
116+
"response_type=code&scope=openid%20webid&state=" + state + "&nonce=" + nonce,
95117
openIdProvider.authorize(authReq).toCompletableFuture().join().toString()
96118
);
97119
}

0 commit comments

Comments
 (0)