optimisation appel pdo, retry consent
Some checks failed
Cadoles/hydra-sql/pipeline/pr-develop There was a failure building this commit
Some checks failed
Cadoles/hydra-sql/pipeline/pr-develop There was a failure building this commit
This commit is contained in:
@ -3,14 +3,20 @@
|
||||
namespace App\Hydra;
|
||||
|
||||
use App\Hydra\Exception\InvalidChallengeException;
|
||||
use Exception;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
use Symfony\Contracts\HttpClient\ResponseInterface;
|
||||
|
||||
class Client
|
||||
{
|
||||
protected $client;
|
||||
|
||||
protected $hydraAdminBaseUrl;
|
||||
private HttpClientInterface $client;
|
||||
private const MAX_RETRY = 3;
|
||||
private const SLEEP_TIME = [
|
||||
5,
|
||||
500,
|
||||
5000,
|
||||
];
|
||||
private string $hydraAdminBaseUrl;
|
||||
|
||||
public function __construct(HttpClientInterface $client, string $hydraAdminBaseUrl)
|
||||
{
|
||||
@ -22,11 +28,11 @@ class Client
|
||||
{
|
||||
$response = $this->client->request(
|
||||
'GET',
|
||||
$this->hydraAdminBaseUrl . '/oauth2/auth/requests/login',
|
||||
$this->hydraAdminBaseUrl.'/oauth2/auth/requests/login',
|
||||
[
|
||||
'query' => [
|
||||
'login_challenge' => $loginChallenge,
|
||||
]
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
@ -35,7 +41,6 @@ class Client
|
||||
throw new InvalidChallengeException();
|
||||
}
|
||||
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
@ -43,11 +48,11 @@ class Client
|
||||
{
|
||||
$response = $this->client->request(
|
||||
'GET',
|
||||
$this->hydraAdminBaseUrl . '/oauth2/auth/requests/logout',
|
||||
$this->hydraAdminBaseUrl.'/oauth2/auth/requests/logout',
|
||||
[
|
||||
'query' => [
|
||||
'logout_challenge' => $logoutChallenge,
|
||||
]
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
@ -56,27 +61,38 @@ class Client
|
||||
throw new InvalidChallengeException();
|
||||
}
|
||||
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function fetchConsentRequestInfo(string $consentChallenge): ResponseInterface
|
||||
{
|
||||
$response = $this->client->request(
|
||||
'GET',
|
||||
$this->hydraAdminBaseUrl . '/oauth2/auth/requests/consent',
|
||||
[
|
||||
'query' => [
|
||||
'consent_challenge' => $consentChallenge,
|
||||
$attempt = 0;
|
||||
while ($attempt < self::MAX_RETRY) {
|
||||
$response = $this->client->request(
|
||||
'GET',
|
||||
$this->hydraAdminBaseUrl.'/oauth2/auth/requests/consent',
|
||||
[
|
||||
'query' => [
|
||||
'consent_challenge' => $consentChallenge,
|
||||
],
|
||||
]
|
||||
]
|
||||
);
|
||||
);
|
||||
|
||||
switch ($response->getStatusCode()) {
|
||||
case 404:
|
||||
throw new InvalidChallengeException();
|
||||
$status = $response->getStatusCode();
|
||||
if (503 === $status) {
|
||||
++$attempt;
|
||||
usleep(1000 * self::SLEEP_TIME[$attempt] + rand(1, 5) * 1000);
|
||||
continue;
|
||||
}
|
||||
switch ($status) {
|
||||
case 404:
|
||||
throw new InvalidChallengeException();
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (self::MAX_RETRY === $attempt) {
|
||||
throw new Exception(sprintf('Fetch consent a rencontré une erreur %s après %s tentatives', $response->getStatusCode(), self::MAX_RETRY));
|
||||
}
|
||||
|
||||
|
||||
return $response;
|
||||
}
|
||||
@ -85,18 +101,18 @@ class Client
|
||||
{
|
||||
$response = $this->client->request(
|
||||
'PUT',
|
||||
$this->hydraAdminBaseUrl . '/oauth2/auth/requests/login/accept',
|
||||
$this->hydraAdminBaseUrl.'/oauth2/auth/requests/login/accept',
|
||||
[
|
||||
'query' => [
|
||||
'login_challenge' => $loginChallenge,
|
||||
],
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/json'
|
||||
'Content-Type' => 'application/json',
|
||||
],
|
||||
'body' => json_encode($payload),
|
||||
]
|
||||
);
|
||||
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
@ -104,13 +120,13 @@ class Client
|
||||
{
|
||||
$response = $this->client->request(
|
||||
'PUT',
|
||||
$this->hydraAdminBaseUrl . '/oauth2/auth/requests/consent/accept',
|
||||
$this->hydraAdminBaseUrl.'/oauth2/auth/requests/consent/accept',
|
||||
[
|
||||
'query' => [
|
||||
'consent_challenge' => $consentChallenge,
|
||||
],
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/json'
|
||||
'Content-Type' => 'application/json',
|
||||
],
|
||||
'body' => json_encode($payload),
|
||||
]
|
||||
@ -123,13 +139,13 @@ class Client
|
||||
{
|
||||
$response = $this->client->request(
|
||||
'PUT',
|
||||
$this->hydraAdminBaseUrl . '/oauth2/auth/requests/logout/accept',
|
||||
$this->hydraAdminBaseUrl.'/oauth2/auth/requests/logout/accept',
|
||||
[
|
||||
'query' => [
|
||||
'logout_challenge' => $logoutChallenge,
|
||||
],
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/json'
|
||||
'Content-Type' => 'application/json',
|
||||
],
|
||||
]
|
||||
);
|
||||
|
Reference in New Issue
Block a user