1010
1111namespace KnpU \OAuth2ClientBundle \Client ;
1212
13- use League \OAuth2 \Client \Provider \AbstractProvider ;
1413use League \OAuth2 \Client \Token \AccessToken ;
1514use League \OAuth2 \Client \Token \AccessTokenInterface ;
16- use Symfony \Component \HttpFoundation \Exception \SessionNotFoundException ;
1715use Symfony \Component \HttpFoundation \RedirectResponse ;
18- use Symfony \Component \HttpFoundation \RequestStack ;
1916use Symfony \Component \HttpFoundation \Session \SessionInterface ;
2017
2118/**
@@ -27,14 +24,6 @@ class OAuth2PKCEClient extends OAuth2Client
2724{
2825 public const VERIFIER_KEY = 'pkce_code_verifier ' ;
2926
30- private RequestStack $ requestStack ;
31-
32- public function __construct (AbstractProvider $ provider , RequestStack $ requestStack )
33- {
34- parent ::__construct ($ provider , $ requestStack );
35- $ this ->requestStack = $ requestStack ;
36- }
37-
3827 /**
3928 * Enhance the RedirectResponse prepared by OAuth2Client::redirect() with
4029 * PKCE code challenge and code challenge method parameters.
@@ -66,29 +55,23 @@ public function redirect(array $scopes = [], array $options = [])
6655 */
6756 public function getAccessToken (array $ options = [])
6857 {
69- if (!$ this ->getSession ()->has (static ::VERIFIER_KEY )) {
58+ $ session = $ this ->getSession ();
59+
60+ if (!$ session ->has (static ::VERIFIER_KEY )) {
7061 throw new \LogicException ('Unable to fetch token from OAuth2 server because there is no PKCE code verifier stored in the session ' );
7162 }
72- $ pkce = ['code_verifier ' => $ this ->getSession ()->get (static ::VERIFIER_KEY )];
73- $ this ->getSession ()->remove (static ::VERIFIER_KEY );
63+
64+ $ pkce = ['code_verifier ' => $ session ->get (static ::VERIFIER_KEY )];
65+ $ session ->remove (static ::VERIFIER_KEY );
7466
7567 return parent ::getAccessToken ($ options + $ pkce );
7668 }
7769
7870 /**
7971 * @return SessionInterface
80- *
81- * @throws \LogicException When there is no current request
82- * @throws SessionNotFoundException When session is not set properly [thrown by Request::getSession()]
8372 */
84- protected function getSession ()
73+ protected function getSession (bool $ isPKCE = true )
8574 {
86- $ request = $ this ->requestStack ->getCurrentRequest ();
87-
88- if (!$ request ) {
89- throw new \LogicException ('There is no "current request", and it is needed to perform this action ' );
90- }
91-
92- return $ request ->getSession ();
75+ return parent ::getSession ($ isPKCE );
9376 }
9477}
0 commit comments