Вход Регистрация
Файл: symfony-2.7/src/Symfony/Component/Security/Http/Firewall/BasicAuthenticationListener.php
Строк: 219
<?php

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

namespace SymfonyComponentSecurityHttpFirewall;

use 
SymfonyComponentSecurityCoreAuthenticationAuthenticationManagerInterface;
use 
SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorageInterface;
use 
SymfonyComponentSecurityHttpEntryPointAuthenticationEntryPointInterface;
use 
PsrLogLoggerInterface;
use 
SymfonyComponentHttpKernelEventGetResponseEvent;
use 
SymfonyComponentSecurityCoreAuthenticationTokenUsernamePasswordToken;
use 
SymfonyComponentSecurityCoreExceptionAuthenticationException;

/**
 * BasicAuthenticationListener implements Basic HTTP authentication.
 *
 * @author Fabien Potencier <fabien@symfony.com>
 */
class BasicAuthenticationListener implements ListenerInterface
{
    private 
$tokenStorage;
    private 
$authenticationManager;
    private 
$providerKey;
    private 
$authenticationEntryPoint;
    private 
$logger;
    private 
$ignoreFailure;

    public function 
__construct(TokenStorageInterface $tokenStorageAuthenticationManagerInterface $authenticationManager$providerKeyAuthenticationEntryPointInterface $authenticationEntryPointLoggerInterface $logger null)
    {
        if (empty(
$providerKey)) {
            throw new 
InvalidArgumentException('$providerKey must not be empty.');
        }

        
$this->tokenStorage $tokenStorage;
        
$this->authenticationManager $authenticationManager;
        
$this->providerKey $providerKey;
        
$this->authenticationEntryPoint $authenticationEntryPoint;
        
$this->logger $logger;
        
$this->ignoreFailure false;
    }

    
/**
     * Handles basic authentication.
     *
     * @param GetResponseEvent $event A GetResponseEvent instance
     */
    
public function handle(GetResponseEvent $event)
    {
        
$request $event->getRequest();

        if (
false === $username $request->headers->get('PHP_AUTH_USER'false)) {
            return;
        }

        if (
null !== $token $this->tokenStorage->getToken()) {
            if (
$token instanceof UsernamePasswordToken && $token->isAuthenticated() && $token->getUsername() === $username) {
                return;
            }
        }

        if (
null !== $this->logger) {
            
$this->logger->info('Basic authentication Authorization header found for user.', array('username' => $username));
        }

        try {
            
$token $this->authenticationManager->authenticate(new UsernamePasswordToken($username$request->headers->get('PHP_AUTH_PW'), $this->providerKey));
            
$this->tokenStorage->setToken($token);
        } catch (
AuthenticationException $failed) {
            
$token $this->tokenStorage->getToken();
            if (
$token instanceof UsernamePasswordToken && $this->providerKey === $token->getProviderKey()) {
                
$this->tokenStorage->setToken(null);
            }

            if (
null !== $this->logger) {
                
$this->logger->info('Basic authentication failed for user.', array('username' => $username'exception' => $failed));
            }

            if (
$this->ignoreFailure) {
                return;
            }

            
$event->setResponse($this->authenticationEntryPoint->start($request$failed));
        }
    }
}
Онлайн: 0
Реклама