diff --git a/src/Domain/Process/OAuth2/GrantAccessByOAuth2TokenProcess.php b/src/Domain/Process/OAuth2/GrantAccessByOAuth2TokenProcess.php index 43ce5d1..c62a7f5 100644 --- a/src/Domain/Process/OAuth2/GrantAccessByOAuth2TokenProcess.php +++ b/src/Domain/Process/OAuth2/GrantAccessByOAuth2TokenProcess.php @@ -22,10 +22,12 @@ use Exception; use Lcobucci\JWT\Token\RegisteredClaims; use Psr\Log\LoggerInterface; -use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; +use Symfony\Contracts\HttpClient\Exception\ExceptionInterface; +use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface; use Throwable; final class GrantAccessByOAuth2TokenProcess @@ -125,6 +127,27 @@ public function createRedirectResponseForRequest(Request $request, UserOAuthLogi private function logException(Request $request, Throwable $throwable): void { $context = $this->contextFactory->buildFromRequest($request); + + $content = $throwable->getTraceAsString(); + $prevException = $throwable->getPrevious(); + if ($prevException) { + $content .= "\nPrevious exception:\n" . $prevException->getTraceAsString(); + } + if ($prevException instanceof HttpExceptionInterface) { + $response = $prevException->getResponse(); + try { + $context + ->setHttpStatus($response->getStatusCode()) + ->setResponse($response->getContent()) + ; + } catch (ExceptionInterface $responseException) { + $context + ->setResponse(sprintf('Failed to retrieve a response content! (error: %s)', $responseException->getMessage())) + ; + } + } + + $context->setContent($content); $arrayContext = $this->serializer->toArray($context); if (false === is_array($arrayContext)) { $arrayContext = []; diff --git a/src/Security/Authentication/ApiTokenAuthenticator.php b/src/Security/Authentication/ApiTokenAuthenticator.php index af60d68..190b8d2 100644 --- a/src/Security/Authentication/ApiTokenAuthenticator.php +++ b/src/Security/Authentication/ApiTokenAuthenticator.php @@ -58,10 +58,15 @@ public function onAuthenticationFailure(Request $request, AuthenticationExceptio private function getCredentials(Request $request): array { - return u((string) $request->headers->get('Authorization')) + $credentials = u((string) $request->headers->get('Authorization')) ->replaceMatches('~Bearer[\s+]~', '') ->trim() ->split(':', 2); + if (2 === count($credentials)) { + return $credentials; + } + + return [null, null]; } private function checkCredentials(string $token, ApiTokenUserInterface $user): bool