20
20
21
21
use DateTime ;
22
22
use Surfnet \SamlBundle \Monolog \SamlAuthenticationLogger ;
23
- use Surfnet \StepupBundle \Service \LoaResolutionService ;
24
- use Surfnet \StepupBundle \Service \SecondFactorTypeService ;
25
- use Surfnet \StepupBundle \Value \Loa ;
26
23
use Surfnet \StepupGateway \GatewayBundle \Exception \InvalidArgumentException ;
27
- use Surfnet \StepupGateway \GatewayBundle \Saml \Proxy \ ProxyStateHandler ;
24
+ use Surfnet \StepupGateway \GatewayBundle \Saml \ResponseContext ;
28
25
use Surfnet \StepupGateway \GatewayBundle \Service \SecondFactorService ;
29
26
30
27
class AuthenticationLogger
31
28
{
32
- /**
33
- * @var ProxyStateHandler
34
- */
35
- private $ ssoProxyStateHandler ;
36
-
37
- /**
38
- * @var ProxyStateHandler
39
- */
40
- private $ sfoProxyStateHandler ;
41
29
42
30
/**
43
31
* @var SecondFactorService
44
32
*/
45
33
private $ secondFactorService ;
46
34
47
- /**
48
- * @var LoaResolutionService
49
- */
50
- private $ loaResolutionService ;
51
-
52
35
/**
53
36
* @var SamlAuthenticationLogger
54
37
*/
55
38
private $ authenticationChannelLogger ;
39
+ private ResponseContext $ sfoResponseContext ;
40
+ private ResponseContext $ ssoResponseContext ;
56
41
57
- /**
58
- * @var SecondFactorTypeService
59
- */
60
- private $ secondFactorTypeService ;
61
42
62
43
public function __construct (
63
- LoaResolutionService $ loaResolutionService ,
64
- ProxyStateHandler $ ssoProxyStateHandler ,
65
- ProxyStateHandler $ sfoProxyStateHandler ,
66
44
SecondFactorService $ secondFactorService ,
67
45
SamlAuthenticationLogger $ authenticationChannelLogger ,
68
- SecondFactorTypeService $ service
46
+ ResponseContext $ sfoResponseContext ,
47
+ ResponseContext $ ssoResponseContext ,
69
48
) {
70
- $ this ->loaResolutionService = $ loaResolutionService ;
71
- $ this ->ssoProxyStateHandler = $ ssoProxyStateHandler ;
72
- $ this ->sfoProxyStateHandler = $ sfoProxyStateHandler ;
73
49
$ this ->secondFactorService = $ secondFactorService ;
74
50
$ this ->authenticationChannelLogger = $ authenticationChannelLogger ;
75
- $ this ->secondFactorTypeService = $ service ;
76
- }
77
-
78
- /**
79
- * @param string $requestId The SAML authentication request ID of the original request (not the proxy request).
80
- */
81
- public function logIntrinsicLoaAuthentication ($ requestId ): void
82
- {
83
- $ context = [
84
- 'second_factor_id ' => '' ,
85
- 'second_factor_type ' => '' ,
86
- 'institution ' => '' ,
87
- 'authentication_result ' => 'NONE ' ,
88
- 'resulting_loa ' => (string ) $ this ->loaResolutionService ->getLoaByLevel (Loa::LOA_1 ),
89
- ];
90
-
91
- $ this ->log ('Intrinsic Loa Requested ' , $ context , $ requestId );
51
+ $ this ->sfoResponseContext = $ sfoResponseContext ;
52
+ $ this ->ssoResponseContext = $ ssoResponseContext ;
92
53
}
93
54
94
55
/**
@@ -97,62 +58,53 @@ public function logIntrinsicLoaAuthentication($requestId): void
97
58
*/
98
59
public function logSecondFactorAuthentication (string $ requestId , string $ authenticationMode ): void
99
60
{
100
- $ stateHandler = $ this ->getStateHandler ($ authenticationMode );
101
- $ secondFactor = $ this ->secondFactorService ->findByUuid ($ stateHandler ->getSelectedSecondFactorId ());
102
- $ loa = $ this ->loaResolutionService ->getLoaByLevel ($ secondFactor ->getLoaLevel ($ this ->secondFactorTypeService ));
103
-
104
- $ context = [
105
- 'second_factor_id ' => $ secondFactor ->secondFactorId ,
106
- 'second_factor_type ' => $ secondFactor ->secondFactorType ,
107
- 'institution ' => $ secondFactor ->institution ,
108
- 'authentication_result ' => $ stateHandler ->isSecondFactorVerified () ? 'OK ' : 'FAILED ' ,
61
+ $ context = $ this ->getResponseContext ($ authenticationMode );
62
+
63
+ $ secondFactor = $ this ->secondFactorService ->findByUuid ($ context ->getSelectedSecondFactor (), $ context );
64
+ $ loa = $ this ->secondFactorService ->getLoaLevel ($ secondFactor );
65
+
66
+ $ data = [
67
+ 'second_factor_id ' => $ secondFactor ->getSecondFactorId (),
68
+ 'second_factor_type ' => $ secondFactor ->getSecondFactorType (),
69
+ 'institution ' => $ secondFactor ->getInstitution (),
70
+ 'authentication_result ' => $ context ->isSecondFactorVerified () ? 'OK ' : 'FAILED ' ,
109
71
'resulting_loa ' => (string ) $ loa ,
110
- 'sso ' => $ stateHandler ->isVerifiedBySsoOn2faCookie () ? 'YES ' : 'NO ' ,
72
+ 'sso ' => $ context ->isVerifiedBySsoOn2faCookie () ? 'YES ' : 'NO ' ,
111
73
];
112
74
113
- if ($ stateHandler ->isVerifiedBySsoOn2faCookie ()) {
114
- $ context ['sso_cookie_id ' ] = $ stateHandler ->getSsoOn2faCookieFingerprint ();
75
+ if ($ context ->isVerifiedBySsoOn2faCookie ()) {
76
+ $ context ['sso_cookie_id ' ] = $ context ->getSsoOn2faCookieFingerprint ();
115
77
}
116
78
117
- $ this ->log ('Second Factor Authenticated ' , $ context , $ requestId );
79
+ $ this ->log ('Second Factor Authenticated ' , $ data , $ requestId, $ authenticationMode );
118
80
}
119
81
120
82
/**
121
83
* @param string $message
122
- * @param array $context
84
+ * @param array $data
123
85
* @param string $requestId
124
86
*/
125
- private function log ($ message , array $ context , $ requestId ): void
87
+ private function log (string $ message , array $ data , string $ requestId, string $ authenticationMode ): void
126
88
{
127
- if (!is_string ($ requestId )) {
128
- throw InvalidArgumentException::invalidType ('string ' , 'requestId ' , $ requestId );
129
- }
130
- // Regardless of authentication type, the authentication mode can be retrieved from any state handler
131
- // given you provide the request id
132
- $ authenticationMode = $ this ->getStateHandler ('sso ' )->getAuthenticationModeForRequestId ($ requestId );
133
- $ stateHandler = $ this ->getStateHandler ($ authenticationMode );
89
+ $ context = $ this ->getResponseContext ($ authenticationMode );
134
90
135
- $ context ['identity_id ' ] = $ stateHandler ->getIdentityNameId ();
136
- $ context ['authenticating_idp ' ] = $ stateHandler ->getAuthenticatingIdp ();
137
- $ context ['requesting_sp ' ] = $ stateHandler ->getRequestServiceProvider ();
138
- $ context ['datetime ' ] = (new DateTime ())->format ('Y-m-d \\TH:i:sP ' );
91
+ $ data ['identity_id ' ] = $ context ->getIdentityNameId ();
92
+ $ data ['authenticating_idp ' ] = $ context ->getAuthenticatingIdp ();
93
+ $ data ['requesting_sp ' ] = $ context ->getRequestServiceProvider ();
94
+ $ data ['datetime ' ] = (new DateTime ())->format ('Y-m-d \\TH:i:sP ' );
139
95
140
- $ this ->authenticationChannelLogger ->forAuthentication ($ requestId )->notice ($ message , $ context );
96
+ $ this ->authenticationChannelLogger ->forAuthentication ($ requestId )->notice ($ message , $ data );
141
97
}
142
98
143
- /**
144
- * @param string $authenticationMode
145
- * @return ProxyStateHandler
146
- */
147
- private function getStateHandler ($ authenticationMode )
99
+ private function getResponseContext (string $ authenticationMode ): ResponseContext
148
100
{
149
101
if ($ authenticationMode === 'sfo ' ) {
150
- return $ this ->sfoProxyStateHandler ;
102
+ return $ this ->sfoResponseContext ;
151
103
} elseif ($ authenticationMode === 'sso ' ) {
152
- return $ this ->ssoProxyStateHandler ;
104
+ return $ this ->ssoResponseContext ;
153
105
}
154
106
throw new InvalidArgumentException (
155
- sprintf ('Retrieving a state handler for authentication type %s is not supported ' , $ authenticationMode )
107
+ sprintf ('Retrieving a response context for authentication type %s is not supported ' , $ authenticationMode )
156
108
);
157
109
}
158
110
}
0 commit comments