|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Pdsinterop\Solid\Auth\Factory; |
| 4 | + |
| 5 | +use DateInterval; |
| 6 | +use Exception; |
| 7 | +use InvalidArgumentException; |
| 8 | +use League\OAuth2\Server\Grant\AuthCodeGrant; |
| 9 | +use League\OAuth2\Server\Grant\ClientCredentialsGrant; |
| 10 | +use League\OAuth2\Server\Grant\GrantTypeInterface; |
| 11 | +use League\OAuth2\Server\Grant\RefreshTokenGrant; |
| 12 | +use Pdsinterop\Solid\Auth\Config\Expiration; |
| 13 | +use Pdsinterop\Solid\Auth\Enum\OAuth2\GrantType; |
| 14 | + |
| 15 | +class GrantTypeFactory |
| 16 | +{ |
| 17 | + ////////////////////////////// CLASS PROPERTIES \\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
| 18 | + |
| 19 | + /** @var Expiration */ |
| 20 | + private $expiration; |
| 21 | + /** @var RepositoryFactory */ |
| 22 | + private $repositoryFactory; |
| 23 | + |
| 24 | + //////////////////////////////// PUBLIC API \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
| 25 | + |
| 26 | + final public function __construct(Expiration $expiration, RepositoryFactory $repositoryFactory) |
| 27 | + { |
| 28 | + $this->expiration = $expiration; |
| 29 | + $this->repositoryFactory = $repositoryFactory; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @param string $grantType |
| 34 | + * |
| 35 | + * @return GrantTypeInterface |
| 36 | + * |
| 37 | + * @throws Exception |
| 38 | + */ |
| 39 | + final public function createGrantType(string $grantType) : GrantTypeInterface |
| 40 | + { |
| 41 | + $expiration = $this->expiration; |
| 42 | + $factory = $this->repositoryFactory; |
| 43 | + |
| 44 | + switch ($grantType) { |
| 45 | + case GrantType::AUTH_CODE: |
| 46 | + $grant = $this->createAuthCodeGrant($factory, $expiration->forAuthCode()); |
| 47 | + $grant->setRefreshTokenTTL($expiration->forRefreshToken()); |
| 48 | + break; |
| 49 | + |
| 50 | + |
| 51 | + case GrantType::CLIENT_CREDENTIALS: |
| 52 | + $grant = $this->createClientCredentialsGrant(); |
| 53 | + break; |
| 54 | + |
| 55 | + case GrantType::REFRESH_TOKEN: |
| 56 | + $grant = $this->createRefreshTokenGrant($factory); |
| 57 | + break; |
| 58 | + |
| 59 | + default: |
| 60 | + throw new InvalidArgumentException('Given grant type "' . $grantType . '"is not supported'); |
| 61 | + break; |
| 62 | + } |
| 63 | + |
| 64 | + return $grant; |
| 65 | + } |
| 66 | + |
| 67 | + ////////////////////////////// UTILITY METHODS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
| 68 | + |
| 69 | + private function createClientCredentialsGrant() : ClientCredentialsGrant |
| 70 | + { |
| 71 | + return new ClientCredentialsGrant(); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @param RepositoryFactory $factory |
| 76 | + * @param DateInterval $expiration |
| 77 | + * |
| 78 | + * @return AuthCodeGrant |
| 79 | + * |
| 80 | + * @throws Exception |
| 81 | + */ |
| 82 | + private function createAuthCodeGrant(RepositoryFactory $factory, DateInterval $expiration) : AuthCodeGrant |
| 83 | + { |
| 84 | + return new AuthCodeGrant( |
| 85 | + $factory->createAuthCodeRepository(), |
| 86 | + $factory->createRefreshTokenRepository(), |
| 87 | + $expiration |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + private function createRefreshTokenGrant(RepositoryFactory $factory) : RefreshTokenGrant |
| 92 | + { |
| 93 | + return new RefreshTokenGrant( |
| 94 | + $factory->createRefreshTokenRepository() |
| 95 | + ); |
| 96 | + } |
| 97 | +} |
0 commit comments