Skip to content

Commit

Permalink
📖
Browse files Browse the repository at this point in the history
  • Loading branch information
codemasher committed May 20, 2024
1 parent f8a7457 commit c880698
Show file tree
Hide file tree
Showing 24 changed files with 77 additions and 30 deletions.
14 changes: 4 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ A transparent, framework-agnostic, easily extensible PHP [PSR-18](https://www.ph
- [RFC-7009: Token Revocation](https://datatracker.ietf.org/doc/html/rfc7009)
- [RFC-7636: PKCE](https://datatracker.ietf.org/doc/html/rfc7636) (Proof Key for Code Exchange)
- [RFC-9126: PAR](https://datatracker.ietf.org/doc/html/rfc9126) (Pushed Authorization Requests)
- ~~[RFC-9449: DPoP](https://datatracker.ietf.org/doc/html/rfc9449) (Demonstrating Proof of Possession)~~ ([planned](https://github.com/chillerlan/php-oauth/issues/3))
- Proprietary, OAuth-like authorization flows (e.g. [Last.fm](https://www.last.fm/api/authentication))
- Invalidation of access tokens (if supported by the provider)
- Several built-in provider implementations ([see below](#implemented-providers))
Expand All @@ -67,6 +68,9 @@ A transparent, framework-agnostic, easily extensible PHP [PSR-18](https://www.ph
- The user manual is at https://php-oauth.readthedocs.io/ ([sources](https://github.com/chillerlan/php-oauth/tree/main/docs))
- An API documentation created with [phpDocumentor](https://www.phpdoc.org/) can be found at https://chillerlan.github.io/php-oauth/
- The documentation for the `AccessToken`, `AuthenticatedUser` and `OAuthOptions` containers can be found here: [chillerlan/php-settings-container](https://github.com/chillerlan/php-settings-container#readme)
- There is [the suite of get-token examples](https://php-oauth.readthedocs.io/en/main/Usage/Using-examples.html), which is mostly intended for development, and there are self-contained examples for a quickstart:
- [OAuth1 example](https://github.com/chillerlan/php-oauth/tree/main/examples/example-oauth1.php)
- [OAuth2 example](https://github.com/chillerlan/php-oauth/tree/main/examples/example-oauth2.php)


## Installation with [composer](https://getcomposer.org)
Expand Down Expand Up @@ -95,16 +99,6 @@ composer require chillerlan/php-oauth
Note: replace `dev-main` with a [version constraint](https://getcomposer.org/doc/articles/versions.md#writing-version-constraints), e.g. `^1.0` - see [releases](https://github.com/chillerlan/php-oauth/releases) for valid versions.


## Examples

There is [the suite of get-token examples](https://php-oauth.readthedocs.io/en/main/Usage/Using-examples.html),
which is mostly intended for development, and there are self-contained examples for a quickstart:

- [OAuth1 example](https://github.com/chillerlan/php-oauth/tree/main/examples/example-oauth1.php)
- [OAuth2 example](https://github.com/chillerlan/php-oauth/tree/main/examples/example-oauth2.php)



# Implemented Providers

<!-- TABLE-START -->
Expand Down
6 changes: 3 additions & 3 deletions src/Storage/OAuthStorageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
use Psr\Log\LoggerInterface;

/**
* Specifies the methods required for an OAuth token storage adapter
* Specifies the methods required for an OAuth storage adapter
*
* The token storage is intended to be invoked per-user,
* for whom it can store tokens for any of the implemented providers.
* The storage is intended to be invoked per-user, for whom it can
* store tokens, state etc. for any of the implemented providers.
*
* The implementer must ensure that the same storage instance is not used for multiple users.
*/
Expand Down
2 changes: 2 additions & 0 deletions tests/Attributes/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

/**
* Supplies the provider class name
*
* example: `#[Provider(GitHub::class)]`
*/
#[Attribute(Attribute::TARGET_CLASS)]
final class Provider{
Expand Down
6 changes: 3 additions & 3 deletions tests/Core/AuthenticatedUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use PHPUnit\Framework\TestCase;

/**
*
* Tests the AuthenticatedUser class
*/
final class AuthenticatedUserTest extends TestCase{

Expand Down Expand Up @@ -80,10 +80,10 @@ public static function displayNameProvider():array{
}

#[DataProvider('displayNameProvider')]
public function testSetDisplayName(string|null $displayName, string|null $expexted):void{
public function testSetDisplayName(string|null $displayName, string|null $expected):void{
$user = new AuthenticatedUser(['displayName' => $displayName]);

$this::assertSame($expexted, $user->displayName);
$this::assertSame($expected, $user->displayName);
}

}
2 changes: 1 addition & 1 deletion tests/Core/OAuthOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use PHPUnit\Framework\TestCase;

/**
*
* Tests the OAuthOptions class
*/
class OAuthOptionsTest extends TestCase{

Expand Down
2 changes: 1 addition & 1 deletion tests/Core/OAuthProviderFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use function realpath;

/**
*
* Tests the OAuthProviderFactory class
*/
class OAuthProviderFactoryTest extends TestCase{
use HttpFactoryTrait;
Expand Down
2 changes: 1 addition & 1 deletion tests/Core/UtilitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use InvalidArgumentException, ReflectionClass;

/**
*
* Tests the Utilities class
*/
class UtilitiesTest extends TestCase{

Expand Down
2 changes: 2 additions & 0 deletions tests/Providers/Live/OAuth1ProviderLiveTestAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace chillerlan\OAuthTest\Providers\Live;

/**
* OAuth1 live API test
*
* @property \chillerlan\OAuth\Core\OAuth1Interface $provider
*/
abstract class OAuth1ProviderLiveTestAbstract extends OAuthProviderLiveTestAbstract{
Expand Down
2 changes: 2 additions & 0 deletions tests/Providers/Live/OAuth2ProviderLiveTestAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use function time;

/**
* OAuth2 live API test
*
* @property \chillerlan\OAuth\Core\OAuth2Interface $provider
*/
abstract class OAuth2ProviderLiveTestAbstract extends OAuthProviderLiveTestAbstract{
Expand Down
2 changes: 2 additions & 0 deletions tests/Providers/Live/OAuthProviderLiveTestAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use function constant, sprintf;

/**
* abstract OAuth live API test
*
* @property \chillerlan\OAuth\Core\OAuthInterface $provider
*/
abstract class OAuthProviderLiveTestAbstract extends ProviderLiveTestAbstract{
Expand Down
6 changes: 4 additions & 2 deletions tests/Providers/ProviderLiveTestAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use function defined;

/**
*
* The abstract base class for all live API tests
*/
abstract class ProviderLiveTestAbstract extends ProviderUnitTestAbstract{

Expand All @@ -28,7 +28,9 @@ abstract class ProviderLiveTestAbstract extends ProviderUnitTestAbstract{
protected DotEnv $dotEnv;
protected string $ENV_PREFIX;

/** a test username for live API tests, defined in .env as {ENV-PREFIX}_TESTUSER*/
/**
* a test username for live API tests, defined in `.env` as `<$provider::IDENTIFIER>_TESTUSER`
*/
protected string $TEST_USER = '';

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/Providers/ProviderLiveTestHttpClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@
use function sprintf;

/**
* A PSR-18 HTTP client factory for clients used in live API tests
*
* The phpunit constant `HTTP_CLIENT_FACTORY` should be set with a valid FQCN for a `HttpClientFactoryInterface`
*
* @see \chillerlan\PHPUnitHttp\HttpClientFactoryInterface
* @link https://github.com/chillerlan/phpunit-http
*/
final class ProviderLiveTestHttpClientFactory implements HttpClientFactoryInterface{

Expand Down
2 changes: 1 addition & 1 deletion tests/Providers/ProviderTestLoggerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use const JSON_UNESCAPED_SLASHES;

/**
*
* A simple PSR-3 logger factory used in provider tests
*/
final class ProviderTestLoggerFactory{

Expand Down
29 changes: 25 additions & 4 deletions tests/Providers/ProviderUnitTestAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
use function sprintf;

/**
*
* The abstract base class for all provider tests
*/
abstract class ProviderUnitTestAbstract extends TestCase{
use HttpFactoryTrait;
Expand All @@ -57,6 +57,9 @@ abstract class ProviderUnitTestAbstract extends TestCase{
protected const CFGDIR = self::PROJECT_ROOT.'/.config';
protected const CACERT = self::PROJECT_ROOT.'/tests/cacert.pem';

/**
* Initializes the unit test
*/
protected function setUp():void{
ini_set('date.timezone', 'UTC');

Expand Down Expand Up @@ -93,6 +96,9 @@ protected function setUp():void{
* init dependencies
*/

/**
* Initializes the environment config (from `phpunit.xml`)
*/
protected function initConfig():void{

foreach(['TEST_ENVFILE'] as $constant){
Expand All @@ -103,6 +109,9 @@ protected function initConfig():void{

}

/**
* Initializes an `OAuthOptions` instance
*/
protected function initOptions():OAuthOptions{
$options = new OAuthOptions;

Expand All @@ -114,6 +123,9 @@ protected function initOptions():OAuthOptions{
return $options;
}

/**
* Initializes an `OAuthStorageInterface` instance
*/
protected function initStorage(OAuthOptions $options):OAuthStorageInterface{
return new MemoryStorage($options);
}
Expand All @@ -123,20 +135,29 @@ protected function initStorage(OAuthOptions $options):OAuthStorageInterface{
* Reflection utilities
*/

/**
* Sets a property in the current provider instance with the given value
*/
final protected function setReflectionProperty(string $property, mixed $value):void{
$this->reflection->getProperty($property)->setValue($this->provider, $value);
}

/**
* Returns the current value of the given propertyin the current provider instance
*/
final protected function getReflectionProperty(string $property):mixed{
return $this->reflection->getProperty($property)->getValue($this->provider);
}

/**
* Invokes a method vith the given arguments in the current provider instance
*/
final protected function invokeReflectionMethod(string $method, array $args = []):mixed{
return $this->reflection->getMethod($method)->invokeArgs($this->provider, $args);
}

/**
* returns the fully qualified class name (FQCN) of the test subject
* Returns the fully qualified class name (FQCN) of the test subject
*
* @see \chillerlan\OAuthTest\Attributes\Provider
*/
Expand All @@ -156,7 +177,7 @@ final protected function getProviderFQCN():string{
*/

/**
* creates a stupid simple ClientInterface that returns the given response instance
* Creates a stupid simple `ClientInterface` that returns the given response instance
*/
protected function getMockHttpClient(ResponseInterface $response):ClientInterface{
return new class ($response) implements ClientInterface{
Expand All @@ -174,7 +195,7 @@ public function sendRequest(RequestInterface $request):ResponseInterface{
}

/**
* sets a custom response in the mock http client and sets the client in the current provider
* Sets a custom response in the mock http client and sets the client in the current provider
*/
protected function setMockResponse(ResponseInterface|StreamInterface $response):void{

Expand Down
5 changes: 5 additions & 0 deletions tests/Providers/ProviderUnitTestHttpClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
use Psr\Http\Message\ResponseFactoryInterface;

/**
* A PSR-18 HTTP client factory for provider unit tests
*
* This factory just returns an echo client, that returns the given request
*
* @see \chillerlan\PHPUnitHttp\HttpClientFactoryInterface
* @link https://github.com/chillerlan/phpunit-http
*/
final class ProviderUnitTestHttpClientFactory implements HttpClientFactoryInterface{

Expand Down
2 changes: 2 additions & 0 deletions tests/Providers/Unit/OAuth1ProviderUnitTestAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use function str_starts_with;

/**
* OAuth1 unit test
*
* @property \chillerlan\OAuth\Core\OAuth1Interface $provider
*/
abstract class OAuth1ProviderUnitTestAbstract extends OAuthProviderUnitTestAbstract{
Expand Down
2 changes: 2 additions & 0 deletions tests/Providers/Unit/OAuth2ProviderUnitTestAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
use function base64_encode, implode, json_decode, json_encode;

/**
* OAuth2 unit test
*
* @property \chillerlan\OAuth\Core\OAuth2Interface $provider
*/
abstract class OAuth2ProviderUnitTestAbstract extends OAuthProviderUnitTestAbstract{
Expand Down
2 changes: 2 additions & 0 deletions tests/Providers/Unit/OAuthProviderUnitTestAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
use function sprintf;

/**
* abstract OAuth unit test
*
* @property \chillerlan\OAuth\Core\OAuthInterface $provider
*/
abstract class OAuthProviderUnitTestAbstract extends ProviderUnitTestAbstract{
Expand Down
2 changes: 1 addition & 1 deletion tests/Storage/FileStorageEncryptedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use chillerlan\OAuth\OAuthOptions;

/**
* Tests the file storage (encrypted)
* Tests the `FileStorage` class (encrypted)
*/
final class FileStorageEncryptedTest extends FileStorageTest{

Expand Down
2 changes: 1 addition & 1 deletion tests/Storage/FileStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use const DIRECTORY_SEPARATOR;

/**
* Tests the file storage
* Tests the `FileStorage` class
*/
class FileStorageTest extends StorageTestAbstract{

Expand Down
3 changes: 3 additions & 0 deletions tests/Storage/MemoryStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
use chillerlan\OAuth\OAuthOptions;
use chillerlan\OAuth\Storage\{MemoryStorage, OAuthStorageInterface};

/**
* Tests the `MemoryStorage` class
*/
final class MemoryStorageTest extends StorageTestAbstract{

protected function initStorage(OAuthOptions $options):OAuthStorageInterface{
Expand Down
2 changes: 1 addition & 1 deletion tests/Storage/SessionStorageEncryptedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use chillerlan\OAuth\OAuthOptions;

/**
* Tests the session storage (encrypted)
* Tests the `SessionStorage` class (encrypted)
*/
final class SessionStorageEncryptedTest extends SessionStorageTest{

Expand Down
2 changes: 1 addition & 1 deletion tests/Storage/SessionStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use chillerlan\OAuth\Storage\{OAuthStorageInterface, SessionStorage};

/**
* Tests the session storage
* Tests the `SessionStorage` class
*/
class SessionStorageTest extends StorageTestAbstract{

Expand Down
3 changes: 3 additions & 0 deletions tests/Storage/StorageTestAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
use ReflectionMethod;
use function array_merge;

/**
* The abstract storage test
*/
abstract class StorageTestAbstract extends TestCase{

protected const ENCRYPTION_KEY = '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f';
Expand Down

0 comments on commit c880698

Please sign in to comment.