diff --git a/config.ini.example b/config.ini.example index 27877231e6f6ed26a90df25da5f9925ab6461478..2494f64135e913871deef53ccf5b1fc2f49a5396 100644 --- a/config.ini.example +++ b/config.ini.example @@ -2,6 +2,7 @@ env = development esbuild_metafile = esbuild-meta-app.json scheme = https +url = https://resonance.distantmagic.com [database] default[driver] = mysql diff --git a/docs/pages/docs/features/security/oauth2/server/authorization-code-grant.md b/docs/pages/docs/features/security/oauth2/authorization-code-grant.md similarity index 96% rename from docs/pages/docs/features/security/oauth2/server/authorization-code-grant.md rename to docs/pages/docs/features/security/oauth2/authorization-code-grant.md index d1e8d84aa8866b8477fba6a16d386bbde883d330..f35543bfd0b8de4d9e87835911e09fec06c12d97 100644 --- a/docs/pages/docs/features/security/oauth2/server/authorization-code-grant.md +++ b/docs/pages/docs/features/security/oauth2/authorization-code-grant.md @@ -1,7 +1,9 @@ --- collections: - - documents + - name: documents + next: docs/features/security/oauth2/persistent-data layout: dm:document +next: docs/features/security/oauth2/persistent-data parent: docs/features/security/oauth2/index title: Authorization Code Grant description: > diff --git a/docs/pages/docs/features/security/oauth2/server/configuration.md b/docs/pages/docs/features/security/oauth2/configuration.md similarity index 100% rename from docs/pages/docs/features/security/oauth2/server/configuration.md rename to docs/pages/docs/features/security/oauth2/configuration.md diff --git a/docs/pages/docs/features/security/oauth2/enabling-grants.md b/docs/pages/docs/features/security/oauth2/enabling-grants.md new file mode 100644 index 0000000000000000000000000000000000000000..ceeaf29f17735d0e87160a39f353b7e94983660a --- /dev/null +++ b/docs/pages/docs/features/security/oauth2/enabling-grants.md @@ -0,0 +1,89 @@ +--- +collections: + - name: documents + next: docs/features/security/oauth2/authorization-code-grant +layout: dm:document +next: docs/features/security/oauth2/authorization-code-grant +parent: docs/features/security/oauth2/index +title: Enabling Grants +description: > + Learn how to add methods of acquiring access tokens. +--- + +# Enabling Grants + +Grant represents a method of obtaining an access token or, in other words, +different authentication flows (through password, token, etc.). + +By default, the OAuth2 server has no grants enabled, so you have to add at +least one if you want to use it. + +# Usage + +You can follow +[thephpleague/oauth2-server](https://oauth2.thephpleague.com/authorization-server/which-grant/) +recommendations to decide which grants you want to enable. You can either use +League's built-in grants or provide your own. + +## Doctrine Considerations + +If you want to implement repositories by using +{{docs/features/database/doctrine/index}}, you should probably use +`withRepository()` method to obtain the Entity Manager. For example: + +```php +<?php + +use Distantmagic\Resonance\DoctrineEntityManagerRepository; +use Doctrine\ORM\EntityRepository; + +#[Singleton(provides: AccessTokenRepositoryInterface::class)] +readonly class OAuth2AccessTokenRepository implements AccessTokenRepositoryInterface +{ + public function __construct(private DoctrineEntityManagerRepository $doctrineEntityManagerRepository) + { + } + + public function getNewToken(ClientEntityInterface $clientEntity, array $scopes, $userIdentifier = null) + { + $this + ->doctrineEntityManagerRepository + ->withRepository(MyDoctrineTokenRepository::class, function (EntityRepository $entityRepository) { + // ... + }) + ; + + // ... + } + + // ... +} +``` + +## Enabling Grant Types + +For each grant you want to enable you have to add a grant provider. For +example, if you want to enable client credentials grant: + +```php file:app/OAuth2GrantProvider/ClientCredentialsGrantProvider.php +<?php + +namespace App\OAuth2GrantProvider; + +use Distantmagic\Resonance\Attribute\ProvidesOAuth2Grant; +use Distantmagic\Resonance\Attribute\Singleton; +use Distantmagic\Resonance\OAuth2GrantProvider; +use Distantmagic\Resonance\SingletonCollection; +use League\OAuth2\Server\Grant\ClientCredentialsGrant; +use League\OAuth2\Server\Grant\GrantTypeInterface; + +#[ProvidesOAuth2Grant] +#[Singleton(collection: SingletonCollection::OAuth2Grant)] +readonly class ClientCredentialsGrantProvider extends OAuth2GrantProvider +{ + public function getGrant(): GrantTypeInterface + { + return new ClientCredentialsGrant(); + } +} +``` diff --git a/docs/pages/docs/features/security/oauth2/server/installation.md b/docs/pages/docs/features/security/oauth2/installation.md similarity index 100% rename from docs/pages/docs/features/security/oauth2/server/installation.md rename to docs/pages/docs/features/security/oauth2/installation.md diff --git a/docs/pages/docs/features/security/oauth2/server/enabling-grants.md b/docs/pages/docs/features/security/oauth2/persistent-data.md similarity index 73% rename from docs/pages/docs/features/security/oauth2/server/enabling-grants.md rename to docs/pages/docs/features/security/oauth2/persistent-data.md index e589a6c4a834cb06a0c373e9f78b592edfd2e955..a9e92802c68339402c33c2b22c5b4fc02366bf62 100644 --- a/docs/pages/docs/features/security/oauth2/server/enabling-grants.md +++ b/docs/pages/docs/features/security/oauth2/persistent-data.md @@ -1,22 +1,14 @@ --- collections: - - name: documents - next: docs/features/security/oauth2/authorization-code-grant + - documents layout: dm:document -next: docs/features/security/oauth2/authorization-code-grant parent: docs/features/security/oauth2/index -title: Enabling Grants +title: Persistent Data description: > - Learn how to add methods of acquiring access tokens. + Learn how to persist OAuth2 tokens and other data by using Doctrine. --- -# Enabling Grants - -Grant represents a method of obtaining an access token or, in other words, -different authentication flows (through password, token, etc.). - -By default, the OAuth2 server has no grants enabled, so you have to add at -least one if you want to use it. +# Persistent Data You can implement only the ones you need in your application. Grants use repositories to store and retrieve the data they need to operate. @@ -33,76 +25,6 @@ additional step compared to other grant types (exchanging code for an access token instead of immediately generating access token), thus it has it's own documentation page that explains the process further. -# Doctrine Considerations - -If you want to implement repositories by using -{{docs/features/database/doctrine/index}}, you should probably use -`withRepository()` method to obtain the Entity Manager. For example: - -```php -<?php - -use Distantmagic\Resonance\DoctrineEntityManagerRepository; -use Doctrine\ORM\EntityRepository; - -#[Singleton(provides: AccessTokenRepositoryInterface::class)] -readonly class OAuth2AccessTokenRepository implements AccessTokenRepositoryInterface -{ - public function __construct(private DoctrineEntityManagerRepository $doctrineEntityManagerRepository) - { - } - - public function getNewToken(ClientEntityInterface $clientEntity, array $scopes, $userIdentifier = null) - { - $this - ->doctrineEntityManagerRepository - ->withRepository(MyDoctrineTokenRepository::class, function (EntityRepository $entityRepository) { - // ... - }) - ; - - // ... - } - - // ... -} -``` - -# Usage - -You can follow -[thephpleague/oauth2-server](https://oauth2.thephpleague.com/authorization-server/which-grant/) -recommendations to decide which grants you want to enable. You can either use -League's built-in grants or provide your own. - -## Enabling Grant Types - -For each grant you want to enable you have to add a grant provider. For -example, if you want to enable client credentials grant: - -```php file:app/OAuth2GrantProvider/ClientCredentialsGrantProvider.php -<?php - -namespace App\OAuth2GrantProvider; - -use Distantmagic\Resonance\Attribute\ProvidesOAuth2Grant; -use Distantmagic\Resonance\Attribute\Singleton; -use Distantmagic\Resonance\OAuth2GrantProvider; -use Distantmagic\Resonance\SingletonCollection; -use League\OAuth2\Server\Grant\ClientCredentialsGrant; -use League\OAuth2\Server\Grant\GrantTypeInterface; - -#[ProvidesOAuth2Grant] -#[Singleton(collection: SingletonCollection::OAuth2Grant)] -readonly class ClientCredentialsGrantProvider extends OAuth2GrantProvider -{ - public function getGrant(): GrantTypeInterface - { - return new ClientCredentialsGrant(); - } -} -``` - ## Persistent Data Repositories You can learn more on {{docs/features/database/doctrine/entity-managers}} page. diff --git a/src/Command/StaticPagesBuild.php b/src/Command/StaticPagesBuild.php index d6a5b22d571b46b4cb38b2add87181cf82c70c15..efaffa28ca62df176f30654071dcecb8b4bc9592 100644 --- a/src/Command/StaticPagesBuild.php +++ b/src/Command/StaticPagesBuild.php @@ -24,7 +24,7 @@ final class StaticPagesBuild extends CoroutineCommand protected function executeInCoroutine(InputInterface $input, OutputInterface $output): int { - $this->staticPageProcessor->process(...); + $this->staticPageProcessor->process(); return Command::SUCCESS; } diff --git a/src/DatabaseConnectionPoolConfiguration.php b/src/DatabaseConnectionPoolConfiguration.php index 6564d40c28c91d236a519b1dd406d80b380e9a4f..4eed44a007b25ff85a0ec191936bd113f79d1bf9 100644 --- a/src/DatabaseConnectionPoolConfiguration.php +++ b/src/DatabaseConnectionPoolConfiguration.php @@ -26,7 +26,7 @@ readonly class DatabaseConnectionPoolConfiguration #[SensitiveParameter] public int $port, #[SensitiveParameter] - public string $unixSocket, + public ?string $unixSocket, #[SensitiveParameter] public string $username, ) {} diff --git a/src/SingletonProvider/ConfigurationProvider/DatabaseConfigurationProvider.php b/src/SingletonProvider/ConfigurationProvider/DatabaseConfigurationProvider.php index 2cf098b547cf8b8b60938b4871dffdf95926d65f..abbed870b45af0404aeee8cf3f6360467c40d3cc 100644 --- a/src/SingletonProvider/ConfigurationProvider/DatabaseConfigurationProvider.php +++ b/src/SingletonProvider/ConfigurationProvider/DatabaseConfigurationProvider.php @@ -50,7 +50,7 @@ final readonly class DatabaseConfigurationProvider extends ConfigurationProvider 'pool_prefill' => Expect::bool()->required(), 'pool_size' => Expect::int()->min(1)->required(), 'port' => Expect::int()->min(1)->max(65535)->default(3306), - 'unix_socket' => Expect::string()->min(1)->nullable()->default(null), + 'unix_socket' => Expect::string()->nullable()->default(null), 'username' => Expect::string()->min(1)->required(), ]);