Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Commit

Permalink
:octocat: +UnauthorizedAccessException, InvalidAccessTokenException
Browse files Browse the repository at this point in the history
  • Loading branch information
codemasher committed Mar 18, 2024
1 parent 86dbab2 commit 154eef3
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 20 deletions.
18 changes: 18 additions & 0 deletions src/Core/InvalidAccessTokenException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* Class InvalidAccessTokenException
*
* @created 18.03.2024
* @author smiley <[email protected]>
* @copyright 2024 smiley
* @license MIT
*/

namespace chillerlan\OAuth\Core;

/**
*
*/
class InvalidAccessTokenException extends UnauthorizedAccessException{

}
26 changes: 19 additions & 7 deletions src/Core/OAuthProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ protected function cleanBodyParams(iterable $params):array{

/**
* @inheritDoc
* @throws \chillerlan\OAuth\Core\UnauthorizedAccessException
*/
public function request(
string $path,
Expand Down Expand Up @@ -264,7 +265,14 @@ public function request(
$request = $request->withProtocolVersion($protocolVersion);
}

return $this->sendRequest($request);
$response = $this->sendRequest($request);

// we're throwing here immideately on unauthorized/forbidden
if(in_array($response->getStatusCode(), [401, 403], true)){
throw new UnauthorizedAccessException;
}

return $response;
}

/**
Expand Down Expand Up @@ -360,6 +368,7 @@ protected function getRequestTarget(string $uri):string{

/**
* @inheritDoc
* @throws \chillerlan\OAuth\Core\InvalidAccessTokenException
*/
public function sendRequest(RequestInterface $request):ResponseInterface{

Expand All @@ -368,12 +377,15 @@ public function sendRequest(RequestInterface $request):ResponseInterface{
$token = $this->storage->getAccessToken($this->serviceName);

// attempt to refresh an expired token
if(
$this instanceof TokenRefresh
&& $this->options->tokenAutoRefresh
&& ($token->isExpired() || $token->expires === $token::EOL_UNKNOWN)
){
$token = $this->refreshAccessToken($token);
if($token->isExpired() || $token->expires === $token::EOL_UNKNOWN){

if($this instanceof TokenRefresh && $this->options->tokenAutoRefresh){
$token = $this->refreshAccessToken($token);
}
else{
throw new InvalidAccessTokenException;
}

}

$request = $this->getRequestAuthorization($request, $token);
Expand Down
20 changes: 20 additions & 0 deletions src/Core/UnauthorizedAccessException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* Class UnauthorizedAccessException
*
* @created 18.03.2024
* @author smiley <[email protected]>
* @copyright 2024 smiley
* @license MIT
*/

namespace chillerlan\OAuth\Core;

use chillerlan\OAuth\OAuthException;

/**
*
*/
class UnauthorizedAccessException extends OAuthException{

}
9 changes: 7 additions & 2 deletions src/Providers/Amazon.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace chillerlan\OAuth\Providers;

use chillerlan\HTTP\Utils\MessageUtil;
use chillerlan\OAuth\Core\{CSRFToken, OAuth2Provider, TokenRefresh};
use chillerlan\OAuth\Core\{CSRFToken, InvalidAccessTokenException, OAuth2Provider, TokenRefresh};
use Psr\Http\Message\ResponseInterface;
use function sprintf;

Expand All @@ -35,7 +35,7 @@ class Amazon extends OAuth2Provider implements CSRFToken, TokenRefresh{
protected string $authURL = 'https://www.amazon.com/ap/oa';
protected string $accessTokenURL = 'https://www.amazon.com/ap/oatoken';
protected string $apiURL = 'https://api.amazon.com';
protected string|null $applicationURL = 'https://sellercentral.amazon.com/hz/home';
protected string|null $applicationURL = 'https://developer.amazon.com/loginwithamazon/console/site/lwa/overview.html';

/**
* @inheritDoc
Expand All @@ -51,6 +51,11 @@ public function me():ResponseInterface{
$json = MessageUtil::decodeJSON($response);

if(isset($json->error, $json->error_description)){

if($json->error === 'invalid_token'){
throw new InvalidAccessTokenException($json->error_description);
}

throw new ProviderException($json->error_description);
}

Expand Down
14 changes: 10 additions & 4 deletions src/Providers/BattleNet.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace chillerlan\OAuth\Providers;

use chillerlan\HTTP\Utils\MessageUtil;
use chillerlan\OAuth\Core\{ClientCredentials, CSRFToken, OAuth2Provider};
use chillerlan\OAuth\Core\{ClientCredentials, CSRFToken, InvalidAccessTokenException, OAuth2Provider};
use Psr\Http\Message\ResponseInterface;
use function in_array;
use function ltrim;
Expand Down Expand Up @@ -141,13 +141,19 @@ public function me():ResponseInterface{
return $response;
}

try{
$json = null;

// response may be html in some cases
if(str_contains($response->getHeaderLine('Content-Type'), 'application/json')){
$json = MessageUtil::decodeJSON($response);
}
catch(Throwable){
}

if(isset($json->error, $json->error_description)){

if($status === 401){
throw new InvalidAccessTokenException($json->error_description);
}

throw new ProviderException($json->error_description);
}

Expand Down
7 changes: 6 additions & 1 deletion src/Providers/Deezer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace chillerlan\OAuth\Providers;

use chillerlan\HTTP\Utils\{MessageUtil, QueryUtil};
use chillerlan\OAuth\Core\{AccessToken, CSRFToken, OAuth2Provider};
use chillerlan\OAuth\Core\{AccessToken, CSRFToken, InvalidAccessTokenException, OAuth2Provider};
use Psr\Http\Message\{ResponseInterface, UriInterface};
use function array_merge, implode, sprintf;
use const PHP_QUERY_RFC1738;
Expand Down Expand Up @@ -140,6 +140,11 @@ public function me():ResponseInterface{
}

if(isset($json->error)){

if($json->error->code === 300){
throw new InvalidAccessTokenException($json->error->message);
}

throw new ProviderException($json->error->message);
}

Expand Down
9 changes: 8 additions & 1 deletion src/Providers/Flickr.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace chillerlan\OAuth\Providers;

use chillerlan\HTTP\Utils\{MessageUtil, QueryUtil};
use chillerlan\OAuth\Core\{OAuth1Provider};
use chillerlan\OAuth\Core\{InvalidAccessTokenException, OAuth1Provider};
use Psr\Http\Message\{ResponseInterface, StreamInterface};
use function array_merge, sprintf;

Expand Down Expand Up @@ -63,6 +63,8 @@ public function request(
}

/**
* hi flickr, can i have a 401 on invalid token???
*
* @inheritDoc
*/
public function me():ResponseInterface{
Expand All @@ -75,6 +77,11 @@ public function me():ResponseInterface{
}

if(isset($json->message)){

if($json->message === 'Invalid auth token'){
throw new InvalidAccessTokenException($json->message);
}

throw new ProviderException($json->message);
}

Expand Down
12 changes: 10 additions & 2 deletions src/Providers/LastFM.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace chillerlan\OAuth\Providers;

use chillerlan\HTTP\Utils\{MessageUtil, QueryUtil};
use chillerlan\OAuth\Core\{AccessToken, OAuthProvider};
use chillerlan\OAuth\Core\{AccessToken, OAuthProvider, UnauthorizedAccessException};
use Psr\Http\Message\{RequestInterface, ResponseInterface, StreamInterface, UriInterface};
use Throwable;
use function array_merge, in_array, is_array, ksort, md5, sprintf, trigger_error;
Expand Down Expand Up @@ -172,6 +172,7 @@ public function request(
/** @phan-suppress-next-line PhanTypeMismatchArgumentNullable */
$request = $this->requestFactory->createRequest($method, QueryUtil::merge($this->apiURL, $params));

/** @noinspection PhpParamsInspection */
foreach(array_merge($this::HEADERS_API, ($headers ?? [])) as $header => $value){
$request = $request->withAddedHeader($header, $value);
}
Expand All @@ -182,7 +183,14 @@ public function request(
$request = $request->withBody($body);
}

return $this->http->sendRequest($request);
$response = $this->sendRequest($request);

// we're throwing here immideately on unauthorized/forbidden
if(in_array($response->getStatusCode(), [401, 403], true)){
throw new UnauthorizedAccessException;
}

return $response;
}

/**
Expand Down
8 changes: 7 additions & 1 deletion src/Providers/Mixcloud.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
namespace chillerlan\OAuth\Providers;

use chillerlan\HTTP\Utils\MessageUtil;
use chillerlan\OAuth\Core\{OAuth2Provider};
use chillerlan\OAuth\Core\{InvalidAccessTokenException, OAuth2Provider};
use Psr\Http\Message\ResponseInterface;
use function sprintf;
use function str_contains;

/**
* Mixcloud OAuth2
Expand Down Expand Up @@ -47,6 +48,11 @@ public function me():ResponseInterface{
$json = MessageUtil::decodeJSON($response);

if(isset($json->error, $json->error->message)){

if($status === 400 && str_contains($json->error->message, 'invalid access token')){
throw new InvalidAccessTokenException($json->error->message);
}

throw new ProviderException($json->error->message);
}

Expand Down
7 changes: 6 additions & 1 deletion src/Providers/Slack.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace chillerlan\OAuth\Providers;

use chillerlan\HTTP\Utils\MessageUtil;
use chillerlan\OAuth\Core\{CSRFToken, OAuth2Provider};
use chillerlan\OAuth\Core\{CSRFToken, InvalidAccessTokenException, OAuth2Provider};
use Psr\Http\Message\ResponseInterface;
use function sprintf;

Expand Down Expand Up @@ -107,6 +107,11 @@ public function me():ResponseInterface{
}

if(isset($json->error)){

if($json->error === 'invalid_auth'){
throw new InvalidAccessTokenException;
}

throw new ProviderException($json->error);
}

Expand Down
7 changes: 6 additions & 1 deletion src/Providers/WordPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace chillerlan\OAuth\Providers;

use chillerlan\HTTP\Utils\MessageUtil;
use chillerlan\OAuth\Core\{CSRFToken, OAuth2Provider};
use chillerlan\OAuth\Core\{CSRFToken, InvalidAccessTokenException, OAuth2Provider};
use Psr\Http\Message\ResponseInterface;
use function sprintf;

Expand Down Expand Up @@ -50,6 +50,11 @@ public function me():ResponseInterface{
$json = MessageUtil::decodeJSON($response);

if(isset($json->error, $json->message)){

if($status === 400 && $json->error === 'invalid_token'){
throw new InvalidAccessTokenException($json->message);
}

throw new ProviderException($json->message);
}

Expand Down

0 comments on commit 154eef3

Please sign in to comment.