<commit_before><?php
// src/AppBundle/Security/TokenAuthenticator.php
namespace PRayno\CasAuthBundle\Security;

use GuzzleHttp\Client;
use PRayno\CasAuthBundle\Event\CASAuthenticationFailureEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserProviderInterface;

class CasAuthenticator extends AbstractGuardAuthenticator
{
    protected $server_login_url;
    protected $server_validation_url;
    protected $xml_namespace;
    protected $username_attribute;
    protected $query_ticket_parameter;
    protected $query_service_parameter;
    protected $options;

    private $eventDispatcher;

    /**
     * Process configuration
     * @param array $config
     */
    public function __construct($config, EventDispatcherInterface $eventDispatcher)
    {
        $this->server_login_url = $config['server_login_url'];
        $this->server_validation_url = $config['server_validation_url'];
        $this->xml_namespace = $config['xml_namespace'];
        $this->username_attribute = $config['username_attribute'];
        $this->query_service_parameter = $config['query_service_parameter'];
        $this->query_ticket_parameter = $config['query_ticket_parameter'];
        $this->options = $config['options'];

        $this->eventDispatcher = $eventDispatcher;
    }

    /**
     * Does the authenticator support the given Request?
     *
     * If this returns false, the authenticator will be skipped.
     *
     * @param Request $request
     *
     * @return bool
     */
    public function supports(Request $request)
    {
        return (bool) $request->get($this->query_ticket_parameter);
    }

    /**
     * Called on every request. Return whatever credentials you want,
     * or null to stop authentication.
     */
    public function getCredentials(Request $request)
    {
        $url = $this->server_validation_url.'?'.$this->query_ticket_parameter.'='.
            $request->get($this->query_ticket_parameter).'&'.
            $this->query_service_parameter.'='.urlencode($this->removeCasTicket($request->getUri()));

        $client = new Client();
        $response = $client->request('GET', $url, $this->options);

        $string = $response->getBody()->getContents();

        $xml = new \SimpleXMLElement($string, 0, false, $this->xml_namespace, true);

        if (isset($xml->authenticationSuccess)) {
            return (array) $xml->authenticationSuccess;
        }
    }

    /**
     * Calls the UserProvider providing a valid User
     * @param array $credentials
     * @param UserProviderInterface $userProvider
     * @return bool
     */
    public function getUser($credentials, UserProviderInterface $userProvider)
    {
        if (isset($credentials[$this->username_attribute])) {
            return $userProvider->loadUserByUsername($credentials[$this->username_attribute]);
        } else {<commit_msg>Return an empty array from getCredentials if authentication does not succeed<commit_after><?php
// src/AppBundle/Security/TokenAuthenticator.php
namespace PRayno\CasAuthBundle\Security;

use GuzzleHttp\Client;
use PRayno\CasAuthBundle\Event\CASAuthenticationFailureEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserProviderInterface;

class CasAuthenticator extends AbstractGuardAuthenticator
{
    protected $server_login_url;
    protected $server_validation_url;
    protected $xml_namespace;
    protected $username_attribute;
    protected $query_ticket_parameter;
    protected $query_service_parameter;
    protected $options;

    private $eventDispatcher;

    /**
     * Process configuration
     * @param array $config
     */
    public function __construct($config, EventDispatcherInterface $eventDispatcher)
    {
        $this->server_login_url = $config['server_login_url'];
        $this->server_validation_url = $config['server_validation_url'];
        $this->xml_namespace = $config['xml_namespace'];
        $this->username_attribute = $config['username_attribute'];
        $this->query_service_parameter = $config['query_service_parameter'];
        $this->query_ticket_parameter = $config['query_ticket_parameter'];
        $this->options = $config['options'];

        $this->eventDispatcher = $eventDispatcher;
    }

    /**
     * Does the authenticator support the given Request?
     *
     * If this returns false, the authenticator will be skipped.
     *
     * @param Request $request
     *
     * @return bool
     */
    public function supports(Request $request)
    {
        return (bool) $request->get($this->query_ticket_parameter);
    }

    /**
     * {@inheritdoc}
     */
    public function getCredentials(Request $request)
    {
        $url = $this->server_validation_url.'?'.$this->query_ticket_parameter.'='.
            $request->get($this->query_ticket_parameter).'&'.
            $this->query_service_parameter.'='.urlencode($this->removeCasTicket($request->getUri()));

        $client = new Client();
        $response = $client->request('GET', $url, $this->options);

        $string = $response->getBody()->getContents();

        $xml = new \SimpleXMLElement($string, 0, false, $this->xml_namespace, true);

        if (isset($xml->authenticationSuccess)) {
            return (array) $xml->authenticationSuccess;
        }
        return [];
    }

    /**
     * Calls the UserProvider providing a valid User
     * @param array $credentials
     * @param UserProviderInterface $userProvider
     * @return bool
     */
    public function getUser($credentials, UserProviderInterface $userProvider)
    {
        if (isset($credentials[$this->username_attribute])) {
            return $userProvider->loadUserByUsername($credentials[$this->username_attribute]);
        } else {
{'fraction_non_alphanumeric': 0.11541929666366095, 'fraction_numerical': 0.00030057108506161706, 'mean_word_length': 3.7265625, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2619038', 'n_tokens_mistral': 1805, 'n_tokens_neox': 1748, 'n_words': 334}