mirror of
https://github.com/bitinflow/accounts.git
synced 2026-03-13 13:35:52 +00:00
Add app token repository
This commit is contained in:
@@ -13,7 +13,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"php": "^7.2|^8.0",
|
"php": "^7.4|^8.0",
|
||||||
"ext-json": "*",
|
"ext-json": "*",
|
||||||
"illuminate/support": "~5.4|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0",
|
"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",
|
"illuminate/console": "~5.4|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0",
|
||||||
|
|||||||
15
src/Accounts/Contracts/AppTokenRepository.php
Normal file
15
src/Accounts/Contracts/AppTokenRepository.php
Normal 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;
|
||||||
|
}
|
||||||
27
src/Accounts/Exceptions/RequestFreshAccessTokenException.php
Normal file
27
src/Accounts/Exceptions/RequestFreshAccessTokenException.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,6 +8,8 @@ use Bitinflow\Accounts\Auth\TokenGuard;
|
|||||||
use Bitinflow\Accounts\Auth\UserProvider;
|
use Bitinflow\Accounts\Auth\UserProvider;
|
||||||
use Bitinflow\Accounts\BitinflowAccounts;
|
use Bitinflow\Accounts\BitinflowAccounts;
|
||||||
use Bitinflow\Accounts\Helpers\JwtParser;
|
use Bitinflow\Accounts\Helpers\JwtParser;
|
||||||
|
use Bitinflow\Accounts\Contracts;
|
||||||
|
use Bitinflow\Accounts\Repository;
|
||||||
use Illuminate\Auth\RequestGuard;
|
use Illuminate\Auth\RequestGuard;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
@@ -35,6 +37,7 @@ class BitinflowAccountsServiceProvider extends ServiceProvider
|
|||||||
public function register()
|
public function register()
|
||||||
{
|
{
|
||||||
$this->mergeConfigFrom(dirname(__DIR__, 3) . '/config/bitinflow-accounts.php', 'bitinflow-accounts');
|
$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 () {
|
$this->app->singleton(BitinflowAccounts::class, function () {
|
||||||
return new BitinflowAccounts;
|
return new BitinflowAccounts;
|
||||||
});
|
});
|
||||||
|
|||||||
61
src/Accounts/Repository/AppTokenRepository.php
Normal file
61
src/Accounts/Repository/AppTokenRepository.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,7 +17,7 @@ class Result
|
|||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Query successfull.
|
* Query successful.
|
||||||
*
|
*
|
||||||
* @var boolean
|
* @var boolean
|
||||||
*/
|
*/
|
||||||
@@ -111,7 +111,7 @@ class Result
|
|||||||
private function setProperty(stdClass $jsonResponse, string $responseProperty, string $attribute = null): void
|
private function setProperty(stdClass $jsonResponse, string $responseProperty, string $attribute = null): void
|
||||||
{
|
{
|
||||||
$classAttribute = $attribute ?? $responseProperty;
|
$classAttribute = $attribute ?? $responseProperty;
|
||||||
if ($jsonResponse !== null && property_exists($jsonResponse, $responseProperty)) {
|
if (property_exists($jsonResponse, $responseProperty)) {
|
||||||
$this->{$classAttribute} = $jsonResponse->{$responseProperty};
|
$this->{$classAttribute} = $jsonResponse->{$responseProperty};
|
||||||
} elseif ($responseProperty === 'data') {
|
} elseif ($responseProperty === 'data') {
|
||||||
$this->{$classAttribute} = $jsonResponse;
|
$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
|
* @return bool Success state
|
||||||
*/
|
*/
|
||||||
@@ -186,8 +186,6 @@ class Result
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the Paginator to fetch the next set of results.
|
* Set the Paginator to fetch the next set of results.
|
||||||
*
|
|
||||||
* @return null|Paginator
|
|
||||||
*/
|
*/
|
||||||
public function next(): ?Paginator
|
public function next(): ?Paginator
|
||||||
{
|
{
|
||||||
@@ -196,8 +194,6 @@ class Result
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the Paginator to fetch the last set of results.
|
* Set the Paginator to fetch the last set of results.
|
||||||
*
|
|
||||||
* @return null|Paginator
|
|
||||||
*/
|
*/
|
||||||
public function back(): ?Paginator
|
public function back(): ?Paginator
|
||||||
{
|
{
|
||||||
@@ -233,8 +229,6 @@ class Result
|
|||||||
*
|
*
|
||||||
* @param string $identifierAttribute Attribute to identify the users
|
* @param string $identifierAttribute Attribute to identify the users
|
||||||
* @param string $insertTo Data index to insert user data
|
* @param string $insertTo Data index to insert user data
|
||||||
*
|
|
||||||
* @return self
|
|
||||||
*/
|
*/
|
||||||
public function insertUsers(string $identifierAttribute = 'user_id', string $insertTo = 'user'): 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.
|
* Set the Paginator to fetch the first set of results.
|
||||||
*
|
|
||||||
* @return null|Paginator
|
|
||||||
*/
|
*/
|
||||||
public function first(): ?Paginator
|
public function first(): ?Paginator
|
||||||
{
|
{
|
||||||
return $this->paginator !== null ? $this->paginator->first() : null;
|
return $this->paginator !== null ? $this->paginator->first() : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function response(): ?ResponseInterface
|
||||||
|
{
|
||||||
|
return $this->response;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user