Skip to content

Commit

Permalink
Server exception when refreshing token (#31)
Browse files Browse the repository at this point in the history
* new: throw an exception if server responds with > 500 when authenticating

* new: use a dedicated exception for when authentication fails with 5xx error

Co-authored-by: Lauris Stekels <[email protected]>
  • Loading branch information
l-stekels and Lauris Stekels authored Mar 31, 2022
1 parent aa02804 commit c688ac2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Exception/Domain/AuthorizationServerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace HappyrMatch\ApiClient\Exception\Domain;

use HappyrMatch\ApiClient\Exception\DomainException;

class AuthorizationServerException extends \Exception implements DomainException
{
}
4 changes: 4 additions & 0 deletions src/Http/AuthenticationPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace HappyrMatch\ApiClient\Http;

use HappyrMatch\ApiClient\Exception\Domain\AuthorizationServerException;
use Http\Client\Common\Plugin;
use Http\Promise\Promise;
use Psr\Http\Message\RequestInterface;
Expand Down Expand Up @@ -41,6 +42,9 @@ public function __construct(Authenticator $authenticator, string $accessToken)
$this->accessToken = \json_decode($accessToken, true);
}

/**
* @throws AuthorizationServerException
*/
public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise
{
if (null === $this->accessToken || $request->hasHeader('Authorization')) {
Expand Down
7 changes: 7 additions & 0 deletions src/Http/Authenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace HappyrMatch\ApiClient\Http;

use HappyrMatch\ApiClient\Exception\Domain\AuthorizationServerException;
use HappyrMatch\ApiClient\RequestBuilder;
use Http\Client\HttpClient;

Expand Down Expand Up @@ -71,6 +72,9 @@ public function createAccessToken(string $code, string $redirectUri): ?string
return $this->accessToken;
}

/**
* @throws AuthorizationServerException
*/
public function refreshAccessToken(string $accessToken, string $refreshToken): ?string
{
$request = $this->requestBuilder->create('POST', '/oauth/token', [
Expand All @@ -84,6 +88,9 @@ public function refreshAccessToken(string $accessToken, string $refreshToken): ?
]));

$response = $this->httpClient->sendRequest($request);
if ($response->getStatusCode() >= 500) {
throw new AuthorizationServerException();
}
if (200 !== $response->getStatusCode()) {
return null;
}
Expand Down

0 comments on commit c688ac2

Please sign in to comment.