From 7ba3cdffb069f6ca69e430d68cea7237b8f27959 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Preu=C3=9F?= Date: Sat, 1 Oct 2022 12:09:36 +0200 Subject: [PATCH] Add app token repository --- composer.json | 2 +- src/Accounts/Contracts/AppTokenRepository.php | 15 +++++ .../RequestFreshAccessTokenException.php | 27 ++++++++ .../BitinflowAccountsServiceProvider.php | 3 + .../Repository/AppTokenRepository.php | 61 +++++++++++++++++++ src/Accounts/Result.php | 19 +++--- 6 files changed, 115 insertions(+), 12 deletions(-) create mode 100644 src/Accounts/Contracts/AppTokenRepository.php create mode 100644 src/Accounts/Exceptions/RequestFreshAccessTokenException.php create mode 100644 src/Accounts/Repository/AppTokenRepository.php diff --git a/composer.json b/composer.json index ee41380..7d0e926 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ } ], "require": { - "php": "^7.2|^8.0", + "php": "^7.4|^8.0", "ext-json": "*", "illuminate/support": "~5.4|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0", "illuminate/console": "~5.4|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0", diff --git a/src/Accounts/Contracts/AppTokenRepository.php b/src/Accounts/Contracts/AppTokenRepository.php new file mode 100644 index 0000000..3d1a0fd --- /dev/null +++ b/src/Accounts/Contracts/AppTokenRepository.php @@ -0,0 +1,15 @@ + + */ +class RequestFreshAccessTokenException extends DomainException +{ + private ResponseInterface $response; + + public static function fromResponse(ResponseInterface $response): self + { + $instance = new self(sprintf('Refresh token request from bitinflow accounts failed. Status Code is %s.', $response->getStatusCode())); + $instance->response = $response; + + return $instance; + } + + public function getResponse(): ResponseInterface + { + return $this->response; + } +} \ No newline at end of file diff --git a/src/Accounts/Providers/BitinflowAccountsServiceProvider.php b/src/Accounts/Providers/BitinflowAccountsServiceProvider.php index c8fc70a..aae0cda 100644 --- a/src/Accounts/Providers/BitinflowAccountsServiceProvider.php +++ b/src/Accounts/Providers/BitinflowAccountsServiceProvider.php @@ -8,6 +8,8 @@ use Bitinflow\Accounts\Auth\TokenGuard; use Bitinflow\Accounts\Auth\UserProvider; use Bitinflow\Accounts\BitinflowAccounts; use Bitinflow\Accounts\Helpers\JwtParser; +use Bitinflow\Accounts\Contracts; +use Bitinflow\Accounts\Repository; use Illuminate\Auth\RequestGuard; use Illuminate\Support\Facades\Auth; use Illuminate\Support\ServiceProvider; @@ -35,6 +37,7 @@ class BitinflowAccountsServiceProvider extends ServiceProvider public function register() { $this->mergeConfigFrom(dirname(__DIR__, 3) . '/config/bitinflow-accounts.php', 'bitinflow-accounts'); + $this->app->singleton(Contracts\AppTokenRepository::class, Repository\AppTokenRepository::class); $this->app->singleton(BitinflowAccounts::class, function () { return new BitinflowAccounts; }); diff --git a/src/Accounts/Repository/AppTokenRepository.php b/src/Accounts/Repository/AppTokenRepository.php new file mode 100644 index 0000000..a46c695 --- /dev/null +++ b/src/Accounts/Repository/AppTokenRepository.php @@ -0,0 +1,61 @@ +client = app(BitinflowAccounts::class); + } + + /** + * {@inheritDoc} + */ + public function getAccessToken(): string + { + $accessToken = Cache::get(self::ACCESS_TOKEN_CACHE_KEY); + + if ($accessToken) { + return $accessToken; + } + + return $this->requestFreshAccessToken('*'); + } + + /** + * @param string $scope + * + * @throws RequestFreshAccessTokenException + * + * @return mixed + */ + private function requestFreshAccessToken(string $scope) + { + $result = $this->getClient()->retrievingToken('client_credentials', [ + 'scope' => $scope, + ]); + + if ( ! $result->success()) { + throw RequestFreshAccessTokenException::fromResponse($result->response()); + } + + Cache::put(self::ACCESS_TOKEN_CACHE_KEY, $accessToken = $result->data()->access_token, now()->addWeek()); + + return $accessToken; + } + + private function getClient(): BitinflowAccounts + { + return $this->client; + } +} \ No newline at end of file diff --git a/src/Accounts/Result.php b/src/Accounts/Result.php index d1951e2..3095702 100644 --- a/src/Accounts/Result.php +++ b/src/Accounts/Result.php @@ -17,7 +17,7 @@ class Result { /** - * Query successfull. + * Query successful. * * @var boolean */ @@ -111,7 +111,7 @@ class Result private function setProperty(stdClass $jsonResponse, string $responseProperty, string $attribute = null): void { $classAttribute = $attribute ?? $responseProperty; - if ($jsonResponse !== null && property_exists($jsonResponse, $responseProperty)) { + if (property_exists($jsonResponse, $responseProperty)) { $this->{$classAttribute} = $jsonResponse->{$responseProperty}; } elseif ($responseProperty === 'data') { $this->{$classAttribute} = $jsonResponse; @@ -119,7 +119,7 @@ class Result } /** - * Returns wether the query was successfull. + * Returns whether the query was successfully. * * @return bool Success state */ @@ -186,8 +186,6 @@ class Result /** * Set the Paginator to fetch the next set of results. - * - * @return null|Paginator */ public function next(): ?Paginator { @@ -196,8 +194,6 @@ class Result /** * Set the Paginator to fetch the last set of results. - * - * @return null|Paginator */ public function back(): ?Paginator { @@ -233,8 +229,6 @@ class Result * * @param string $identifierAttribute Attribute to identify the users * @param string $insertTo Data index to insert user data - * - * @return self */ public function insertUsers(string $identifierAttribute = 'user_id', string $insertTo = 'user'): self { @@ -258,11 +252,14 @@ class Result /** * Set the Paginator to fetch the first set of results. - * - * @return null|Paginator */ public function first(): ?Paginator { return $this->paginator !== null ? $this->paginator->first() : null; } + + public function response(): ?ResponseInterface + { + return $this->response; + } } \ No newline at end of file