Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Security] JwtAuthenticator add previous exception to thrown exception on failure #218

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/security/Http/Authenticator/JwtAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ public function onAuthenticationSuccess(Request $request, TokenInterface $token,

public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
{
throw new HttpException(Response::HTTP_FORBIDDEN, $this->translate($exception->getMessageKey(), $exception->getMessageData()));
throw new HttpException(
Response::HTTP_FORBIDDEN,
$this->translate($exception->getMessageKey(), $exception->getMessageData()),
previous: $exception
);
}

private function translate(string $message, array $data = []): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,20 +367,27 @@ public function testOnAuthenticationFailure(): void
$this->expectException(HttpException::class);
$this->expectExceptionMessage($translatedMessage);

$previous = new CustomUserMessageAuthenticationException(
$message,
$messageData
);

try {
$this->object->onAuthenticationFailure(
new Request(),
new CustomUserMessageAuthenticationException(
$message,
$messageData
)
$previous
);
} catch (HttpException $error) {
static::assertSame(
Response::HTTP_FORBIDDEN,
$error->getStatusCode()
);

static::assertSame(
$previous,
$error->getPrevious()
);

throw $error;
}
}
Expand Down Expand Up @@ -424,7 +431,7 @@ public function testOnAuthenticationFailureNoTranslator(): void
}

/**
* This is form the parent abstract class but we test it as part of a contract test.
* This is form the parent abstract class but, we test it as part of a contract test.
*
* @see AbstractAuthenticator
*/
Expand Down
Loading