Skip to content

Commit b3a6c25

Browse files
chore: add logging to exception handler (#20)
1 parent 8980ab8 commit b3a6c25

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/Services/ExceptionHandler.php

+37
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@
55
namespace MinVWS\OpenIDConnectLaravel\Services;
66

77
use Exception;
8+
use Illuminate\Http\Request;
89
use Jumbojett\OpenIDConnectClientException;
10+
use Psr\Log\LoggerInterface;
911
use Symfony\Component\HttpFoundation\Response;
1012

1113
class ExceptionHandler implements ExceptionHandlerInterface
1214
{
15+
public function __construct(
16+
protected ?LoggerInterface $logger = null,
17+
) {
18+
}
19+
1320
public function handleExceptionWhileAuthenticate(OpenIDConnectClientException $exception): Response
1421
{
1522
if (str_starts_with($exception->getMessage(), 'Error: ')) {
@@ -20,6 +27,9 @@ public function handleExceptionWhileAuthenticate(OpenIDConnectClientException $e
2027
return $this->handleUnableToDetermineState($exception);
2128
}
2229

30+
$this->logger?->error('OIDC Exception occurred while authenticating', [
31+
'exception' => $exception,
32+
]);
2333
return $this->defaultResponse($exception);
2434
}
2535

@@ -30,11 +40,19 @@ public function handleExceptionWhileAuthenticate(OpenIDConnectClientException $e
3040
*/
3141
public function handleExceptionWhileRequestUserInfo(OpenIDConnectClientException $exception): Response
3242
{
43+
$this->logger?->error('OIDC Exception occurred while requesting user info', [
44+
'exception' => $exception,
45+
]);
46+
3347
return $this->defaultResponse($exception);
3448
}
3549

3650
public function handleException(Exception $exception): Response
3751
{
52+
$this->logger?->error('OIDC Generic exception occurred', [
53+
'exception' => $exception,
54+
]);
55+
3856
return $this->defaultResponseGenericException($exception);
3957
}
4058

@@ -46,6 +64,11 @@ public function handleException(Exception $exception): Response
4664
*/
4765
protected function handleRequestError(OpenIDConnectClientException $exception): Response
4866
{
67+
$this->logger?->debug('OIDC Request error', [
68+
'exception' => $exception,
69+
'query' => $this->getRequest()?->query->all(),
70+
]);
71+
4972
return $this->default400Response($exception);
5073
}
5174

@@ -56,6 +79,10 @@ protected function handleRequestError(OpenIDConnectClientException $exception):
5679
*/
5780
protected function handleUnableToDetermineState(OpenIDConnectClientException $exception): Response
5881
{
82+
$this->logger?->debug('OIDC State in url does not match with session', [
83+
'exception' => $exception,
84+
]);
85+
5986
return $this->default400Response($exception);
6087
}
6188

@@ -73,4 +100,14 @@ protected function default400Response(OpenIDConnectClientException $exception):
73100
{
74101
abort(400, $exception->getMessage());
75102
}
103+
104+
protected function getRequest(): ?Request
105+
{
106+
$request = request();
107+
if (!($request instanceof Request)) {
108+
return null;
109+
}
110+
111+
return $request;
112+
}
76113
}

0 commit comments

Comments
 (0)