@@ -45,11 +45,44 @@ abstract protected function getBaseUrl(): string;
4545 */
4646 public function redirect (): ResponseInterface
4747 {
48+ $ state = null ;
49+ $ nonce = null ;
50+
51+ if ($ this ->usesState ()) {
52+ $ this ->request ->session ()->put ('state ' , $ state = $ this ->getState ());
53+ }
54+
55+ if ($ this ->usesPKCE ()) {
56+ $ this ->request ->session ()->put ('code_verifier ' , $ this ->getCodeVerifier ());
57+ }
58+
4859 if ($ this ->usesNonce ()) {
49- $ this ->request ->session ()->put ('nonce ' , $ this ->getNonce ());
60+ $ this ->request ->session ()->put ('nonce ' , $ nonce = $ this ->getNonce ());
5061 }
5162
52- return parent ::redirect ();
63+ return $ this ->response ->redirect (
64+ $ this ->getAuthUrl ($ state , $ nonce )
65+ );
66+ }
67+
68+ /**
69+ * Get the authentication URL for the provider.
70+ */
71+ protected function getAuthUrl (?string $ state , ?string $ nonce = null ): string
72+ {
73+ return $ this ->buildAuthUrlFromBase (
74+ $ this ->getOpenIdConfig ()['authorization_endpoint ' ],
75+ $ state ,
76+ $ nonce
77+ );
78+ }
79+
80+ /**
81+ * Build the authentication URL for the provider from the given base URL.
82+ */
83+ protected function buildAuthUrlFromBase (string $ url , ?string $ state , ?string $ nonce = null ): string
84+ {
85+ return $ url . '? ' . http_build_query ($ this ->getCodeFields ($ state , $ nonce ), '' , '& ' , $ this ->encodingType );
5386 }
5487
5588 /**
@@ -76,26 +109,15 @@ protected function getJwksUri(): string
76109 return $ this ->getOpenIdConfig ()['jwks_uri ' ];
77110 }
78111
79- /**
80- * Get the authentication URL for the provider.
81- */
82- protected function getAuthUrl (string $ state ): string
83- {
84- return $ this ->buildAuthUrlFromBase (
85- $ this ->getOpenIdConfig ()['authorization_endpoint ' ],
86- $ state
87- );
88- }
89-
90112 /**
91113 * Get the GET parameters for the code request.
92114 */
93- protected function getCodeFields (?string $ state = null ): array
115+ protected function getCodeFields (?string $ state = null , ? string $ nonce = null ): array
94116 {
95117 $ fields = parent ::getCodeFields ($ state );
96118
97- if ($ this ->usesState ()) {
98- $ fields ['state ' ] = $ state ;
119+ if ($ this ->usesNonce ()) {
120+ $ fields ['nonce ' ] = $ nonce ;
99121 }
100122
101123 return $ fields ;
0 commit comments