Skip to content

Commit

Permalink
[Security] JwtAuthenticator add previous exception to thrown exceptio…
Browse files Browse the repository at this point in the history
…n on failure
  • Loading branch information
mpoiriert committed Dec 12, 2023
1 parent 2125f6d commit 35a5e39
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
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

0 comments on commit 35a5e39

Please sign in to comment.