11/*
2- * Copyright 2020-2022 the original author or authors.
2+ * Copyright 2020-2023 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
@@ -132,10 +132,11 @@ public void setAuthenticationValidatorWhenNullThenThrowIllegalArgumentException(
132132 @ Test
133133 public void authenticateWhenInvalidClientIdThenThrowOAuth2AuthorizationCodeRequestAuthenticationException () {
134134 RegisteredClient registeredClient = TestRegisteredClients .registeredClient ().build ();
135+ String redirectUri = registeredClient .getRedirectUris ().toArray (new String [0 ])[1 ];
135136 OAuth2AuthorizationCodeRequestAuthenticationToken authentication =
136137 new OAuth2AuthorizationCodeRequestAuthenticationToken (
137138 AUTHORIZATION_URI , registeredClient .getClientId (), principal ,
138- registeredClient . getRedirectUris (). iterator (). next () , STATE , registeredClient .getScopes (), null );
139+ redirectUri , STATE , registeredClient .getScopes (), null );
139140 assertThatThrownBy (() -> this .authenticationProvider .authenticate (authentication ))
140141 .isInstanceOf (OAuth2AuthorizationCodeRequestAuthenticationException .class )
141142 .satisfies (ex ->
@@ -301,10 +302,11 @@ public void authenticateWhenClientNotAuthorizedToRequestCodeThenThrowOAuth2Autho
301302 .build ();
302303 when (this .registeredClientRepository .findByClientId (eq (registeredClient .getClientId ())))
303304 .thenReturn (registeredClient );
305+ String redirectUri = registeredClient .getRedirectUris ().toArray (new String [0 ])[1 ];
304306 OAuth2AuthorizationCodeRequestAuthenticationToken authentication =
305307 new OAuth2AuthorizationCodeRequestAuthenticationToken (
306308 AUTHORIZATION_URI , registeredClient .getClientId (), principal ,
307- registeredClient . getRedirectUris (). iterator (). next () , STATE , registeredClient .getScopes (), null );
309+ redirectUri , STATE , registeredClient .getScopes (), null );
308310 assertThatThrownBy (() -> this .authenticationProvider .authenticate (authentication ))
309311 .isInstanceOf (OAuth2AuthorizationCodeRequestAuthenticationException .class )
310312 .satisfies (ex ->
@@ -319,10 +321,11 @@ public void authenticateWhenInvalidScopeThenThrowOAuth2AuthorizationCodeRequestA
319321 .build ();
320322 when (this .registeredClientRepository .findByClientId (eq (registeredClient .getClientId ())))
321323 .thenReturn (registeredClient );
324+ String redirectUri = registeredClient .getRedirectUris ().toArray (new String [0 ])[2 ];
322325 OAuth2AuthorizationCodeRequestAuthenticationToken authentication =
323326 new OAuth2AuthorizationCodeRequestAuthenticationToken (
324327 AUTHORIZATION_URI , registeredClient .getClientId (), principal ,
325- registeredClient . getRedirectUris (). iterator (). next () , STATE ,
328+ redirectUri , STATE ,
326329 Collections .singleton ("invalid-scope" ), null );
327330 assertThatThrownBy (() -> this .authenticationProvider .authenticate (authentication ))
328331 .isInstanceOf (OAuth2AuthorizationCodeRequestAuthenticationException .class )
@@ -339,10 +342,11 @@ public void authenticateWhenPkceRequiredAndMissingCodeChallengeThenThrowOAuth2Au
339342 .build ();
340343 when (this .registeredClientRepository .findByClientId (eq (registeredClient .getClientId ())))
341344 .thenReturn (registeredClient );
345+ String redirectUri = registeredClient .getRedirectUris ().toArray (new String [0 ])[2 ];
342346 OAuth2AuthorizationCodeRequestAuthenticationToken authentication =
343347 new OAuth2AuthorizationCodeRequestAuthenticationToken (
344348 AUTHORIZATION_URI , registeredClient .getClientId (), principal ,
345- registeredClient . getRedirectUris (). iterator (). next () , STATE , registeredClient .getScopes (), null );
349+ redirectUri , STATE , registeredClient .getScopes (), null );
346350 assertThatThrownBy (() -> this .authenticationProvider .authenticate (authentication ))
347351 .isInstanceOf (OAuth2AuthorizationCodeRequestAuthenticationException .class )
348352 .satisfies (ex ->
@@ -356,13 +360,14 @@ public void authenticateWhenPkceUnsupportedCodeChallengeMethodThenThrowOAuth2Aut
356360 RegisteredClient registeredClient = TestRegisteredClients .registeredClient ().build ();
357361 when (this .registeredClientRepository .findByClientId (eq (registeredClient .getClientId ())))
358362 .thenReturn (registeredClient );
363+ String redirectUri = registeredClient .getRedirectUris ().toArray (new String [0 ])[0 ];
359364 Map <String , Object > additionalParameters = new HashMap <>();
360365 additionalParameters .put (PkceParameterNames .CODE_CHALLENGE , "code-challenge" );
361366 additionalParameters .put (PkceParameterNames .CODE_CHALLENGE_METHOD , "unsupported" );
362367 OAuth2AuthorizationCodeRequestAuthenticationToken authentication =
363368 new OAuth2AuthorizationCodeRequestAuthenticationToken (
364369 AUTHORIZATION_URI , registeredClient .getClientId (), principal ,
365- registeredClient . getRedirectUris (). iterator (). next () , STATE , registeredClient .getScopes (), additionalParameters );
370+ redirectUri , STATE , registeredClient .getScopes (), additionalParameters );
366371 assertThatThrownBy (() -> this .authenticationProvider .authenticate (authentication ))
367372 .isInstanceOf (OAuth2AuthorizationCodeRequestAuthenticationException .class )
368373 .satisfies (ex ->
@@ -377,12 +382,13 @@ public void authenticateWhenPkceMissingCodeChallengeMethodThenThrowOAuth2Authori
377382 RegisteredClient registeredClient = TestRegisteredClients .registeredClient ().build ();
378383 when (this .registeredClientRepository .findByClientId (eq (registeredClient .getClientId ())))
379384 .thenReturn (registeredClient );
385+ String redirectUri = registeredClient .getRedirectUris ().toArray (new String [0 ])[2 ];
380386 Map <String , Object > additionalParameters = new HashMap <>();
381387 additionalParameters .put (PkceParameterNames .CODE_CHALLENGE , "code-challenge" );
382388 OAuth2AuthorizationCodeRequestAuthenticationToken authentication =
383389 new OAuth2AuthorizationCodeRequestAuthenticationToken (
384390 AUTHORIZATION_URI , registeredClient .getClientId (), principal ,
385- registeredClient . getRedirectUris (). iterator (). next () , STATE , registeredClient .getScopes (), additionalParameters );
391+ redirectUri , STATE , registeredClient .getScopes (), additionalParameters );
386392 assertThatThrownBy (() -> this .authenticationProvider .authenticate (authentication ))
387393 .isInstanceOf (OAuth2AuthorizationCodeRequestAuthenticationException .class )
388394 .satisfies (ex ->
@@ -398,10 +404,11 @@ public void authenticateWhenPrincipalNotAuthenticatedThenReturnAuthorizationCode
398404 .thenReturn (registeredClient );
399405 this .principal .setAuthenticated (false );
400406
407+ String redirectUri = registeredClient .getRedirectUris ().toArray (new String [0 ])[1 ];
401408 OAuth2AuthorizationCodeRequestAuthenticationToken authentication =
402409 new OAuth2AuthorizationCodeRequestAuthenticationToken (
403410 AUTHORIZATION_URI , registeredClient .getClientId (), principal ,
404- registeredClient . getRedirectUris (). iterator (). next () , STATE , registeredClient .getScopes (), null );
411+ redirectUri , STATE , registeredClient .getScopes (), null );
405412
406413 OAuth2AuthorizationCodeRequestAuthenticationToken authenticationResult =
407414 (OAuth2AuthorizationCodeRequestAuthenticationToken ) this .authenticationProvider .authenticate (authentication );
@@ -418,10 +425,11 @@ public void authenticateWhenRequireAuthorizationConsentThenReturnAuthorizationCo
418425 when (this .registeredClientRepository .findByClientId (eq (registeredClient .getClientId ())))
419426 .thenReturn (registeredClient );
420427
428+ String redirectUri = registeredClient .getRedirectUris ().toArray (new String [0 ])[0 ];
421429 OAuth2AuthorizationCodeRequestAuthenticationToken authentication =
422430 new OAuth2AuthorizationCodeRequestAuthenticationToken (
423431 AUTHORIZATION_URI , registeredClient .getClientId (), principal ,
424- registeredClient . getRedirectUris (). iterator (). next () , STATE , registeredClient .getScopes (), null );
432+ redirectUri , STATE , registeredClient .getScopes (), null );
425433
426434 OAuth2AuthorizationConsentAuthenticationToken authenticationResult =
427435 (OAuth2AuthorizationConsentAuthenticationToken ) this .authenticationProvider .authenticate (authentication );
@@ -468,10 +476,11 @@ public void authenticateWhenRequireAuthorizationConsentAndOnlyOpenidScopeRequest
468476 when (this .registeredClientRepository .findByClientId (eq (registeredClient .getClientId ())))
469477 .thenReturn (registeredClient );
470478
479+ String redirectUri = registeredClient .getRedirectUris ().toArray (new String [0 ])[1 ];
471480 OAuth2AuthorizationCodeRequestAuthenticationToken authentication =
472481 new OAuth2AuthorizationCodeRequestAuthenticationToken (
473482 AUTHORIZATION_URI , registeredClient .getClientId (), principal ,
474- registeredClient . getRedirectUris (). iterator (). next () , STATE , registeredClient .getScopes (), null );
483+ redirectUri , STATE , registeredClient .getScopes (), null );
475484
476485 OAuth2AuthorizationCodeRequestAuthenticationToken authenticationResult =
477486 (OAuth2AuthorizationCodeRequestAuthenticationToken ) this .authenticationProvider .authenticate (authentication );
@@ -494,10 +503,11 @@ public void authenticateWhenRequireAuthorizationConsentAndAllPreviouslyApprovedT
494503 when (this .authorizationConsentService .findById (eq (registeredClient .getId ()), eq (this .principal .getName ())))
495504 .thenReturn (previousAuthorizationConsent );
496505
506+ String redirectUri = registeredClient .getRedirectUris ().toArray (new String [0 ])[2 ];
497507 OAuth2AuthorizationCodeRequestAuthenticationToken authentication =
498508 new OAuth2AuthorizationCodeRequestAuthenticationToken (
499509 AUTHORIZATION_URI , registeredClient .getClientId (), principal ,
500- registeredClient . getRedirectUris (). iterator (). next () , STATE , registeredClient .getScopes (), null );
510+ redirectUri , STATE , registeredClient .getScopes (), null );
501511
502512 OAuth2AuthorizationCodeRequestAuthenticationToken authenticationResult =
503513 (OAuth2AuthorizationCodeRequestAuthenticationToken ) this .authenticationProvider .authenticate (authentication );
@@ -511,13 +521,14 @@ public void authenticateWhenAuthorizationCodeRequestValidThenReturnAuthorization
511521 when (this .registeredClientRepository .findByClientId (eq (registeredClient .getClientId ())))
512522 .thenReturn (registeredClient );
513523
524+ String redirectUri = registeredClient .getRedirectUris ().toArray (new String [0 ])[0 ];
514525 Map <String , Object > additionalParameters = new HashMap <>();
515526 additionalParameters .put (PkceParameterNames .CODE_CHALLENGE , "code-challenge" );
516527 additionalParameters .put (PkceParameterNames .CODE_CHALLENGE_METHOD , "S256" );
517528 OAuth2AuthorizationCodeRequestAuthenticationToken authentication =
518529 new OAuth2AuthorizationCodeRequestAuthenticationToken (
519530 AUTHORIZATION_URI , registeredClient .getClientId (), principal ,
520- registeredClient . getRedirectUris (). iterator (). next () , STATE , registeredClient .getScopes (), additionalParameters );
531+ redirectUri , STATE , registeredClient .getScopes (), additionalParameters );
521532
522533 OAuth2AuthorizationCodeRequestAuthenticationToken authenticationResult =
523534 (OAuth2AuthorizationCodeRequestAuthenticationToken ) this .authenticationProvider .authenticate (authentication );
@@ -535,10 +546,11 @@ public void authenticateWhenAuthorizationCodeNotGeneratedThenThrowOAuth2Authoriz
535546 OAuth2TokenGenerator <OAuth2AuthorizationCode > authorizationCodeGenerator = mock (OAuth2TokenGenerator .class );
536547 this .authenticationProvider .setAuthorizationCodeGenerator (authorizationCodeGenerator );
537548
549+ String redirectUri = registeredClient .getRedirectUris ().toArray (new String [0 ])[1 ];
538550 OAuth2AuthorizationCodeRequestAuthenticationToken authentication =
539551 new OAuth2AuthorizationCodeRequestAuthenticationToken (
540552 AUTHORIZATION_URI , registeredClient .getClientId (), principal ,
541- registeredClient . getRedirectUris (). iterator (). next () , STATE , registeredClient .getScopes (), null );
553+ redirectUri , STATE , registeredClient .getScopes (), null );
542554
543555 assertThatThrownBy (() -> this .authenticationProvider .authenticate (authentication ))
544556 .isInstanceOf (OAuth2AuthorizationCodeRequestAuthenticationException .class )
@@ -559,10 +571,11 @@ public void authenticateWhenCustomAuthenticationValidatorThenUsed() {
559571 Consumer <OAuth2AuthorizationCodeRequestAuthenticationContext > authenticationValidator = mock (Consumer .class );
560572 this .authenticationProvider .setAuthenticationValidator (authenticationValidator );
561573
574+ String redirectUri = registeredClient .getRedirectUris ().toArray (new String [0 ])[2 ];
562575 OAuth2AuthorizationCodeRequestAuthenticationToken authentication =
563576 new OAuth2AuthorizationCodeRequestAuthenticationToken (
564577 AUTHORIZATION_URI , registeredClient .getClientId (), principal ,
565- registeredClient . getRedirectUris (). iterator (). next () , STATE , registeredClient .getScopes (), null );
578+ redirectUri , STATE , registeredClient .getScopes (), null );
566579
567580 OAuth2AuthorizationCodeRequestAuthenticationToken authenticationResult =
568581 (OAuth2AuthorizationCodeRequestAuthenticationToken ) this .authenticationProvider .authenticate (authentication );
0 commit comments