@@ -138,6 +138,77 @@ public function testSuccessfulPKCEAuthCodeRequest(): void
138138
139139 $ this ->assertInstanceOf (AuthorizationCode::class, $ authCode );
140140 $ this ->assertSame (FixtureFactory::FIXTURE_PUBLIC_CLIENT , $ authCode ->getClient ()->getIdentifier ());
141+ $ this ->assertSame (FixtureFactory::FIXTURE_USER , $ authCode ->getUserIdentifier ());
142+ }
143+
144+ public function testSuccessfulAuthCodeRequestWhenTheLoggedUserIsOverriddenInTheAuthorizationRequestResolveEvent (): void
145+ {
146+ $ state = bin2hex (random_bytes (20 ));
147+ $ codeVerifier = bin2hex (random_bytes (64 ));
148+ $ codeChallengeMethod = 'S256 ' ;
149+
150+ $ codeChallenge = strtr (
151+ rtrim (base64_encode (hash ('sha256 ' , $ codeVerifier , true )), '= ' ),
152+ '+/ ' ,
153+ '-_ '
154+ );
155+
156+ $ this ->loginUser ();
157+
158+ $ this ->client
159+ ->getContainer ()
160+ ->get ('event_dispatcher ' )
161+ ->addListener (OAuth2Events::AUTHORIZATION_REQUEST_RESOLVE , function (AuthorizationRequestResolveEvent $ event ) use ($ state , $ codeChallenge , $ codeChallengeMethod ): void {
162+ $ this ->assertSame ($ state , $ event ->getState ());
163+ $ this ->assertSame ($ codeChallenge , $ event ->getCodeChallenge ());
164+ $ this ->assertSame ($ codeChallengeMethod , $ event ->getCodeChallengeMethod ());
165+
166+ $ event ->setUser (FixtureFactory::createUser ([], FixtureFactory::FIXTURE_USER_TWO ));
167+ $ event ->resolveAuthorization (AuthorizationRequestResolveEvent::AUTHORIZATION_APPROVED );
168+ });
169+
170+ $ this ->client ->request (
171+ 'GET ' ,
172+ '/authorize ' ,
173+ [
174+ 'client_id ' => FixtureFactory::FIXTURE_PUBLIC_CLIENT ,
175+ 'response_type ' => 'code ' ,
176+ 'scope ' => '' ,
177+ 'state ' => $ state ,
178+ 'code_challenge ' => $ codeChallenge ,
179+ 'code_challenge_method ' => $ codeChallengeMethod ,
180+ ]
181+ );
182+
183+ $ response = $ this ->client ->getResponse ();
184+
185+ $ this ->assertSame (302 , $ response ->getStatusCode ());
186+ $ redirectUri = $ response ->headers ->get ('Location ' );
187+
188+ $ this ->assertStringStartsWith (FixtureFactory::FIXTURE_CLIENT_FIRST_REDIRECT_URI , $ redirectUri );
189+ $ query = [];
190+ parse_str (parse_url ($ redirectUri , \PHP_URL_QUERY ), $ query );
191+ $ this ->assertArrayHasKey ('state ' , $ query );
192+ $ this ->assertSame ($ state , $ query ['state ' ]);
193+
194+ $ this ->assertArrayHasKey ('code ' , $ query );
195+ $ payload = json_decode (TestHelper::decryptPayload ($ query ['code ' ]), true );
196+
197+ $ this ->assertArrayHasKey ('code_challenge ' , $ payload );
198+ $ this ->assertArrayHasKey ('code_challenge_method ' , $ payload );
199+ $ this ->assertSame ($ codeChallenge , $ payload ['code_challenge ' ]);
200+ $ this ->assertSame ($ codeChallengeMethod , $ payload ['code_challenge_method ' ]);
201+
202+ /** @var AuthorizationCode|null $authCode */
203+ $ authCode = $ this ->client
204+ ->getContainer ()
205+ ->get ('doctrine.orm.entity_manager ' )
206+ ->getRepository (AuthorizationCode::class)
207+ ->findOneBy (['identifier ' => $ payload ['auth_code_id ' ]]);
208+
209+ $ this ->assertInstanceOf (AuthorizationCode::class, $ authCode );
210+ $ this ->assertSame (FixtureFactory::FIXTURE_PUBLIC_CLIENT , $ authCode ->getClient ()->getIdentifier ());
211+ $ this ->assertSame (FixtureFactory::FIXTURE_USER_TWO , $ authCode ->getUserIdentifier ());
141212 }
142213
143214 public function testAuthCodeRequestWithPublicClientWithoutCodeChallengeWhenTheChallengeIsRequiredForPublicClients (): void
0 commit comments