Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.

Commit 21236d7

Browse files
committed
:octocat: move nonce() method from OAuth1Provider to OAuthProvider
1 parent a67478f commit 21236d7

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

src/Core/OAuth1Provider.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use chillerlan\HTTP\Utils\{MessageUtil, QueryUtil};
1515
use chillerlan\OAuth\Providers\ProviderException;
1616
use Psr\Http\Message\{RequestInterface, ResponseInterface, UriInterface};
17-
use function array_merge, base64_encode, hash_hmac, implode, random_bytes, sodium_bin2hex, sprintf, strtoupper, time;
17+
use function array_merge, base64_encode, hash_hmac, implode, sprintf, strtoupper, time;
1818

1919
/**
2020
* Implements an abstract OAuth1 provider with all methods required by the OAuth1Interface.
@@ -114,15 +114,6 @@ protected function parseTokenResponse(ResponseInterface $response, bool $checkCa
114114
return $token;
115115
}
116116

117-
/**
118-
* returns a 32 byte random string (in hexadecimal representation) for use as a nonce
119-
*
120-
* @see https://datatracker.ietf.org/doc/html/rfc5849#section-3.3
121-
*/
122-
protected function nonce():string{
123-
return sodium_bin2hex(random_bytes(32));
124-
}
125-
126117
/**
127118
* Generates a request signature
128119
*

src/Core/OAuth2Provider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ public function setState(array $params):array{
298298
}
299299

300300
if(!isset($params['state'])){
301-
$params['state'] = sha1(random_bytes(256));
301+
$params['state'] = $this->nonce();
302302
}
303303

304304
$this->storage->storeCSRFState($params['state'], $this->serviceName);

src/Core/OAuthProvider.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
use Psr\Log\{LoggerInterface, NullLogger};
2525
use ReflectionClass;
2626
use function array_merge, array_shift, explode, implode, in_array, is_array, is_string,
27-
json_encode, ltrim, rtrim, sprintf, str_starts_with, strtolower;
27+
json_encode, ltrim, random_bytes, rtrim, sodium_bin2hex, sprintf, str_starts_with, strtolower;
2828
use const PHP_QUERY_RFC1738;
2929

3030
/**
@@ -235,6 +235,16 @@ protected function cleanBodyParams(iterable $params):array{
235235
return QueryUtil::cleanParams($params, QueryUtil::BOOLEANS_AS_BOOL, true);
236236
}
237237

238+
/**
239+
* returns a 32 byte random string (in hexadecimal representation) for use as a nonce
240+
*
241+
* @see https://datatracker.ietf.org/doc/html/rfc5849#section-3.3
242+
* @see https://datatracker.ietf.org/doc/html/rfc6749#section-10.12
243+
*/
244+
protected function nonce(int $bytes = 32):string{
245+
return sodium_bin2hex(random_bytes($bytes));
246+
}
247+
238248
/**
239249
* @inheritDoc
240250
* @throws \chillerlan\OAuth\Core\UnauthorizedAccessException

0 commit comments

Comments
 (0)