Skip to content

Commit 3a5ccbe

Browse files
authored
Merge pull request #199 from harphield/fix-generate-pkce-code-verifier
Fix: generatePKCECodeVerifier length
2 parents ee93abe + 5dbe1c8 commit 3a5ccbe

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/OAuth2/AbstractProvider.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ abstract class AbstractProvider extends AbstractBaseProvider
2727

2828
protected bool $pkce = false;
2929

30+
protected int $pkceCodeVerifierByteLength = 96;
31+
3032
/**
3133
* @return string
3234
*/
@@ -50,7 +52,7 @@ public function getAuthUrlParameters(): array
5052
$parameters['response_type'] = 'code';
5153

5254
if ($this->pkce) {
53-
$codeVerifier = $this->generatePKCECodeVerifier();
55+
$codeVerifier = $this->generatePKCECodeVerifier($this->pkceCodeVerifierByteLength);
5456
$this->session->set('code_verifier', $codeVerifier);
5557

5658
$parameters['code_challenge'] = $this->generatePKCECodeChallenge($codeVerifier);
@@ -60,13 +62,15 @@ public function getAuthUrlParameters(): array
6062
return $parameters;
6163
}
6264

63-
private function generatePKCECodeVerifier(int $length = 128)
65+
private function generatePKCECodeVerifier(int $byteLength = 96): string
6466
{
65-
if ($length < 43 || $length > 128) {
66-
throw new \Exception("Length must be between 43 and 128");
67+
if ($byteLength < 32 || $byteLength > 96) {
68+
throw new \Exception(
69+
"Final length must be between 43 and 128, so the number of random bytes must be between 32 and 96"
70+
);
6771
}
6872

69-
$randomBytes = random_bytes($length);
73+
$randomBytes = random_bytes($byteLength);
7074
return rtrim(strtr(base64_encode($randomBytes), '+/', '-_'), '=');
7175
}
7276

0 commit comments

Comments
 (0)