Skip to content

Commit 7ef4516

Browse files
Validate state before ID Token request
1 parent f7c91b9 commit 7ef4516

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/OpenIDConnectClient.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,15 @@ public function authenticate(): bool
306306

307307
// If we have an authorization code then proceed to request a token
308308
if (isset($_REQUEST['code'])) {
309+
// Do an OpenID Connect session check
310+
if (!isset($_REQUEST['state']) || ($_REQUEST['state'] !== $this->getState())) {
311+
throw new OpenIDConnectClientException('Unable to determine state');
312+
}
309313

314+
// Cleanup state
315+
$this->unsetState();
316+
317+
// Request ID Token
310318
$code = $_REQUEST['code'];
311319
$token_json = $this->requestTokens($code);
312320

@@ -318,14 +326,6 @@ public function authenticate(): bool
318326
throw new OpenIDConnectClientException('Got response: ' . $token_json->error);
319327
}
320328

321-
// Do an OpenID Connect session check
322-
if (!isset($_REQUEST['state']) || ($_REQUEST['state'] !== $this->getState())) {
323-
throw new OpenIDConnectClientException('Unable to determine state');
324-
}
325-
326-
// Cleanup state
327-
$this->unsetState();
328-
329329
if (!property_exists($token_json, 'id_token')) {
330330
throw new OpenIDConnectClientException('User did not authorize openid scope.');
331331
}
@@ -379,7 +379,7 @@ public function authenticate(): bool
379379
$accessToken = $_REQUEST['access_token'] ?? null;
380380

381381
// Do an OpenID Connect session check
382-
if (!isset($_REQUEST['state']) || ($_REQUEST['state'] !== $this->getState())) {
382+
if (!isset($_REQUEST['state']) || ($_REQUEST['state'] !== $this->getState())) {
383383
throw new OpenIDConnectClientException('Unable to determine state');
384384
}
385385

0 commit comments

Comments
 (0)