2525
2626import com .inrupt .client .auth .DPoP ;
2727import com .inrupt .client .openid .TokenRequest .Builder ;
28+ import com .inrupt .client .util .URIBuilder ;
2829
2930import java .io .ByteArrayInputStream ;
3031import java .io .IOException ;
3334import java .security .NoSuchAlgorithmException ;
3435import java .util .Arrays ;
3536import java .util .Collections ;
36- import java .util .HashMap ;
37- import java .util .Map ;
3837import java .util .OptionalInt ;
3938import java .util .UUID ;
4039import java .util .concurrent .CompletableFuture ;
@@ -49,14 +48,14 @@ class OpenIdProviderTest {
4948
5049 private static OpenIdProvider openIdProvider ;
5150 private static final OpenIdMockHttpService mockHttpService = new OpenIdMockHttpService ();
52- private static final Map <String , String > config = new HashMap <>();
5351 private static final DPoP dpop = DPoP .of ();
5452
53+ private static URI issuer ;
5554
5655 @ BeforeAll
5756 static void setup () throws NoSuchAlgorithmException {
58- config . put ( "openid_uri" , mockHttpService .start ());
59- openIdProvider = new OpenIdProvider (URI . create ( config . get ( "openid_uri" )) , dpop );
57+ issuer = URI . create ( mockHttpService .start ());
58+ openIdProvider = new OpenIdProvider (issuer , dpop );
6059 }
6160
6261 @ AfterAll
@@ -66,15 +65,16 @@ static void teardown() {
6665
6766 @ Test
6867 void metadataAsyncTest () {
69- assertEquals ("http://example.test" ,
70- openIdProvider .metadata ().toCompletableFuture ().join ().issuer . toString () );
71- assertEquals ("http://example.test/oauth/ jwks" ,
72- openIdProvider .metadata ().toCompletableFuture ().join ().jwksUri . toString () );
68+ assertEquals (issuer ,
69+ openIdProvider .metadata ().toCompletableFuture ().join ().issuer );
70+ assertEquals (URIBuilder . newBuilder ( issuer ). path ( " jwks"). build () ,
71+ openIdProvider .metadata ().toCompletableFuture ().join ().jwksUri );
7372 }
7473
7574 @ Test
7675 void unknownMetadata () {
77- final OpenIdProvider provider = new OpenIdProvider (URI .create (config .get ("openid_uri" ) + "/not-found" ), dpop );
76+ final OpenIdProvider provider = new OpenIdProvider (URIBuilder .newBuilder (issuer ).path ("not-found" ).build (),
77+ dpop );
7878 final CompletionException err = assertThrows (CompletionException .class ,
7979 provider .metadata ().toCompletableFuture ()::join );
8080 assertTrue (err .getCause () instanceof OpenIdException );
@@ -90,7 +90,7 @@ void authorizeAsyncTest() {
9090 URI .create ("myRedirectUri" )
9191 );
9292 assertEquals (
93- "http://example.test /auth?client_id=myClientId&redirect_uri=myRedirectUri&" +
93+ issuer + " /auth?client_id=myClientId&redirect_uri=myRedirectUri&" +
9494 "response_type=code&code_challenge=myCodeChallenge&code_challenge_method=method" ,
9595 openIdProvider .authorize (authReq ).toCompletableFuture ().join ().toString ()
9696 );
@@ -114,11 +114,67 @@ 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://not.an.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 tokenIssuerMissing () {
138+ final TokenRequest tokenReq = TokenRequest .newBuilder ()
139+ .code ("someCode" )
140+ .codeVerifier ("myCodeverifier" )
141+ .redirectUri (URI .create ("https://example.test/redirectUri" ))
142+ .build (
143+ "authorization_code" ,
144+ "myClientId"
145+ );
146+
147+ final CompletionException ex = assertThrows (CompletionException .class , openIdProvider .token (tokenReq )
148+ .toCompletableFuture ()::join );
149+ assertTrue (ex .getCause () instanceof OpenIdException );
150+ final OpenIdException cause = (OpenIdException ) ex .getCause ();
151+ assertTrue (cause .getMessage ().contains ("Issuer mismatch" ));
152+ }
153+
154+ @ Test
155+ void tokenIssuerMatch () {
156+ final TokenRequest tokenReq = TokenRequest .newBuilder ()
157+ .code ("someCode" )
158+ .codeVerifier ("myCodeverifier" )
159+ .issuer (issuer )
160+ .redirectUri (URI .create ("https://example.test/redirectUri" ))
161+ .build (
162+ "authorization_code" ,
163+ "myClientId"
164+ );
165+ final TokenResponse token = openIdProvider .token (tokenReq )
166+ .toCompletableFuture ().join ();
167+ assertEquals ("123456" , token .accessToken );
168+ assertNotNull (token .idToken );
169+ assertEquals ("Bearer" , token .tokenType );
170+ }
171+
117172 @ Test
118173 void tokenNoClientSecretTest () {
119174 final TokenRequest tokenReq = TokenRequest .newBuilder ()
120175 .code ("someCode" )
121176 .codeVerifier ("myCodeverifier" )
177+ .issuer (issuer )
122178 .redirectUri (URI .create ("https://example.test/redirectUri" ))
123179 .build (
124180 "authorization_code" ,
@@ -137,6 +193,7 @@ void tokenWithClientSecretBasicTest() {
137193 .code ("someCode" )
138194 .codeVerifier ("myCodeverifier" )
139195 .clientSecret ("myClientSecret" )
196+ .issuer (issuer )
140197 .authMethod ("client_secret_basic" )
141198 .redirectUri (URI .create ("https://example.test/redirectUri" ))
142199 .build (
@@ -156,6 +213,7 @@ void tokenWithClientSecretePostTest() {
156213 .code ("someCode" )
157214 .codeVerifier ("myCodeverifier" )
158215 .clientSecret ("myClientSecret" )
216+ .issuer (issuer )
159217 .authMethod ("client_secret_post" )
160218 .redirectUri (URI .create ("https://example.test/redirectUri" ))
161219 .build (
@@ -174,6 +232,7 @@ void tokenAsyncTest() {
174232 final TokenRequest tokenReq = TokenRequest .newBuilder ()
175233 .code ("someCode" )
176234 .codeVerifier ("myCodeverifier" )
235+ .issuer (issuer )
177236 .redirectUri (URI .create ("https://example.test/redirectUri" ))
178237 .build ("authorization_code" , "myClientId" );
179238 final TokenResponse token = openIdProvider .token (tokenReq ).toCompletableFuture ().join ();
@@ -187,6 +246,7 @@ void tokenAsyncStatusCodesTest() {
187246 final TokenRequest tokenReq = TokenRequest .newBuilder ()
188247 .code ("none" )
189248 .codeVerifier ("none" )
249+ .issuer (issuer )
190250 .redirectUri (URI .create ("none" ))
191251 .build ("authorization_code" , "none" );
192252
@@ -217,7 +277,7 @@ void endSessionAsyncTest() {
217277 .build ();
218278 final URI uri = openIdProvider .endSession (endReq ).toCompletableFuture ().join ();
219279 assertEquals (
220- "http://example.test /endSession?" +
280+ issuer + " /endSession?" +
221281 "client_id=myClientId&post_logout_redirect_uri=https://example.test/redirectUri&id_token_hint=&state=solid" ,
222282 uri .toString ()
223283 );
0 commit comments