Skip to content

Commit

Permalink
Logging improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
pmeulen committed Aug 19, 2022
1 parent e4336db commit 0c03a0d
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions src/Controller/AuthenticationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ public function __construct(
public function authenticationAction(Request $request): Response
{
$nameId = $this->authenticationService->getNameId();
$logger = WithContextLogger::from($this->logger, ['nameId' => $nameId]);
$sari = $this->stateHandler->getRequestId();
$logger = WithContextLogger::from($this->logger, ['nameId' => $nameId, 'sari' => $sari]);

$logger->info('Verifying if there is a pending authentication request from SP');

Expand Down Expand Up @@ -139,7 +140,7 @@ public function authenticationAction(Request $request): Response
$logger->info('Start authentication');
$this->tiqrService->startAuthentication(
$nameId,
$this->stateHandler->getRequestId()
$sari
);
} catch (Exception $e) {
$logger->error(sprintf(
Expand All @@ -165,17 +166,20 @@ public function authenticationAction(Request $request): Response
*/
public function authenticationStatusAction(): JsonResponse
{
$this->logger->info('Request for authentication status');
$nameId = $this->authenticationService->getNameId();
$sari = $this->stateHandler->getRequestId();
$logger = WithContextLogger::from($this->logger, ['nameId' => $nameId, 'sari' => $sari]);
$logger->info('Request for authentication status');

if (!$this->authenticationService->authenticationRequired()) {
$this->logger->error('There is no pending authentication request from SP');
$logger->error('There is no pending authentication request from SP');
return $this->refreshAuthenticationPage();
}

$isAuthenticated = $this->tiqrService->isAuthenticated();

if ($isAuthenticated) {
$this->logger->info('Send json response is authenticated');
$logger->info('Send json response is authenticated');

return $this->refreshAuthenticationPage();
}
Expand All @@ -184,7 +188,7 @@ public function authenticationStatusAction(): JsonResponse
return $this->timeoutNeedsManualRetry();
}

$this->logger->info('Send json response is not authenticated');
$logger->info('Send json response is not authenticated');

return $this->scheduleNextPollOnAuthenticationPage();
}
Expand Down Expand Up @@ -281,16 +285,19 @@ private function generateNotificationResponse(string $status): JsonResponse
*/
public function authenticationQrAction(): Response
{
$this->logger->info('Client request QR image');
$nameId = $this->authenticationService->getNameId();
$sari = $this->stateHandler->getRequestId();
$logger = WithContextLogger::from($this->logger, ['nameId' => $nameId, 'sari' => $sari]);
$logger->info('Client request QR image');

// Do have a valid sample AuthnRequest?.
if (!$this->authenticationService->authenticationRequired()) {
$this->logger->error('There is no pending authentication request from SP');
$logger->error('There is no pending authentication request from SP');

return new Response('No authentication required', Response::HTTP_BAD_REQUEST);
}

$this->logger->info('Return QR image response');
$logger->info('Return QR image response');

return $this->tiqrService->createAuthenticationQRResponse();
}
Expand All @@ -302,23 +309,25 @@ public function authenticationQrAction(): Response
*/
public function authenticationNotificationAction(): Response
{
$this->logger->info('Client request QR image');
$nameId = $this->authenticationService->getNameId();
$sari = $this->stateHandler->getRequestId();
$logger = WithContextLogger::from($this->logger, ['nameId' => $nameId, 'sari' => $sari]);
$logger->info('Client requests sending push notification');

// Do have a valid sample AuthnRequest?.
if (!$this->authenticationService->authenticationRequired()) {
$this->logger->error('There is no pending authentication request from SP');
$logger->error('There is no pending authentication request from SP');

return new Response('No authentication required', Response::HTTP_BAD_REQUEST);
}

$this->logger->info('Return QR image response');
$logger->info('Sending push notification');

// Get user.
$nameId = $this->authenticationService->getNameId();
try {
$user = $this->userRepository->getUser($nameId);
} catch (UserNotExistsException $exception) {
$this->logger->error(sprintf(
$logger->error(sprintf(
'User with nameId "%s" not found, error "%s"',
$nameId,
$exception->getMessage()
Expand All @@ -330,6 +339,13 @@ public function authenticationNotificationAction(): Response
// Send notification.
$notificationType = $user->getNotificationType();
$notificationAddress = $user->getNotificationAddress();
$this->logger->notice(sprintf(
'Sending push notification for user "%s" with type "%s" and (untranslated) address "%s"',
$nameId,
$notificationType,
$notificationAddress
));

if ($notificationType && $notificationAddress) {
$result = $this->sendNotification($notificationType, $notificationAddress);
if ($result) {
Expand Down Expand Up @@ -385,12 +401,6 @@ private function showUserIsBlockedErrorPage(bool $isBlockedPermanently): Respons
*/
private function sendNotification(string $notificationType, string $notificationAddress): bool
{
$this->logger->notice(sprintf(
'Sending client notification for type "%s" and address "%s"',
$notificationType,
$notificationAddress
));

try {
$this->tiqrService->sendNotification($notificationType, $notificationAddress);
} catch (Exception $e) {
Expand Down

0 comments on commit 0c03a0d

Please sign in to comment.