Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Ldap][Security] Make LdapAuthenticator an EntryPoint #42354

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

Merged
merged 2 commits into from
Sep 26, 2021
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
23 changes: 22 additions & 1 deletion src/Symfony/Component/Ldap/Security/LdapAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface;
use Symfony\Component\Security\Http\Authenticator\InteractiveAuthenticatorInterface;
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
use Symfony\Component\Security\Http\EntryPoint\Exception\NotAnEntryPointException;

/**
* This class decorates internal authenticators to add the LDAP integration.
Expand All @@ -29,7 +32,7 @@
*
* @final
*/
class LdapAuthenticator implements AuthenticatorInterface
class LdapAuthenticator implements AuthenticationEntryPointInterface, InteractiveAuthenticatorInterface
{
private $authenticator;
private $ldapServiceId;
Expand Down Expand Up @@ -75,4 +78,22 @@ public function onAuthenticationFailure(Request $request, AuthenticationExceptio
{
return $this->authenticator->onAuthenticationFailure($request, $exception);
}

public function start(Request $request, AuthenticationException $authException = null): Response
{
if (!$this->authenticator instanceof AuthenticationEntryPointInterface) {
throw new NotAnEntryPointException(sprintf('Decorated authenticator "%s" does not implement interface "%s".', get_debug_type($this->authenticator), AuthenticationEntryPointInterface::class));
}

return $this->authenticator->start($request, $authException);
}

public function isInteractive(): bool
{
if ($this->authenticator instanceof InteractiveAuthenticatorInterface) {
return $this->authenticator->isInteractive();
}

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Security\Http\EntryPoint\Exception;

/**
* Thrown by generic decorators when a decorated authenticator does not implement
* {@see AuthenticationEntryPointInterface}.
*
* @author Robin Chalas <[email protected]>
*/
class NotAnEntryPointException extends \RuntimeException
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Http\Authorization\AccessDeniedHandlerInterface;
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
use Symfony\Component\Security\Http\EntryPoint\Exception\NotAnEntryPointException;
use Symfony\Component\Security\Http\HttpUtils;
use Symfony\Component\Security\Http\Util\TargetPathTrait;

Expand Down Expand Up @@ -195,11 +196,7 @@ private function handleLogoutException(ExceptionEvent $event, LogoutException $e
private function startAuthentication(Request $request, AuthenticationException $authException): Response
{
if (null === $this->authenticationEntryPoint) {
if (null !== $this->logger) {
$this->logger->notice(sprintf('No Authentication entry point configured, returning a %s HTTP response. Configure "entry_point" on the firewall "%s" if you want to modify the response.', Response::HTTP_UNAUTHORIZED, $this->firewallName));
}

throw new HttpException(Response::HTTP_UNAUTHORIZED, $authException->getMessage(), $authException, [], $authException->getCode());
$this->throwUnauthorizedException($authException);
}

if (null !== $this->logger) {
Expand All @@ -219,7 +216,11 @@ private function startAuthentication(Request $request, AuthenticationException $
}
}

$response = $this->authenticationEntryPoint->start($request, $authException);
try {
$response = $this->authenticationEntryPoint->start($request, $authException);
} catch (NotAnEntryPointException $e) {
$this->throwUnauthorizedException($authException);
}

if (!$response instanceof Response) {
$given = get_debug_type($response);
Expand All @@ -237,4 +238,13 @@ protected function setTargetPath(Request $request)
$this->saveTargetPath($request->getSession(), $this->firewallName, $request->getUri());
}
}

private function throwUnauthorizedException(AuthenticationException $authException)
{
if (null !== $this->logger) {
$this->logger->notice(sprintf('No Authentication entry point configured, returning a %s HTTP response. Configure "entry_point" on the firewall "%s" if you want to modify the response.', Response::HTTP_UNAUTHORIZED, $this->firewallName));
}

throw new HttpException(Response::HTTP_UNAUTHORIZED, $authException->getMessage(), $authException, [], $authException->getCode());
}
}