Skip to content

Commit

Permalink
🚿 OAuthProvider: remove magic properties
Browse files Browse the repository at this point in the history
  • Loading branch information
codemasher committed May 18, 2024
1 parent ea86ace commit f01e147
Show file tree
Hide file tree
Showing 19 changed files with 121 additions and 90 deletions.
10 changes: 5 additions & 5 deletions examples/create-description.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@
/** @var \OAuthExampleProviderFactory $factory */
$provider = $factory->getProvider($p['fqcn'], OAuthExampleProviderFactory::STORAGE_MEMORY);

$oauth = match(true){
$oauthVersion = match(true){
$provider instanceof OAuth2Interface => '2',
$provider instanceof OAuth1Interface => '1',
default => '-',
};

$table[] = '| ['.$p['name'].']('.$provider->apiDocs.')'.
' | [link]('.$provider->applicationURL.')'.
' | '.($provider->userRevokeURL !== null ? '[link]('.$provider->userRevokeURL.')' : '').
' | '.$oauth.
$table[] = '| ['.$provider->getName().']('.$provider->getApiDocURL().')'.
' | [link]('.$provider->getApplicationURL().')'.
' | '.($provider->getUserRevokeURL() !== null ? '[link]('.$provider->getUserRevokeURL().')' : '').
' | '.$oauthVersion.
' | '.(($provider instanceof UserInfo) ? '' : '').
' | '.(($provider instanceof CSRFToken) ? '' : '').
' | '.(($provider instanceof PKCE) ? '' : '').
Expand Down
2 changes: 1 addition & 1 deletion examples/get-token/LastFM.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/

$provider = $factory->getProvider(LastFM::class);
$name = $provider->name;
$name = $provider->getName();

// step 2: redirect to the provider's login screen
if(isset($_GET['login']) && $_GET['login'] === $name){
Expand Down
2 changes: 1 addition & 1 deletion examples/get-token/Steam.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @var \chillerlan\OAuth\Providers\Steam $provider
*/
$provider = $factory->getProvider(Steam::class);
$name = $provider->name;
$name = $provider->getName();

// step 2: redirect to the provider's login screen
if(isset($_GET['login']) && $_GET['login'] === $name){
Expand Down
2 changes: 1 addition & 1 deletion examples/get-token/_flow-oauth1.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @var array|null $PARAMS
*/

$name = $provider->name;
$name = $provider->getName();

// step 2: redirect to the provider's login screen
if(isset($_GET['login']) && $_GET['login'] === $name){
Expand Down
2 changes: 1 addition & 1 deletion examples/get-token/_flow-oauth2.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @var array|null $SCOPES
*/

$name = $provider->name;
$name = $provider->getName();

// step 2: redirect to the provider's login screen
if(isset($_GET['login']) && $_GET['login'] === $name){
Expand Down
26 changes: 20 additions & 6 deletions src/Core/OAuthInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@

/**
* Specifies the basic methods for an OAuth provider.
*
* @property string $name (magic) The name of the provider/class
* @property string $apiURL (magic) The API base URL
* @property string|null $apiDocs (magic) An optional link to the provider's API docs
* @property string|null $applicationURL (magic) An optional URL to the provider's credential registration/application page
* @property string|null $userRevokeURL (magic) An optional link to the page where a user can revoke access tokens
*/
interface OAuthInterface extends ClientInterface{

Expand Down Expand Up @@ -76,6 +70,26 @@ interface OAuthInterface extends ClientInterface{
*/
public const SCOPES_DELIMITER = ' ';

/**
* Returns the name of the provider/class
*/
public function getName():string;

/**
* Returns the link to the provider's API docs, or null if the value is not set
*/
public function getApiDocURL():string|null;

/**
* Returns the link to the provider's credential registration/application page, or null if the value is not set
*/
public function getApplicationURL():string|null;

/**
* Returns the link to the page where a user can revoke access tokens, or null if the value is not set
*/
public function getUserRevokeURL():string|null;

/**
* Prepares the URL with optional $params which redirects to the provider's authorization prompt
* and returns a PSR-7 UriInterface with all necessary parameters set.
Expand Down
86 changes: 56 additions & 30 deletions src/Core/OAuthProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@
*/
abstract class OAuthProvider implements OAuthInterface{

/**
* The options instance
*/
protected OAuthOptions|SettingsContainerInterface $options;

/**
* The PSR-18 HTTP client
*/
Expand All @@ -62,50 +57,61 @@ abstract class OAuthProvider implements OAuthInterface{
protected UriFactoryInterface $uriFactory;

/**
* A storage instance
* A PSR-3 logger
*/
protected OAuthStorageInterface $storage;
protected LoggerInterface $logger;

/**
* A PSR-3 logger
* The options instance
*/
protected LoggerInterface $logger;
protected OAuthOptions|SettingsContainerInterface $options;

/**
* A storage instance
*/
protected OAuthStorageInterface $storage;

/**
* the authorization URL
* The authorization URL
*/
protected string $authorizationURL = '';

/**
* the provider's access token exchange URL
* The access token exchange URL
*/
protected string $accessTokenURL = '';

/**
* an optional URL for application side token revocation
* An optional URL for application side token revocation
*
* @see \chillerlan\OAuth\Core\TokenInvalidate
*/
protected string $revokeURL = '';

/**
* magic properties
*
* @var string[]
* The API base URL
*/
protected string $apiURL = '';

/**
* The name of the provider/class
*/
protected const MAGIC_PROPERTIES = [
'apiDocs', 'apiURL', 'applicationURL', 'name', 'userRevokeURL',
];
protected string $name = '';

/*
* magic properties (doc in interface docblock)
/**
* An optional link to the provider's API docs
*/
protected string|null $apiDocs = null;

protected string $name = '';
protected string $apiURL = '';
protected string|null $apiDocs = null;
/**
* An optional URL to the provider's credential registration/application page
*/
protected string|null $applicationURL = null;
protected string|null $userRevokeURL = null;

/**
* An optional link to the page where a user can revoke access tokens
*/
protected string|null $userRevokeURL = null;

/**
* OAuthProvider constructor.
Expand Down Expand Up @@ -141,15 +147,35 @@ protected function construct():void{
}

/**
* Magic getter for the properties specified in self::ALLOWED_PROPERTIES
* @inheritDoc
* @codeCoverageIgnore
*/
final public function __get(string $name):string|null{
final public function getName():string{
return $this->name;
}

if(in_array($name, $this::MAGIC_PROPERTIES, true)){
return $this->{$name};
}
/**
* @inheritDoc
* @codeCoverageIgnore
*/
final public function getApiDocURL():string|null{
return $this->apiDocs;
}

return null;
/**
* @inheritDoc
* @codeCoverageIgnore
*/
final public function getApplicationURL():string|null{
return $this->applicationURL;
}

/**
* @inheritDoc
* @codeCoverageIgnore
*/
final public function getUserRevokeURL():string|null{
return $this->userRevokeURL;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/Providers/Live/AmazonAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ protected function assertMeResponse(AuthenticatedUser $user):void{
}

public function testMeUnauthorizedAccessException():void{
$token = $this->storage->getAccessToken($this->provider->name);
$token = $this->storage->getAccessToken($this->provider->getName());
// avoid refresh
$token->expires = AccessToken::NEVER_EXPIRES;
$token->refreshToken = null;
// invalidate token
$token->accessToken = 'Atza|nope'; // amazon tokens are prefixed

// using a temp storage here so that the local tokens won't be overwritten
$tempStorage = (new MemoryStorage)->storeAccessToken($token, $this->provider->name);
$tempStorage = (new MemoryStorage)->storeAccessToken($token, $this->provider->getName());

$this->provider->setStorage($tempStorage);

Expand Down
2 changes: 1 addition & 1 deletion tests/Providers/Live/MailChimpAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
final class MailChimpAPITest extends OAuth2ProviderLiveTestAbstract{

public function testGetTokenMetadata():void{
$token = $this->storage->getAccessToken($this->provider->name);
$token = $this->storage->getAccessToken($this->provider->getName());
$token = $this->provider->getTokenMetadata($token);

$this::assertSame($this->TEST_USER, $token->extraParams['accountname']);
Expand Down
4 changes: 2 additions & 2 deletions tests/Providers/Live/OAuthProviderLiveTestAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function testMeUnauthorizedAccessException():void{
$this::markTestSkipped('AuthenticatedUser N/A');
}

$token = $this->storage->getAccessToken($this->provider->name);
$token = $this->storage->getAccessToken($this->provider->getName());
// avoid refresh
$token->expires = AccessToken::NEVER_EXPIRES;
$token->refreshToken = null;
Expand All @@ -76,7 +76,7 @@ public function testMeUnauthorizedAccessException():void{
$token->accessTokenSecret = 'what';

// using a temp storage here so that the local tokens won't be overwritten
$tempStorage = (new MemoryStorage)->storeAccessToken($token, $this->provider->name);
$tempStorage = (new MemoryStorage)->storeAccessToken($token, $this->provider->getName());

$this->provider->setStorage($tempStorage);

Expand Down
2 changes: 1 addition & 1 deletion tests/Providers/Live/SteamAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class SteamAPITest extends OAuthProviderLiveTestAbstract{
protected function setUp():void{
parent::setUp();

$token = $this->storage->getAccessToken($this->provider->name);
$token = $this->storage->getAccessToken($this->provider->getName());

$this->id = $token->extraParams['id_int']; // SteamID64
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Providers/ProviderUnitTestAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ protected function getTestToken(array|null $params = null):AccessToken{
'expires' => 42,
// patreon requires a scope set
'scopes' => ['identity', 'scope1', 'scope2'],
'provider' => $this->provider->name,
'provider' => $this->provider->getName(),
];

return new AccessToken($params);
Expand Down
4 changes: 2 additions & 2 deletions tests/Providers/Unit/BattleNetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ final class BattleNetTest extends OAuth2ProviderUnitTestAbstract{

public function testSetRegion():void{
$this->provider->setRegion('cn');
$this::assertSame('https://gateway.battlenet.com.cn', $this->provider->apiURL);
$this::assertSame('https://gateway.battlenet.com.cn', $this->getReflectionProperty('apiURL'));

$this->provider->setRegion('us');
$this::assertSame('https://us.api.blizzard.com', $this->provider->apiURL);
$this::assertSame('https://us.api.blizzard.com', $this->getReflectionProperty('apiURL'));
}

public function testSetRegionException():void{
Expand Down
4 changes: 2 additions & 2 deletions tests/Providers/Unit/BigCartelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public function testTokenInvalidate():void{

$this->provider->storeAccessToken(new AccessToken(['expires' => 42, 'extraParams' => ['account_id' => 69]]));

$this::assertTrue($this->storage->hasAccessToken($this->provider->name));
$this::assertTrue($this->storage->hasAccessToken($this->provider->getName()));
$this::assertTrue($this->provider->invalidateAccessToken());
$this::assertFalse($this->storage->hasAccessToken($this->provider->name));
$this::assertFalse($this->storage->hasAccessToken($this->provider->getName()));
}

}
6 changes: 3 additions & 3 deletions tests/Providers/Unit/DeviantArtTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public function testTokenInvalidate():void{

$this->provider->storeAccessToken($token);

$this::assertTrue($this->storage->hasAccessToken($this->provider->name));
$this::assertTrue($this->storage->hasAccessToken($this->provider->getName()));
$this::assertTrue($this->provider->invalidateAccessToken());
$this::assertFalse($this->storage->hasAccessToken($this->provider->name));
$this::assertFalse($this->storage->hasAccessToken($this->provider->getName()));

// token via param

Expand All @@ -47,7 +47,7 @@ public function testTokenInvalidate():void{
$this->provider->storeAccessToken($token2);

$this::assertTrue($this->provider->invalidateAccessToken($token));
$this::assertSame('still here', $this->provider->getStorage()->getAccessToken($this->provider->name)->accessToken);
$this::assertSame('still here', $this->provider->getStorage()->getAccessToken($this->provider->getName())->accessToken);
}

}
Loading

0 comments on commit f01e147

Please sign in to comment.