Skip to content
Open
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
7 changes: 5 additions & 2 deletions src/Security/TokenAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
namespace Antenna\Security;

use Antenna\Coder;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\SimplePreAuthenticatorInterface;
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
Expand Down Expand Up @@ -99,7 +99,10 @@ public function supportsToken(TokenInterface $token, $providerKey)
*/
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
return new Response($exception->getMessage(), 401, [
return new JsonResponse([
'code' => 401,
'message' => $exception->getMessage(),
], 401, [
'WWW-Authenticate' => 'Bearer',
]);
}
Expand Down
6 changes: 4 additions & 2 deletions src/Security/UsernamePasswordAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Antenna\WebToken;
use Antenna\ClaimsAwareInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Security\Core\Authentication\SimplePreAuthenticatorInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
Expand Down Expand Up @@ -116,7 +115,10 @@ public function supportsToken(TokenInterface $token, $providerKey)

public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
return new Response($exception->getMessage(), 401);
return new JsonResponse([
'code' => 401,
'message' => $exception->getMessage(),
], 401);
}

public function onAuthenticationSuccess(Request $request, TokenInterface $token)
Expand Down
4 changes: 3 additions & 1 deletion tests/Security/TokenAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ public function testAuthenticationFailedHandler()
$request = new Request();

$response = $this->authenticator->onAuthenticationFailure($request, $exception);
$decoded = json_decode($response->getContent(), true);

$this->assertEquals(401, $response->getStatusCode());
$this->assertEquals('My Custom Message', $response->getContent());
$this->assertEquals(401, $decoded['code']);
$this->assertEquals($exception->getMessage(), $decoded['message']);
}

public function testSupportsToken()
Expand Down
13 changes: 13 additions & 0 deletions tests/Security/UsernamePasswordAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
use Antenna\Coder;
use Antenna\Security\UsernamePasswordAuthenticator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Encoder\EncoderFactory;
use Symfony\Component\Security\Core\Encoder\PlaintextPasswordEncoder;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
use Symfony\Component\Security\Core\User\InMemoryUserProvider;
use Symfony\Component\Security\Core\User\User;
use Symfony\Component\Security\Core\User\UserChecker;
Expand Down Expand Up @@ -111,6 +113,17 @@ public function testAuthenticateTokenInvalidCredentials() {
$this->authenticator->authenticateToken($token, $this->userProvider, 'my_provider');
}

public function testAuthenticationFailedHandler()
{
$exception = new BadCredentialsException('The presented password is invalid.');

$response = $this->authenticator->onAuthenticationFailure(Request::create('/'), $exception);
$decoded = json_decode($response->getContent(), true);

$this->assertEquals(401, $decoded['code']);
$this->assertEquals($exception->getMessage(), $decoded['message']);
}

public function testSupportsToken()
{
$token = new UsernamePasswordToken('my_username', 'my_credential', 'my_provider');
Expand Down