Skip to content

Commit

Permalink
chore: add logging to exception handler (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricklambrechts authored Jul 3, 2023
1 parent 8980ab8 commit b3a6c25
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/Services/ExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@
namespace MinVWS\OpenIDConnectLaravel\Services;

use Exception;
use Illuminate\Http\Request;
use Jumbojett\OpenIDConnectClientException;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Response;

class ExceptionHandler implements ExceptionHandlerInterface
{
public function __construct(
protected ?LoggerInterface $logger = null,
) {
}

public function handleExceptionWhileAuthenticate(OpenIDConnectClientException $exception): Response
{
if (str_starts_with($exception->getMessage(), 'Error: ')) {
Expand All @@ -20,6 +27,9 @@ public function handleExceptionWhileAuthenticate(OpenIDConnectClientException $e
return $this->handleUnableToDetermineState($exception);
}

$this->logger?->error('OIDC Exception occurred while authenticating', [
'exception' => $exception,
]);
return $this->defaultResponse($exception);
}

Expand All @@ -30,11 +40,19 @@ public function handleExceptionWhileAuthenticate(OpenIDConnectClientException $e
*/
public function handleExceptionWhileRequestUserInfo(OpenIDConnectClientException $exception): Response
{
$this->logger?->error('OIDC Exception occurred while requesting user info', [
'exception' => $exception,
]);

return $this->defaultResponse($exception);
}

public function handleException(Exception $exception): Response
{
$this->logger?->error('OIDC Generic exception occurred', [
'exception' => $exception,
]);

return $this->defaultResponseGenericException($exception);
}

Expand All @@ -46,6 +64,11 @@ public function handleException(Exception $exception): Response
*/
protected function handleRequestError(OpenIDConnectClientException $exception): Response
{
$this->logger?->debug('OIDC Request error', [
'exception' => $exception,
'query' => $this->getRequest()?->query->all(),
]);

return $this->default400Response($exception);
}

Expand All @@ -56,6 +79,10 @@ protected function handleRequestError(OpenIDConnectClientException $exception):
*/
protected function handleUnableToDetermineState(OpenIDConnectClientException $exception): Response
{
$this->logger?->debug('OIDC State in url does not match with session', [
'exception' => $exception,
]);

return $this->default400Response($exception);
}

Expand All @@ -73,4 +100,14 @@ protected function default400Response(OpenIDConnectClientException $exception):
{
abort(400, $exception->getMessage());
}

protected function getRequest(): ?Request
{
$request = request();
if (!($request instanceof Request)) {
return null;
}

return $request;
}
}

0 comments on commit b3a6c25

Please sign in to comment.