handle and response on OAuth Server Exception

This commit is contained in:
Artem Vasilev 2024-03-17 21:42:00 +03:00
parent 712e628755
commit 5e8ccd7c66

View File

@ -10,8 +10,10 @@
namespace Webmasterskaya\Component\OauthServer\Site\Controller; namespace Webmasterskaya\Component\OauthServer\Site\Controller;
use Joomla\CMS\Application\CMSApplication; use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\Application\SiteApplication;
use Joomla\CMS\Component\ComponentHelper; use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory; use Joomla\CMS\Factory;
use Joomla\CMS\Log\Log;
use Joomla\CMS\MVC\Controller\BaseController; use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface; use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\CMS\Router\Route; use Joomla\CMS\Router\Route;
@ -274,6 +276,8 @@ class LoginController extends BaseController
// Clean user state after login checks // Clean user state after login checks
$app->setUserState($state_prefix, null); $app->setUserState($state_prefix, null);
try
{
$server = $this->authorizationServer; $server = $this->authorizationServer;
// Validate the HTTP request and return an AuthorizationRequest object. // Validate the HTTP request and return an AuthorizationRequest object.
@ -293,6 +297,13 @@ class LoginController extends BaseController
$authRequest->setAuthorizationApproved(true); $authRequest->setAuthorizationApproved(true);
$app->setResponse($server->completeAuthorizationRequest($authRequest, $app->getResponse())); $app->setResponse($server->completeAuthorizationRequest($authRequest, $app->getResponse()));
}
catch (OAuthServerException $e)
{
$this->handleOAuthServerException($e);
}
return $this; return $this;
} }
@ -306,6 +317,9 @@ class LoginController extends BaseController
{ {
$server = $this->authorizationServer; $server = $this->authorizationServer;
$serverRequest = ServerRequestFactory::fromGlobals(); $serverRequest = ServerRequestFactory::fromGlobals();
try
{
$response = $this->app->getResponse(); $response = $this->app->getResponse();
$response = $server->respondToAccessTokenRequest($serverRequest, $response); $response = $server->respondToAccessTokenRequest($serverRequest, $response);
$event = new ResolveTokenRequestEvent('onResolveTokenRequest', ['response' => $response]); $event = new ResolveTokenRequestEvent('onResolveTokenRequest', ['response' => $response]);
@ -313,7 +327,12 @@ class LoginController extends BaseController
$this->getDispatcher()->dispatch($event->getName(), $event); $this->getDispatcher()->dispatch($event->getName(), $event);
$this->app->setResponse($event->getArgument('response')); $this->app->setResponse($event->getArgument('response'));
echo $this->app->getResponse()->getBody(); echo $response->getBody();
}
catch (OAuthServerException $e)
{
$this->handleOAuthServerException($e);
}
return $this; return $this;
} }
@ -362,4 +381,23 @@ class LoginController extends BaseController
return $this; return $this;
} }
protected function handleOAuthServerException(OAuthServerException $exception)
{
/** @var SiteApplication $app */
$app = $this->app;
$app->setResponse($exception->generateHttpResponse($app->getResponse()));
$message = $exception->getMessage();
if (($hint = $exception->getHint()) !== null)
{
$message .= ' ' . $hint;
}
Log::add($message, Log::ERROR, 'com_oauthserver');
throw new \RuntimeException($message, $exception->getHttpStatusCode(), $exception);
}
} }