Skip to content

Commit d84ce65

Browse files
committed
:octocat: extract (protected) method OAuthProvider::addBasicAuthHeader()
1 parent 5c9af4c commit d84ce65

File tree

5 files changed

+25
-22
lines changed

5 files changed

+25
-22
lines changed

src/Core/OAuth2Provider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Psr\Http\Message\{RequestInterface, ResponseInterface, UriInterface};
1717
use Throwable;
1818
use function array_merge, date, explode, hash, hash_equals, implode, in_array, is_array, random_int, sodium_bin2base64, sprintf;
19-
use const PHP_QUERY_RFC1738, PHP_VERSION_ID, SODIUM_BASE64_VARIANT_ORIGINAL, SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING;
19+
use const PHP_QUERY_RFC1738, PHP_VERSION_ID, SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING;
2020

2121
/**
2222
* Implements an abstract OAuth2 provider with all methods required by the OAuth2Interface.
@@ -291,12 +291,10 @@ protected function getClientCredentialsTokenRequestBodyParams(array|null $scopes
291291
* sends a request to the client credentials endpoint, using basic authentication
292292
*/
293293
protected function sendClientCredentialsTokenRequest(string $url, array $body):ResponseInterface{
294-
$auth = sodium_bin2base64(sprintf('%s:%s', $this->options->key, $this->options->secret), SODIUM_BASE64_VARIANT_ORIGINAL);
295294

296295
$request = $this->requestFactory
297296
->createRequest('POST', $url)
298297
->withHeader('Accept-Encoding', 'identity')
299-
->withHeader('Authorization', sprintf('Basic %s', $auth))
300298
->withHeader('Content-Type', 'application/x-www-form-urlencoded')
301299
->withBody($this->streamFactory->createStream(QueryUtil::build($body, PHP_QUERY_RFC1738)))
302300
;
@@ -305,6 +303,8 @@ protected function sendClientCredentialsTokenRequest(string $url, array $body):R
305303
$request = $request->withHeader($header, $value);
306304
}
307305

306+
$request = $this->addBasicAuthHeader($request);
307+
308308
return $this->http->sendRequest($request);
309309
}
310310

src/Core/OAuthProvider.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
use Psr\Log\{LoggerInterface, NullLogger};
2525
use ReflectionClass, UnhandledMatchError;
2626
use function array_merge, array_shift, explode, implode, in_array, is_array, is_string,
27-
json_encode, ltrim, random_bytes, rtrim, sodium_bin2hex, sprintf, str_contains,
28-
str_starts_with, strip_tags, strtolower;
29-
use const PHP_QUERY_RFC1738;
27+
json_encode, ltrim, random_bytes, rtrim, sodium_bin2hex, sodium_bin2base64,
28+
sprintf, str_contains, str_starts_with, strip_tags, strtolower;
29+
use const PHP_QUERY_RFC1738, SODIUM_BASE64_VARIANT_ORIGINAL;
3030

