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

Commit a9e28ff

Browse files
committed
:octocat: const'd some internal properties
1 parent 55d8213 commit a9e28ff

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+240
-219
lines changed

src/Core/OAuth1Provider.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function getRequestToken():AccessToken{
6060
->withHeader('Content-Length', '0') // tumblr requires a content-length header set
6161
;
6262

63-
foreach($this->authHeaders as $header => $value){
63+
foreach($this::HEADERS_AUTH as $header => $value){
6464
$request = $request->withAddedHeader($header, $value);
6565
}
6666

src/Core/OAuth2Interface.php

+40
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,49 @@
1818
*/
1919
interface OAuth2Interface extends OAuthInterface{
2020

21+
/** @var int */
2122
public const AUTH_METHOD_HEADER = 1;
23+
/** @var int */
2224
public const AUTH_METHOD_QUERY = 2;
2325

26+
/**
27+
* Specifies the authentication method:
28+
*
29+
* - OAuth2Interface::AUTH_METHOD_HEADER (Bearer, OAuth, ...)
30+
* - OAuth2Interface::AUTH_METHOD_QUERY (access_token, ...)
31+
*
32+
* @var int
33+
*/
34+
public const AUTH_METHOD = self::AUTH_METHOD_HEADER;
35+
36+
/**
37+
* The name of the authentication header in case of OAuth2Interface::AUTH_METHOD_HEADER
38+
*
39+
* @var string
40+
*/
41+
public const AUTH_PREFIX_HEADER = 'Bearer';
42+
43+
/**
44+
* The name of the authentication query parameter in case of OAuth2Interface::AUTH_METHOD_QUERY
45+
*
46+
* @var string
47+
*/
48+
public const AUTH_PREFIX_QUERY = 'access_token';
49+
50+
/**
51+
* Default scopes to apply if none were provided via the $scopes parameter
52+
*
53+
* @var string[]
54+
*/
55+
public const DEFAULT_SCOPES = [];
56+
57+
/**
58+
* The delimiter string for scopes
59+
*
60+
* @var string
61+
*/
62+
public const SCOPE_DELIMITER = ' ';
63+
2464
/**
2565
* Obtains an OAuth2 access token with the given $code, verifies the $state
2666
* if the provider implements the CSRFToken interface, and returns an AccessToken object

src/Core/OAuth2Provider.php

+12-39
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,6 @@
2525
*/
2626
abstract class OAuth2Provider extends OAuthProvider implements OAuth2Interface{
2727

28-
/**
29-
* Specifies the authentication method:
30-
* - OAuth2Interface::AUTH_METHOD_HEADER (Bearer, OAuth, ...)
31-
* - OAuth2Interface::AUTH_METHOD_QUERY (access_token, ...)
32-
*/
33-
protected int $authMethod = self::AUTH_METHOD_HEADER;
34-
35-
/**
36-
* The name of the authentication header in case of OAuth2Interface::AUTH_METHOD_HEADER
37-
*/
38-
protected string $authMethodHeader = 'Bearer';
39-
40-
/**
41-
* The name of the authentication query parameter in case of OAuth2Interface::AUTH_METHOD_QUERY
42-
*/
43-
protected string $authMethodQuery = 'access_token';
44-
45-
/**
46-
* The delimiter string for scopes
47-
*/
48-
protected string $scopesDelimiter = ' ';
49-
5028
/**
5129
* An optional refresh token endpoint in case the provider supports TokenRefresh.
5230
* If the provider supports token refresh and $refreshTokenURL is null, $accessTokenURL will be used instead.
@@ -61,17 +39,12 @@ abstract class OAuth2Provider extends OAuthProvider implements OAuth2Interface{
6139
*/
6240
protected string|null $clientCredentialsTokenURL = null;
6341

64-
/**
65-
* Default scopes to apply if none were provided via the $scopes parameter in OAuth2Provider::getAuthURL()
66-
*/
67-
protected array $defaultScopes = [];
68-
6942
/**
7043
* @inheritDoc
7144
*/
7245
public function getAuthURL(array|null $params = null, array|null $scopes = null):UriInterface{
7346
$params ??= [];
74-
$scopes ??= $this->defaultScopes;
47+
$scopes ??= $this::DEFAULT_SCOPES;
7548

7649
unset($params['client_secret']);
7750

@@ -83,7 +56,7 @@ public function getAuthURL(array|null $params = null, array|null $scopes = null)
8356
]);
8457

8558
if(!empty($scopes)){
86-
$params['scope'] = implode($this->scopesDelimiter, $scopes);
59+
$params['scope'] = implode($this::SCOPE_DELIMITER, $scopes);
8760
}
8861

8962
if($this instanceof CSRFToken){
@@ -128,7 +101,7 @@ protected function parseTokenResponse(ResponseInterface $response):AccessToken{
128101
if(isset($data['scope']) || isset($data['scopes'])){
129102
$scope = ($data['scope'] ?? $data['scopes'] ?? []);
130103

131-
$token->scopes = (is_array($scope)) ? $scope : explode($this->scopesDelimiter, $scope);
104+
$token->scopes = (is_array($scope)) ? $scope : explode($this::SCOPE_DELIMITER, $scope);
132105
}
133106

134107
unset($data['expires_in'], $data['refresh_token'], $data['access_token'], $data['scope'], $data['scopes']);
@@ -161,7 +134,7 @@ public function getAccessToken(string $code, string|null $state = null):AccessTo
161134
->withHeader('Accept-Encoding', 'identity')
162135
->withBody($this->streamFactory->createStream(QueryUtil::build($body, PHP_QUERY_RFC1738)));
163136

164-
foreach($this->authHeaders as $header => $value){
137+
foreach($this::HEADERS_AUTH as $header => $value){
165138
$request = $request->withHeader($header, $value);
166139
}
167140

@@ -177,17 +150,17 @@ public function getAccessToken(string $code, string|null $state = null):AccessTo
177150
*/
178151
public function getRequestAuthorization(RequestInterface $request, AccessToken $token):RequestInterface{
179152

180-
if($this->authMethod === OAuth2Interface::AUTH_METHOD_HEADER){
181-
return $request->withHeader('Authorization', $this->authMethodHeader.' '.$token->accessToken);
153+
if($this::AUTH_METHOD === OAuth2Interface::AUTH_METHOD_HEADER){
154+
return $request->withHeader('Authorization', $this::AUTH_PREFIX_HEADER.' '.$token->accessToken);
182155
}
183156

184-
if($this->authMethod === OAuth2Interface::AUTH_METHOD_QUERY){
185-
$uri = QueryUtil::merge((string)$request->getUri(), [$this->authMethodQuery => $token->accessToken]);
157+
if($this::AUTH_METHOD === OAuth2Interface::AUTH_METHOD_QUERY){
158+
$uri = QueryUtil::merge((string)$request->getUri(), [$this::AUTH_PREFIX_QUERY => $token->accessToken]);
186159

187160
return $request->withUri($this->uriFactory->createUri($uri));
188161
}
189162

190-
throw new ProviderException('invalid auth type');
163+
throw new ProviderException('invalid auth AUTH_METHOD');
191164
}
192165

193166
/**
@@ -203,7 +176,7 @@ public function getClientCredentialsToken(array|null $scopes = null):AccessToken
203176
$params = ['grant_type' => 'client_credentials'];
204177

205178
if(!empty($scopes)){
206-
$params['scope'] = implode($this->scopesDelimiter, $scopes);
179+
$params['scope'] = implode($this::SCOPE_DELIMITER, $scopes);
207180
}
208181

209182
$request = $this->requestFactory
@@ -214,7 +187,7 @@ public function getClientCredentialsToken(array|null $scopes = null):AccessToken
214187
->withBody($this->streamFactory->createStream(QueryUtil::build($params, PHP_QUERY_RFC1738)))
215188
;
216189

217-
foreach($this->authHeaders as $header => $value){
190+
foreach($this::HEADERS_AUTH as $header => $value){
218191
$request = $request->withAddedHeader($header, $value);
219192
}
220193

@@ -267,7 +240,7 @@ public function refreshAccessToken(AccessToken|null $token = null):AccessToken{
267240
->withBody($this->streamFactory->createStream(QueryUtil::build($body, PHP_QUERY_RFC1738)))
268241
;
269242

270-
foreach($this->authHeaders as $header => $value){
243+
foreach($this::HEADERS_AUTH as $header => $value){
271244
$request = $request->withAddedHeader($header, $value);
272245
}
273246

src/Core/OAuthInterface.php

+14
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@
3030
*/
3131
interface OAuthInterface extends ClientInterface{
3232

33+
/**
34+
* additional headers to use during authentication
35+
*
36+
* @var array
37+
*/
38+
public const HEADERS_AUTH = [];
39+
40+
/**
41+
* additional headers to use during API access
42+
*
43+
* @var array
44+
*/
45+
public const HEADERS_API = [];
46+
3347
/**
3448
* Prepares the URL with optional $params which redirects to the provider's authorization prompt
3549
* and returns a PSR-7 UriInterface with all necessary parameters set

src/Core/OAuthProvider.php

+2-11
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,6 @@ abstract class OAuthProvider implements OAuthInterface{
4949
*/
5050
protected string $accessTokenURL;
5151

52-
/**
53-
* additional headers to use during authentication
54-
*/
55-
protected array $authHeaders = [];
56-
57-
/**
58-
* additional headers to use during API access
59-
*/
60-
protected array $apiHeaders = [];
61-
6252
/*
6353
* magic properties (public readonly would be cool if the implementation wasn't fucking stupid)
6454
*/
@@ -249,7 +239,8 @@ public function request(
249239
* Prepare request headers
250240
*/
251241
protected function getRequestHeaders(array|null $headers = null):array{
252-
return array_merge($this->apiHeaders, ($headers ?? []));
242+
/** @noinspection PhpParamsInspection sup PHPStorm?? */
243+
return array_merge($this::HEADERS_API, ($headers ?? []));
253244
}
254245

255246
/**

src/Providers/Amazon.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Amazon extends OAuth2Provider implements CSRFToken, TokenRefresh{
2929
public const SCOPE_PROFILE_USER_ID = 'profile:user_id';
3030
public const SCOPE_POSTAL_CODE = 'postal_code';
3131

32-
protected array $defaultScopes = [
32+
public const DEFAULT_SCOPES = [
3333
self::SCOPE_PROFILE,
3434
self::SCOPE_PROFILE_USER_ID,
3535
];

src/Providers/BattleNet.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
*/
2424
class BattleNet extends OAuth2Provider implements ClientCredentials, CSRFToken{
2525

26-
public const SCOPE_OPENID = 'openid';
27-
public const SCOPE_PROFILE_D3 = 'd3.profile';
28-
public const SCOPE_PROFILE_SC2 = 'sc2.profile';
29-
public const SCOPE_PROFILE_WOW = 'wow.profile';
26+
public const SCOPE_OPENID = 'openid';
27+
public const SCOPE_PROFILE_D3 = 'd3.profile';
28+
public const SCOPE_PROFILE_SC2 = 'sc2.profile';
29+
public const SCOPE_PROFILE_WOW = 'wow.profile';
3030

31-
protected array $defaultScopes = [
31+
public const DEFAULT_SCOPES = [
3232
self::SCOPE_OPENID,
3333
self::SCOPE_PROFILE_D3,
3434
self::SCOPE_PROFILE_SC2,

src/Providers/BigCartel.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,17 @@
2424
*/
2525
class BigCartel extends OAuth2Provider implements CSRFToken, TokenInvalidate{
2626

27+
public const HEADERS_API = [
28+
'Accept' => 'application/vnd.api+json',
29+
];
30+
2731
protected string $authURL = 'https://my.bigcartel.com/oauth/authorize';
2832
protected string $accessTokenURL = 'https://api.bigcartel.com/oauth/token';
2933
protected string $revokeURL = 'https://api.bigcartel.com/oauth/deauthorize/%s'; // sprintf() user id!
3034
protected string $apiURL = 'https://api.bigcartel.com/v1';
3135
protected string|null $userRevokeURL = 'https://my.bigcartel.com/account';
3236
protected string|null $apiDocs = 'https://developers.bigcartel.com/api/v1';
3337
protected string|null $applicationURL = 'https://bigcartel.wufoo.com/forms/big-cartel-api-application/';
34-
protected array $apiHeaders = ['Accept' => 'application/vnd.api+json'];
3538

3639
/**
3740
* @inheritDoc

src/Providers/Deezer.php

+12-11
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,30 @@
2424
*/
2525
class Deezer extends OAuth2Provider implements CSRFToken{
2626

27-
public const SCOPE_BASIC = 'basic_access';
28-
public const SCOPE_EMAIL = 'email';
29-
public const SCOPE_OFFLINE_ACCESS = 'offline_access';
30-
public const SCOPE_MANAGE_LIBRARY = 'manage_library';
31-
public const SCOPE_MANAGE_COMMUNITY = 'manage_community';
32-
public const SCOPE_DELETE_LIBRARY = 'delete_library';
33-
public const SCOPE_LISTENING_HISTORY = 'listening_history';
34-
35-
protected array $defaultScopes = [
27+
public const SCOPE_BASIC = 'basic_access';
28+
public const SCOPE_EMAIL = 'email';
29+
public const SCOPE_OFFLINE_ACCESS = 'offline_access';
30+
public const SCOPE_MANAGE_LIBRARY = 'manage_library';
31+
public const SCOPE_MANAGE_COMMUNITY = 'manage_community';
32+
public const SCOPE_DELETE_LIBRARY = 'delete_library';
33+
public const SCOPE_LISTENING_HISTORY = 'listening_history';
34+
35+
public const DEFAULT_SCOPES = [
3636
self::SCOPE_BASIC,
3737
self::SCOPE_EMAIL,
3838
self::SCOPE_OFFLINE_ACCESS,
3939
self::SCOPE_MANAGE_LIBRARY,
4040
self::SCOPE_LISTENING_HISTORY,
4141
];
4242

43+
public const AUTH_METHOD = self::AUTH_METHOD_QUERY;
44+
4345
protected string $authURL = 'https://connect.deezer.com/oauth/auth.php';
4446
protected string $accessTokenURL = 'https://connect.deezer.com/oauth/access_token.php';
4547
protected string $apiURL = 'https://api.deezer.com';
4648
protected string|null $userRevokeURL = 'https://www.deezer.com/account/apps';
4749
protected string|null $apiDocs = 'https://developers.deezer.com/api';
4850
protected string|null $applicationURL = 'http://developers.deezer.com/myapps';
49-
protected int $authMethod = self::AUTH_METHOD_QUERY;
5051

5152
/**
5253
* @inheritDoc
@@ -61,7 +62,7 @@ public function getAuthURL(array|null $params = null, array|null $scopes = null)
6162
$params = array_merge($params, [
6263
'app_id' => $this->options->key,
6364
'redirect_uri' => $this->options->callbackURL,
64-
'perms' => implode($this->scopesDelimiter, ($scopes ?? [])),
65+
'perms' => implode($this::SCOPE_DELIMITER, ($scopes ?? [])),
6566
]);
6667

6768
$params = $this->setState($params);

src/Providers/DeviantArt.php

+17-14
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,34 @@
2121
*/
2222
class DeviantArt extends OAuth2Provider implements ClientCredentials, CSRFToken, TokenInvalidate, TokenRefresh{
2323

24-
public const SCOPE_BASIC = 'basic';
25-
public const SCOPE_BROWSE = 'browse';
26-
public const SCOPE_COLLECTION = 'collection';
27-
public const SCOPE_COMMENT_POST = 'comment.post';
28-
public const SCOPE_FEED = 'feed';
29-
public const SCOPE_GALLERY = 'gallery';
30-
public const SCOPE_MESSAGE = 'message';
31-
public const SCOPE_NOTE = 'note';
32-
public const SCOPE_STASH = 'stash';
33-
public const SCOPE_USER = 'user';
34-
public const SCOPE_USER_MANAGE = 'user.manage';
35-
36-
protected array $defaultScopes = [
24+
public const SCOPE_BASIC = 'basic';
25+
public const SCOPE_BROWSE = 'browse';
26+
public const SCOPE_COLLECTION = 'collection';
27+
public const SCOPE_COMMENT_POST = 'comment.post';
28+
public const SCOPE_FEED = 'feed';
29+
public const SCOPE_GALLERY = 'gallery';
30+
public const SCOPE_MESSAGE = 'message';
31+
public const SCOPE_NOTE = 'note';
32+
public const SCOPE_STASH = 'stash';
33+
public const SCOPE_USER = 'user';
34+
public const SCOPE_USER_MANAGE = 'user.manage';
35+
36+
public const DEFAULT_SCOPES = [
3737
self::SCOPE_BASIC,
3838
self::SCOPE_BROWSE,
3939
];
4040

41+
public const HEADERS_API = [
42+
'dA-minor-version' => '20210526',
43+
];
44+
4145
protected string $authURL = 'https://www.deviantart.com/oauth2/authorize';
4246
protected string $accessTokenURL = 'https://www.deviantart.com/oauth2/token';
4347
protected string $revokeURL = 'https://www.deviantart.com/oauth2/revoke';
4448
protected string $apiURL = 'https://www.deviantart.com/api/v1/oauth2';
4549
protected string|null $userRevokeURL = 'https://www.deviantart.com/settings/applications';
4650
protected string|null $apiDocs = 'https://www.deviantart.com/developers/';
4751
protected string|null $applicationURL = 'https://www.deviantart.com/developers/apps';
48-
protected array $apiHeaders = ['dA-minor-version' => '20210526'];
4952

5053
/**
5154
* @inheritDoc

src/Providers/Discogs.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,17 @@
2121
*/
2222
class Discogs extends OAuth1Provider{
2323

24+
public const HEADERS_API = [
25+
'Accept' => 'application/vnd.discogs.v2.discogs+json',
26+
];
27+
2428
protected string $requestTokenURL = 'https://api.discogs.com/oauth/request_token';
2529
protected string $authURL = 'https://www.discogs.com/oauth/authorize';
2630
protected string $accessTokenURL = 'https://api.discogs.com/oauth/access_token';
2731
protected string $apiURL = 'https://api.discogs.com';
2832
protected string|null $userRevokeURL = 'https://www.discogs.com/settings/applications';
2933
protected string|null $apiDocs = 'https://www.discogs.com/developers/';
30-
protected string|null $applicationURL = 'https://www.discogs.com/settings/developers';
31-
protected array $apiHeaders = ['Accept' => 'application/vnd.discogs.v2.discogs+json'];
32-
34+
protected string|null $applicationURL = 'https://www.discogs.com/settings/developers';
3335
/**
3436
* @inheritDoc
3537
*/

0 commit comments

Comments
 (0)