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

Commit 0cc8591

Browse files
committed
🛀 auth method cleanup
1 parent 5eb626b commit 0cc8591

File tree

5 files changed

+32
-40
lines changed

5 files changed

+32
-40
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ Protected properties:
126126

127127
property | description
128128
-------- | -----------
129-
`$authMethod` | the authentication method, one of `OAuth2Interface::AUTH_METHODS_HEADER` or `OAuth2Interface::AUTH_METHODS_QUERY`
129+
`$authMethod` | the authentication method, `OAuth2Interface::AUTH_METHOD_HEADER` (default) or `OAuth2Interface::AUTH_METHOD_QUERY`
130+
`$authMethodHeader` | the name of the `Authorization` header in case `OAuth2Interface::AUTH_METHOD_HEADER` is used, defaults to `Bearer`
131+
`$authMethodQuery` | the name of the querystring in case `OAuth2Interface::AUTH_METHOD_QUERY` is used, defaults to `access_token`
130132
`$scopesDelimiter` | (optional) a delimiter string for the OAuth2 scopes, defaults to `' '` (space)
131133
`$refreshTokenURL` | (optional) a refresh token exchange URL, in case it differs from `$accessTokenURL`
132134
`$clientCredentialsTokenURL` | (optional) a client credentials token exchange URL, in case it differs from `$accessTokenURL`
@@ -166,7 +168,8 @@ class MyOauth2Provider extends Oauth2Provider implements ClientCredentials, CSRF
166168

