Skip to content

Commit

Permalink
Implement an alternative solution when user authentication has failed.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Dec 5, 2019
1 parent 427e081 commit a7f8f99
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
17 changes: 14 additions & 3 deletions spec/drupol/CasBundle/Security/CasGuardAuthenticatorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Psr\Log\NullLogger;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\HttpClient\Psr18Client;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
Expand Down Expand Up @@ -160,11 +159,23 @@ public function it_can_get_the_user_from_the_response()

public function it_can_redirect_on_failed_authentication(TokenInterface $token, AuthenticationException $authenticationException)
{
$request = Request::create('http://app/?ticket=ticket');
$request = Request::create('http://protected-resource/?ticket=ticket');

$this
->onAuthenticationFailure($request, $authenticationException)
->shouldBeAnInstanceOf(RedirectResponse::class);

$this
->onAuthenticationFailure($request, $authenticationException)
->headers
->all()
->shouldHaveKeyWithValue('location', ['http://protected-resource/?renew=true']);

$request = Request::create('http://protected-resource/');

$this
->onAuthenticationFailure($request, $authenticationException)
->shouldBeAnInstanceOf(JsonResponse::class);
->shouldBeNull();
}

public function it_can_redirect_on_success_authentication(TokenInterface $token)
Expand Down
24 changes: 14 additions & 10 deletions src/Security/CasGuardAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use InvalidArgumentException;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
Expand Down Expand Up @@ -129,15 +128,20 @@ public function getUser($response, UserProviderInterface $userProvider)
*/
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
return new JsonResponse(
[
'error' => 'Authentication failed.',
'reason' => $exception->getMessage(),
'description' => 'You have been redirected here to prevent infinite redirection loops between the CAS server and your application.',
]
,
500
);
if (true === $request->query->has('ticket')) {
// Remove the ticket parameter.
$uri = Uri::removeParams(
$this->uriFactory->createUri(
$request->getUri()
),
'ticket'
);

// Add the renew parameter to force login again.
$uri = Uri::withParam($uri, 'renew', 'true');

return new RedirectResponse((string) $uri);
}
}

/**
Expand Down

0 comments on commit a7f8f99

Please sign in to comment.