Add app token repository

This commit is contained in:
René Preuß
2022-10-01 12:09:36 +02:00
parent 00f30e097d
commit 7ba3cdffb0
6 changed files with 115 additions and 12 deletions

View File

@@ -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",

View File

@@ -0,0 +1,15 @@
<?php
namespace Bitinflow\Accounts\Contracts;
use Bitinflow\Accounts\Exceptions\RequestFreshAccessTokenException;
interface AppTokenRepository
{
/**
* @throws RequestFreshAccessTokenException
*
* @return string
*/
public function getAccessToken(): string;
}

View File

@@ -0,0 +1,27 @@
<?php
namespace Bitinflow\Accounts\Exceptions;
use DomainException;
use Psr\Http\Message\ResponseInterface;
/**
* @author René Preuß <rene@bitinflow.com>
*/
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;
}
}

View File

@@ -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;
});

View File

@@ -0,0 +1,61 @@
<?php
namespace Bitinflow\Accounts\Repository;
use Bitinflow\Accounts\BitinflowAccounts;
use Bitinflow\Accounts\Contracts\AppTokenRepository as Repository;
use Bitinflow\Accounts\Exceptions\RequestFreshAccessTokenException;
use Illuminate\Support\Facades\Cache;
class AppTokenRepository implements Repository
{
public const ACCESS_TOKEN_CACHE_KEY = 'bitinflow-accounts:access_token';
private BitinflowAccounts $client;
public function __construct()
{
$this->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;
}
}

View File

@@ -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;
}
}