167169
// optional
168170
protected $clientCredentialsTokenURL = 'https://example.com/oauth2/client_credentials';
169-
protected $authMethod = self::HEADER_BEARER;
171+
protected $authMethod = self::AUTH_METHOD_HEADER;
172+
protected $authMethodHeader = 'OAuth';
170173
protected $scopesDelimiter = ',';
171174
}
172175
```

src/Core/OAuth2Interface.php

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,8 @@
1616

1717
interface OAuth2Interface extends OAuthInterface{
1818

19-
const HEADER_OAUTH = 0;
20-
const HEADER_BEARER = 1;
21-
const QUERY_ACCESS_TOKEN = 2;
22-
const QUERY_OAUTH2_ACCESS_TOKEN = 3;
23-
const QUERY_APIKEY = 4;
24-
const QUERY_AUTH = 5;
25-
const QUERY_OAUTH_TOKEN = 6;
26-
27-
const AUTH_METHODS_HEADER = [
28-
self::HEADER_OAUTH => 'OAuth ',
29-
self::HEADER_BEARER => 'Bearer ',
30-
];
31-
32-
const AUTH_METHODS_QUERY = [
33-
self::QUERY_ACCESS_TOKEN => 'access_token',
34-
self::QUERY_OAUTH2_ACCESS_TOKEN => 'oauth2_access_token',
35-
self::QUERY_APIKEY => 'apikey',
36-
self::QUERY_AUTH => 'auth',
37-
self::QUERY_OAUTH_TOKEN => 'oauth_token',
38-
];
19+
const AUTH_METHOD_HEADER = 1;
20+
const AUTH_METHOD_QUERY = 2;
3921

4022
/**
4123
* Obtains an OAuth2 access token with the given $code, verifies the $state
@@ -60,5 +42,4 @@ public function getAccessToken(string $code, string $state = null):AccessToken;
6042
*/
6143
public function getAuthURL(array $params = null, array $scopes = null):UriInterface;
6244

63-
6445
}

src/Core/OAuth2Provider.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
use Psr\Http\Message\{RequestInterface, ResponseInterface, UriInterface};
1818

19-
use function array_key_exists, array_merge, base64_encode, date, hash_equals, http_build_query,
19+
use function array_merge, base64_encode, date, hash_equals, http_build_query,
2020
implode, is_array, json_decode, random_bytes, sha1, sprintf;
2121
use function chillerlan\HTTP\Psr7\{decompress_content, merge_query};
2222

@@ -27,7 +27,17 @@ abstract class OAuth2Provider extends OAuthProvider implements OAuth2Interface{
2727
/**
2828
* @var int
2929
*/
30-
protected $authMethod = self::HEADER_BEARER;
30+
protected $authMethod = self::AUTH_METHOD_HEADER;
31+
32+
/**
33+
* @var string
34+
*/
35+
protected $authMethodHeader = 'Bearer';
36+
37+
/**
38+
* @var string
39+
*/
40+
protected $authMethodQuery = 'access_token';
3141

3242
/**
3343
* @var string
@@ -150,19 +160,17 @@ public function getAccessToken(string $code, string $state = null):AccessToken{
150160
*/
151161
public function getRequestAuthorization(RequestInterface $request, AccessToken $token):RequestInterface{
152162

153-
if(array_key_exists($this->authMethod, OAuth2Interface::AUTH_METHODS_HEADER)){
154-
$request = $request->withHeader('Authorization', OAuth2Interface::AUTH_METHODS_HEADER[$this->authMethod].$token->accessToken);
163+
if($this->authMethod === OAuth2Interface::AUTH_METHOD_HEADER){
164+
return $request->withHeader('Authorization', $this->authMethodHeader.' '.$token->accessToken);
155165
}
156-
elseif(array_key_exists($this->authMethod, OAuth2Interface::AUTH_METHODS_QUERY)){
157-
$uri = merge_query((string)$request->getUri(), [OAuth2Interface::AUTH_METHODS_QUERY[$this->authMethod] => $token->accessToken]);
158166

159-
$request = $request->withUri($this->uriFactory->createUri($uri));
160-
}
161-
else{
162-
throw new ProviderException('invalid auth type');
167+
if($this->authMethod === OAuth2Interface::AUTH_METHOD_QUERY){
168+
$uri = merge_query($request->getUri()->__toString(), [$this->authMethodQuery => $token->accessToken]);
169+
170+
return $request->withUri($this->uriFactory->createUri($uri));
163171
}
164172

165-
return $request;
173+
throw new ProviderException('invalid auth type');
166174
}
167175

168176
/**

tests/Providers/GenericOAuth2Test.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ protected function getProvider():OAuthInterface{
3232
protected $authURL = 'https://example.com/oauth2/authorize';
3333
protected $accessTokenURL = 'https://example.com/oauth2/token';
3434
protected $userRevokeURL = 'https://account.example.com/apps/';
35-
protected $endpointMap = TestEndpoints::class;
35+
protected $endpointMap = TestEndpoints::class;
3636
protected $authHeaders = ['foo' => 'bar'];
3737
protected $apiHeaders = ['foo' => 'bar'];
38-
protected $authMethod = OAuth2Provider::QUERY_ACCESS_TOKEN;
38+
protected $authMethod = OAuth2Provider::AUTH_METHOD_QUERY;
3939

4040
};
4141

tests/Providers/OAuth2ProviderTestAbstract.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,16 @@ public function testGetRequestAuthorization(){
106106
$authMethod = $this->getProperty('authMethod')->getValue($this->provider);
107107

108108
// header (default)
109-
if(isset(OAuth2Interface::AUTH_METHODS_HEADER[$authMethod])){
109+
if($authMethod === OAuth2Interface::AUTH_METHOD_HEADER){
110110
$this->assertStringContainsString(
111-
OAuth2Interface::AUTH_METHODS_HEADER[$authMethod].'test_token',
111+
$this->getProperty('authMethodHeader')->getValue($this->provider).' test_token',
112112
$this->provider->getRequestAuthorization($request, $token)->getHeaderLine('Authorization')
113113
);
114114
}
115115
// query
116-
elseif(isset(OAuth2Interface::AUTH_METHODS_QUERY[$authMethod])){
116+
elseif($authMethod === OAuth2Interface::AUTH_METHOD_QUERY){
117117
$this->assertStringContainsString(
118-
OAuth2Interface::AUTH_METHODS_QUERY[$authMethod].'=test_token',
118+
$this->getProperty('authMethodQuery')->getValue($this->provider).'=test_token',
119119
$this->provider->getRequestAuthorization($request, $token)->getUri()->getQuery()
120120
);
121121
}

0 commit comments

Comments
 (0)