3131
/**
3232
* Implements an abstract OAuth provider with all methods required by the OAuthInterface.
@@ -236,6 +236,15 @@ protected function cleanBodyParams(iterable $params):array{
236236
return QueryUtil::cleanParams($params, QueryUtil::BOOLEANS_AS_BOOL, true);
237237
}
238238

239+
/**
240+
* Adds an "Authorization: Basic <base64(key:secret)>" header to the given request
241+
*/
242+
protected function addBasicAuthHeader(RequestInterface $request):RequestInterface{
243+
$auth = sodium_bin2base64(sprintf('%s:%s', $this->options->key, $this->options->secret), SODIUM_BASE64_VARIANT_ORIGINAL);
244+
245+
return $request->withHeader('Authorization', sprintf('Basic %s', $auth));
246+
}
247+
239248
/**
240249
* returns a 32 byte random string (in hexadecimal representation) for use as a nonce
241250
*

src/Providers/BigCartel.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
namespace chillerlan\OAuth\Providers;
1313

1414
use chillerlan\OAuth\Core\{AccessToken, AuthenticatedUser, CSRFToken, OAuth2Provider, TokenInvalidate, UserInfo};
15-
use function sodium_bin2base64, sprintf;
16-
use const SODIUM_BASE64_VARIANT_ORIGINAL;
15+
use function sprintf;
1716

1817
/**
1918
* BigCartel OAuth2
@@ -58,13 +57,11 @@ public function me():AuthenticatedUser{
5857
public function invalidateAccessToken(AccessToken|null $token = null):bool{
5958
$tokenToInvalidate = ($token ?? $this->storage->getAccessToken($this->name));
6059

61-
$auth = sodium_bin2base64(sprintf('%s:%s', $this->options->key, $this->options->secret), SODIUM_BASE64_VARIANT_ORIGINAL);
62-
6360
$request = $this->requestFactory
6461
->createRequest('POST', sprintf('%s/%s', $this->revokeURL, $this->getAccountID($tokenToInvalidate)))
65-
->withHeader('Authorization', sprintf('Basic %s', $auth))
6662
;
6763

64+
$request = $this->addBasicAuthHeader($request);
6865
$response = $this->http->sendRequest($request);
6966

7067
if($response->getStatusCode() === 204){

src/Providers/PayPal.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
use chillerlan\HTTP\Utils\QueryUtil;
1717
use chillerlan\OAuth\Core\{AuthenticatedUser, ClientCredentials, CSRFToken, OAuth2Provider, TokenRefresh, UserInfo};
1818
use Psr\Http\Message\ResponseInterface;
19-
use function sodium_bin2base64, sprintf;
20-
use const PHP_QUERY_RFC1738, SODIUM_BASE64_VARIANT_ORIGINAL;
19+
use const PHP_QUERY_RFC1738;
2120

2221
/**
2322
* PayPal OAuth2
@@ -58,16 +57,16 @@ protected function getAccessTokenRequestBodyParams(string $code):array{
5857
* @inheritDoc
5958
*/
6059
protected function sendAccessTokenRequest(string $url, array $body):ResponseInterface{
61-
$auth = sodium_bin2base64(sprintf('%s:%s', $this->options->key, $this->options->secret), SODIUM_BASE64_VARIANT_ORIGINAL);
6260

6361
$request = $this->requestFactory
6462
->createRequest('POST', $url)
6563
->withHeader('Accept-Encoding', 'identity')
66-
->withHeader('Authorization', sprintf('Basic %s', $auth))
6764
->withHeader('Content-Type', 'application/x-www-form-urlencoded')
6865
->withBody($this->streamFactory->createStream(QueryUtil::build($body, PHP_QUERY_RFC1738)))
6966
;
7067

68+
$request = $this->addBasicAuthHeader($request);
69+
7170
return $this->http->sendRequest($request);
7271
}
7372

src/Providers/Reddit.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
OAuth2Provider, TokenInvalidate, TokenRefresh, UserInfo
2020
};
2121
use Psr\Http\Message\ResponseInterface;
22-
use function sodium_bin2base64, sprintf;
23-
use const PHP_QUERY_RFC1738, SODIUM_BASE64_VARIANT_ORIGINAL;
22+
use function sprintf;
23+
use const PHP_QUERY_RFC1738;
2424

2525
/**
2626
* @see https://github.com/reddit-archive/reddit/wiki/OAuth2
@@ -99,16 +99,16 @@ protected function getAccessTokenRequestBodyParams(string $code):array{
9999
* @inheritDoc
100100
*/
101101
protected function sendAccessTokenRequest(string $url, array $body):ResponseInterface{
102-
$auth = sodium_bin2base64(sprintf('%s:%s', $this->options->key, $this->options->secret), SODIUM_BASE64_VARIANT_ORIGINAL);
103102

104103
$request = $this->requestFactory
105104
->createRequest('POST', $url)
106105
->withHeader('Accept-Encoding', 'identity')
107-
->withHeader('Authorization', sprintf('Basic %s', $auth))
108106
->withHeader('Content-Type', 'application/x-www-form-urlencoded')
109107
->withBody($this->streamFactory->createStream(QueryUtil::build($body, PHP_QUERY_RFC1738)))
110108
;
111109

110+
$request = $this->addBasicAuthHeader($request);
111+
112112
return $this->http->sendRequest($request);
113113
}
114114

@@ -143,14 +143,12 @@ public function invalidateAccessToken(AccessToken $token = null):bool{
143143
'token_type_hint' => 'access_token',
144144
];
145145

146-
$auth = sodium_bin2base64(sprintf('%s:%s', $this->options->key, $this->options->secret), SODIUM_BASE64_VARIANT_ORIGINAL);
147-
148146
$request = $this->requestFactory
149147
->createRequest('POST', $this->revokeURL)
150-
->withHeader('Authorization', sprintf('Basic %s', $auth))
151148
->withHeader('Content-Type', 'application/x-www-form-urlencoded')
152149
;
153150

151+
$request = $this->addBasicAuthHeader($request);
154152
$request = $this->setRequestBody($bodyParams, $request);
155153
$response = $this->http->sendRequest($request);
156154

0 commit comments

Comments
 (0)