Skip to content

Commit c5a3049

Browse files
committed
Improve logging when handling users and institutions
1 parent d6e8867 commit c5a3049

2 files changed

Lines changed: 45 additions & 28 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,5 @@ local-php-security-checker
8787
###> phpstan/phpstan ###
8888
phpstan.neon
8989
###< phpstan/phpstan ###
90+
91+
.phplint.cache/

src/Surfnet/AzureMfa/Application/Service/AzureMfaService.php

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function __construct(
6464
public function startRegistration(EmailAddress $emailAddress): User
6565
{
6666
// TODO: test attempts / blocked
67-
$this->logger->info('Generating a new UserId based on the user email address');
67+
$this->logger->info('Generating a new UserId based on the user email address '.$emailAddress->getEmailAddress());
6868
$userId = UserId::generate($emailAddress);
6969
$user = new User($userId, $emailAddress, UserStatus::pending());
7070

@@ -76,55 +76,55 @@ public function startRegistration(EmailAddress $emailAddress): User
7676

7777
public function finishRegistration(UserId $userId): UserId
7878
{
79-
$this->logger->info('Finishing the registration');
79+
$this->logger->info('Finishing the registration for userId '.$userId->getUserId());
8080
/** @var User $user */
8181
$user = $this->session->get('user');
8282

8383
if (!$userId->isEqual($user->getUserId())) {
8484
throw new InvalidMfaAuthenticationContextException(
85-
'Unknown registration context another process is started in the meantime'
85+
'Unknown registration context another process is started in the meantime for userId '. $userId->getUserId()
8686
);
8787
}
88-
$this->logger->info('Updating user session: removing');
88+
$this->logger->info('removing user session for '.$userId->getUserId());
8989
$this->session->remove('user');
9090

9191
return $userId;
9292
}
9393

9494
public function startAuthentication(UserId $userId): User
9595
{
96-
$this->logger->info('Starting an authentication based on the provided UserId');
96+
$this->logger->info('Starting an authentication based on the provided UserId:'.$userId->getUserId());
9797
$user = new User($userId, $userId->getEmailAddress(), UserStatus::registered());
9898

99-
$this->logger->info('Updating user session: status registered');
99+
$this->logger->info('Updating user session: status registered for userId:'.$userId->getUserId());
100100
$this->session->set('user', $user);
101101

102102
return $user;
103103
}
104104

105105
public function finishAuthentication(UserId $userId): UserId
106106
{
107-
$this->logger->info('Finishing the authentication');
107+
$this->logger->info('Finishing the authenticationfor userId: '.$userId->getUserId());
108108
/** @var User $user */
109109
$user = $this->session->get('user');
110110

111111
if (!$userId->isEqual($user->getUserId())) {
112112
throw new InvalidMfaAuthenticationContextException(
113-
'Unknown authentication context another process is started in the meantime'
113+
'Unknown authentication context another process is started in the meantime for userId:'.$userId->getUserId()
114114
);
115115
}
116116

117-
$this->logger->info('Updating user session: removing');
117+
$this->logger->info('Removing user session for '.$userId->getUserId());
118118
$this->session->remove('user');
119119

120120
return $userId;
121121
}
122122

123123
public function createAuthnRequest(User $user, bool $forceAuthn = false): string
124124
{
125-
$this->logger->info('Creating a SAML2 AuthnRequest to send to the Azure MFA IdP');
125+
$this->logger->info('Creating a SAML2 AuthnRequest to send to the Azure MFA IdP for user:'.$user->getEmailAddress()->getEmailAddress());
126126

127-
$this->logger->info('Retrieve the institution for the authenticating/registering user');
127+
$this->logger->info('Retrieve the institution for the authenticating/registering user: '.$user->getEmailAddress()->getEmailAddress());
128128
$institution = $this->matchingService->findInstitutionByEmail($user->getEmailAddress());
129129
if (null === $institution) {
130130
$message = sprintf(
@@ -146,13 +146,13 @@ public function createAuthnRequest(User $user, bool $forceAuthn = false): string
146146

147147
// Use email address as subject if not sending to an AzureAD IdP
148148
if (!$azureMfaIdentityProvider->isAzureAD()) {
149-
$this->logger->info('Setting the users email address as the Subject');
149+
$this->logger->info('Setting the users email address '.$user->getEmailAddress().' as the Subject for the request to '.$azureMfaIdentityProvider->getEntityId());
150150
$authnRequest->setSubject($user->getEmailAddress()->getEmailAddress());
151151
}
152152

153153
// Set authnContextClassRef to force MFA
154154
$this->logger->info(
155-
'Setting "http://schemas.microsoft.com/claims/multipleauthn" as the authentication context class reference'
155+
'Setting "http://schemas.microsoft.com/claims/multipleauthn" as the authentication context class reference for the request to '.$azureMfaIdentityProvider->getEntityId()
156156
);
157157
$authnRequest->setAuthenticationContextClassRef('http://schemas.microsoft.com/claims/multipleauthn');
158158

@@ -183,7 +183,7 @@ public function handleResponse(Request $request): User
183183
assert($user instanceof User);
184184

185185
// Retrieve its institution and identity provider
186-
$this->logger->info('Match the user email address to one of the registered institutions');
186+
$this->logger->info('Match the user email address '.$user->getEmailAddress().'to one of the registered institutions');
187187
$institution = $this->matchingService->findInstitutionByEmail($user->getEmailAddress());
188188
if (is_null($institution)) {
189189
throw new AzureADException('The Institution could not be found using the users mail address');
@@ -209,12 +209,20 @@ public function handleResponse(Request $request): User
209209

210210
$azureMfaIdentityProvider = $this->identityProviderCache->rebuild($institution->getName());
211211

212-
$this->logger->info('Reprocess the SAML Response after updating the metadata');
213-
$assertion = $this->postBinding->processResponse(
214-
$request,
215-
$azureMfaIdentityProvider,
216-
$this->serviceProvider
217-
);
212+
$this->logger->info('Reprocess the SAML Response from '.$institution->getName()->getInstitutionName().' after updating the metadata');
213+
try {
214+
$assertion = $this->postBinding->processResponse(
215+
$request,
216+
$azureMfaIdentityProvider,
217+
$this->serviceProvider
218+
);
219+
} catch (Exception $retryException) {
220+
$this->logger->error(sprintf(
221+
'Unable to validate signature after refreshing metadata for %s',
222+
$institution->getName()->getInstitutionName()
223+
));
224+
throw $retryException;
225+
}
218226
}
219227

220228
$attributes = $assertion->getAttributes();
@@ -223,27 +231,34 @@ public function handleResponse(Request $request): User
223231
// the SAML response attribute 'http://schemas.microsoft.com/claims/authnmethodsreferences'
224232
// should contain 'http://schemas.microsoft.com/claims/multipleauthn'
225233
if ($azureMfaIdentityProvider->isAzureAD()) {
226-
$this->logger->info('This is an AzureAD IdP. Validating authnmethodsreferences in the response.');
234+
$this->logger->info($institution->getName()->getInstitutionName().' is an AzureAD IdP. Validating authnmethodsreferences in the response.');
227235
if (!isset($attributes['http://schemas.microsoft.com/claims/authnmethodsreferences']) || !in_array('http://schemas.microsoft.com/claims/multipleauthn', $attributes['http://schemas.microsoft.com/claims/authnmethodsreferences'])) {
228236
throw new AzureADException(
229-
'No http://schemas.microsoft.com/claims/multipleauthn in authnmethodsreferences.'
237+
'No http://schemas.microsoft.com/claims/multipleauthn in authnmethodsreferences response from '.$institution->getName()->getInstitutionName()
230238
);
231239
}
232240
}
233241

234242
if (!isset($attributes[self::SAML_EMAIL_ATTRIBUTE])) {
235243
throw new MissingMailAttributeException(
236-
'The mail attribute in the Azure MFA assertion was missing'
244+
'The mail attribute in the Azure MFA assertion from '.$institution->getName()->getInstitutionName(). 'was missing'
237245
);
238246
}
239247

240248
if (!in_array(strtolower($user->getEmailAddress()->getEmailAddress()), array_map('strtolower', $attributes[self::SAML_EMAIL_ATTRIBUTE]))) {
241-
throw new MailAttributeMismatchException(
242-
'The mail attribute from the Azure MFA assertion did not contain the email address provided during registration'
243-
);
249+
throw new MailAttributeMismatchException(sprintf(
250+
'The mail attribute (%s) from the Azure MFA assertion from %s did not contain the email address provided during registration (%s)',
251+
$attributes[self::SAML_EMAIL_ATTRIBUTE],
252+
$institution->getName()->getInstitutionName(),
253+
strtolower($user->getEmailAddress()->getEmailAddress())
254+
));
244255
}
245256

246-
$this->logger->info('The mail attribute in the response matched the email address of the registering/authenticating user');
247-
return $user;
257+
$this->logger->info(sprintf(
258+
'The mail attribute in the response %s matched the email address of the registering/authenticating user: %s',
259+
$attributes[self::SAML_EMAIL_ATTRIBUTE],
260+
$user->getEmailAddress()->getEmailAddress()
261+
));
262+
return $user;
248263
}
249264
}

0 commit comments

Comments
 (0)