5
5
namespace MinVWS \OpenIDConnectLaravel \Services ;
6
6
7
7
use Exception ;
8
+ use Illuminate \Http \Request ;
8
9
use Jumbojett \OpenIDConnectClientException ;
10
+ use Psr \Log \LoggerInterface ;
9
11
use Symfony \Component \HttpFoundation \Response ;
10
12
11
13
class ExceptionHandler implements ExceptionHandlerInterface
12
14
{
15
+ public function __construct (
16
+ protected ?LoggerInterface $ logger = null ,
17
+ ) {
18
+ }
19
+
13
20
public function handleExceptionWhileAuthenticate (OpenIDConnectClientException $ exception ): Response
14
21
{
15
22
if (str_starts_with ($ exception ->getMessage (), 'Error: ' )) {
@@ -20,6 +27,9 @@ public function handleExceptionWhileAuthenticate(OpenIDConnectClientException $e
20
27
return $ this ->handleUnableToDetermineState ($ exception );
21
28
}
22
29
30
+ $ this ->logger ?->error('OIDC Exception occurred while authenticating ' , [
31
+ 'exception ' => $ exception ,
32
+ ]);
23
33
return $ this ->defaultResponse ($ exception );
24
34
}
25
35
@@ -30,11 +40,19 @@ public function handleExceptionWhileAuthenticate(OpenIDConnectClientException $e
30
40
*/
31
41
public function handleExceptionWhileRequestUserInfo (OpenIDConnectClientException $ exception ): Response
32
42
{
43
+ $ this ->logger ?->error('OIDC Exception occurred while requesting user info ' , [
44
+ 'exception ' => $ exception ,
45
+ ]);
46
+
33
47
return $ this ->defaultResponse ($ exception );
34
48
}
35
49
36
50
public function handleException (Exception $ exception ): Response
37
51
{
52
+ $ this ->logger ?->error('OIDC Generic exception occurred ' , [
53
+ 'exception ' => $ exception ,
54
+ ]);
55
+
38
56
return $ this ->defaultResponseGenericException ($ exception );
39
57
}
40
58
@@ -46,6 +64,11 @@ public function handleException(Exception $exception): Response
46
64
*/
47
65
protected function handleRequestError (OpenIDConnectClientException $ exception ): Response
48
66
{
67
+ $ this ->logger ?->debug('OIDC Request error ' , [
68
+ 'exception ' => $ exception ,
69
+ 'query ' => $ this ->getRequest ()?->query->all (),
70
+ ]);
71
+
49
72
return $ this ->default400Response ($ exception );
50
73
}
51
74
@@ -56,6 +79,10 @@ protected function handleRequestError(OpenIDConnectClientException $exception):
56
79
*/
57
80
protected function handleUnableToDetermineState (OpenIDConnectClientException $ exception ): Response
58
81
{
82
+ $ this ->logger ?->debug('OIDC State in url does not match with session ' , [
83
+ 'exception ' => $ exception ,
84
+ ]);
85
+
59
86
return $ this ->default400Response ($ exception );
60
87
}
61
88
@@ -73,4 +100,14 @@ protected function default400Response(OpenIDConnectClientException $exception):
73
100
{
74
101
abort (400 , $ exception ->getMessage ());
75
102
}
103
+
104
+ protected function getRequest (): ?Request
105
+ {
106
+ $ request = request ();
107
+ if (!($ request instanceof Request)) {
108
+ return null ;
109
+ }
110
+
111
+ return $ request ;
112
+ }
76
113
}
0 commit comments