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 );
@@ -119,7 +119,25 @@ void tokenIssuerMismatch() {
119119 final TokenRequest tokenReq = TokenRequest .newBuilder ()
120120 .code ("someCode" )
121121 .codeVerifier ("myCodeverifier" )
122- .issuer (URI .create ("https://issuer.test" ))
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" )
123141 .redirectUri (URI .create ("https://example.test/redirectUri" ))
124142 .build (
125143 "authorization_code" ,
@@ -138,7 +156,7 @@ void tokenIssuerMatch() {
138156 final TokenRequest tokenReq = TokenRequest .newBuilder ()
139157 .code ("someCode" )
140158 .codeVerifier ("myCodeverifier" )
141- .issuer (URI . create ( "http://example.test" ) )
159+ .issuer (issuer )
142160 .redirectUri (URI .create ("https://example.test/redirectUri" ))
143161 .build (
144162 "authorization_code" ,
@@ -156,6 +174,7 @@ void tokenNoClientSecretTest() {
156174 final TokenRequest tokenReq = TokenRequest .newBuilder ()
157175 .code ("someCode" )
158176 .codeVerifier ("myCodeverifier" )
177+ .issuer (issuer )
159178 .redirectUri (URI .create ("https://example.test/redirectUri" ))
160179 .build (
161180 "authorization_code" ,
@@ -174,6 +193,7 @@ void tokenWithClientSecretBasicTest() {
174193 .code ("someCode" )
175194 .codeVerifier ("myCodeverifier" )
176195 .clientSecret ("myClientSecret" )
196+ .issuer (issuer )
177197 .authMethod ("client_secret_basic" )
178198 .redirectUri (URI .create ("https://example.test/redirectUri" ))
179199 .build (
@@ -193,6 +213,7 @@ void tokenWithClientSecretePostTest() {
193213 .code ("someCode" )
194214 .codeVerifier ("myCodeverifier" )
195215 .clientSecret ("myClientSecret" )
216+ .issuer (issuer )
196217 .authMethod ("client_secret_post" )
197218 .redirectUri (URI .create ("https://example.test/redirectUri" ))
198219 .build (
@@ -211,6 +232,7 @@ void tokenAsyncTest() {
211232 final TokenRequest tokenReq = TokenRequest .newBuilder ()
212233 .code ("someCode" )
213234 .codeVerifier ("myCodeverifier" )
235+ .issuer (issuer )
214236 .redirectUri (URI .create ("https://example.test/redirectUri" ))
215237 .build ("authorization_code" , "myClientId" );
216238 final TokenResponse token = openIdProvider .token (tokenReq ).toCompletableFuture ().join ();
@@ -224,6 +246,7 @@ void tokenAsyncStatusCodesTest() {
224246 final TokenRequest tokenReq = TokenRequest .newBuilder ()
225247 .code ("none" )
226248 .codeVerifier ("none" )
249+ .issuer (issuer )
227250 .redirectUri (URI .create ("none" ))
228251 .build ("authorization_code" , "none" );
229252
@@ -254,7 +277,7 @@ void endSessionAsyncTest() {
254277 .build ();
255278 final URI uri = openIdProvider .endSession (endReq ).toCompletableFuture ().join ();
256279 assertEquals (
257- "http://example.test /endSession?" +
280+ issuer + " /endSession?" +
258281 "client_id=myClientId&post_logout_redirect_uri=https://example.test/redirectUri&id_token_hint=&state=solid" ,
259282 uri .toString ()
260283 );
0 commit comments