Skip to content

Commit

Permalink
fix: use the Authenticator as entry point
Browse files Browse the repository at this point in the history
Let the Authenticator implements the `AuthenticationEntryPoint` interface.

(cherry picked from commit d60de33)
  • Loading branch information
drupol committed Feb 28, 2023
1 parent e6dae46 commit 4b4d065
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 42 deletions.
39 changes: 0 additions & 39 deletions src/Security/CasAuthenticationEntryPoint.php

This file was deleted.

30 changes: 27 additions & 3 deletions src/Security/CasAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,28 @@

use EcPhp\CasBundle\Cas\SymfonyCasInterface;
use EcPhp\CasBundle\Security\Core\User\CasUserProviderInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
use Throwable;

final class CasAuthenticator extends AbstractAuthenticator
final class CasAuthenticator extends AbstractAuthenticator implements AuthenticationEntryPointInterface
{
public function __construct(private readonly SymfonyCasInterface $cas, private readonly CasUserProviderInterface $userProvider)
{
public function __construct(
private readonly SymfonyCasInterface $cas,
private readonly CasUserProviderInterface $userProvider,
private readonly UrlGeneratorInterface $urlGenerator
) {
}

public function authenticate(Request $request): Passport
Expand Down Expand Up @@ -81,6 +87,24 @@ public function onAuthenticationSuccess(Request $request, TokenInterface $token,
return new RedirectResponse($request->getUri());
}

public function start(Request $request, ?AuthenticationException $authException = null): Response
{
if (true === $request->isXmlHttpRequest()) {
return new JsonResponse(
['message' => 'Authentication required'],
Response::HTTP_UNAUTHORIZED
);
}

return new RedirectResponse(
$this
->urlGenerator
->generate(
'cas_bundle_login',
)
);
}

public function supports(Request $request): bool
{
return $this
Expand Down

0 comments on commit 4b4d065

Please sign in to comment.