Skip to content

Commit 7992c15

Browse files
authored
Merge pull request #1089 from nextcloud/carl/backtrace-logs
feat: Propagate exception from saml library to our logs
2 parents a665880 + b83f7b0 commit 7992c15

1 file changed

Lines changed: 29 additions & 8 deletions

File tree

lib/Controller/SAMLController.php

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -360,14 +360,7 @@ public function assertionConsumerService(): Http\RedirectResponse {
360360

361361
$this->logger->debug('Attributes send by the IDP: ' . json_encode($auth->getAttributes(), JSON_THROW_ON_ERROR));
362362

363-
$errors = $auth->getErrors();
364-
365-
if (!empty($errors)) {
366-
foreach ($errors as $error) {
367-
$this->logger->error($error, ['app' => $this->appName]);
368-
}
369-
$this->logger->error($auth->getLastErrorReason() ?? 'No last error reason found', ['app' => $this->appName]);
370-
}
363+
$this->handleAuthErrors($auth);
371364

372365
if (!$auth->isAuthenticated()) {
373366
$this->logger->info('Auth failed', ['app' => $this->appName]);
@@ -530,6 +523,8 @@ private function tryProcessSLOResponse(?int $idp): array {
530523
));
531524
if ($auth->getLastErrorReason() === null) {
532525
return [$targetUrl, $auth];
526+
} else {
527+
$this->handleAuthErrors($auth);
533528
}
534529
} catch (Error) {
535530
continue;
@@ -660,4 +655,30 @@ public function base(): Http\TemplateResponse {
660655
$message = $this->l->t('This page should not be visited directly.');
661656
return new Http\TemplateResponse($this->appName, 'error', ['message' => $message], 'guest');
662657
}
658+
659+
private function handleAuthErrors(Auth $auth): void {
660+
$errors = $auth->getErrors();
661+
$lastReason = $auth->getLastErrorReason();
662+
663+
if ($errors === []) {
664+
return;
665+
}
666+
667+
if ($lastReason === null) {
668+
$this->logger->error('SAML errored with no error message: ' . $errors[0] . '.');
669+
return;
670+
}
671+
672+
// Only the last error has a corresponding exception and reason
673+
$this->logger->error('SAML errored with: ' . $lastReason . ' (code: ' . $errors[count($errors) - 1] . ').', [
674+
'exception' => $auth->getLastErrorException(),
675+
]);
676+
677+
if (count($errors) > 1) {
678+
// iterate from second last to first one
679+
for ($i = count($errors) - 2; $i >= 0; $i--) {
680+
$this->logger->error('Additional SAML error code: ' . $errors[$i]);
681+
}
682+
}
683+
}
663684
}

0 commit comments

Comments
 (0)