diff --git a/src/OpenIDConnectClient.php b/src/OpenIDConnectClient.php index 6aa80b17..b01da1a1 100644 --- a/src/OpenIDConnectClient.php +++ b/src/OpenIDConnectClient.php @@ -307,7 +307,15 @@ public function authenticate(): bool // If we have an authorization code then proceed to request a token if (isset($_REQUEST['code'])) { + // Do an OpenID Connect session check + if (!isset($_REQUEST['state']) || ($_REQUEST['state'] !== $this->getState())) { + throw new OpenIDConnectClientException('Unable to determine state'); + } + // Cleanup state + $this->unsetState(); + + // Request ID Token $code = $_REQUEST['code']; $token_json = $this->requestTokens($code); @@ -319,14 +327,6 @@ public function authenticate(): bool throw new OpenIDConnectClientException('Got response: ' . $token_json->error); } - // Do an OpenID Connect session check - if (!isset($_REQUEST['state']) || ($_REQUEST['state'] !== $this->getState())) { - throw new OpenIDConnectClientException('Unable to determine state'); - } - - // Cleanup state - $this->unsetState(); - if (!property_exists($token_json, 'id_token')) { throw new OpenIDConnectClientException('User did not authorize openid scope.'); } @@ -380,7 +380,7 @@ public function authenticate(): bool $accessToken = $_REQUEST['access_token'] ?? null; // Do an OpenID Connect session check - if (!isset($_REQUEST['state']) || ($_REQUEST['state'] !== $this->getState())) { + if (!isset($_REQUEST['state']) || ($_REQUEST['state'] !== $this->getState())) { throw new OpenIDConnectClientException('Unable to determine state'); }