<commit_before><?php

/**
 * This file is part of the Da Project.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Da\ApiServerBundle\Security\Authentication\Provider;

use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Da\ApiServerBundle\Security\Authentication\Token\ApiToken;
use Da\AuthModelBundle\Model\ClientManagerInterface;
use Da\AuthModelBundle\Exception\InvalidApiTokenException;

/**
 * ApiAuthProvider class.
 *
 * @author <NAME>
 */
class ApiAuthProvider implements AuthenticationProviderInterface
{
    /**
     * The user provider.
     *
     * @var \Symfony\Component\Security\Core\User\UserProviderInterface
     */
    protected $userProvider;

    /**
     * The client manager.
     *
     * @var ClientManagerInterface
     */
    protected $clientManager;

    /**
     * @param UserProviderInterface  $userProvider  The user provider.
     * @param ClientManagerInterface $clientManager The client manager.
     */
    public function __construct(UserProviderInterface $userProvider, ClientManagerInterface $clientManager)
    {
        $this->userProvider  = $userProvider;
        $this->clientManager = $clientManager;
    }

    /**
     * {@inheritdoc}
     */
    public function authenticate(TokenInterface $token)
    {
        if (!$this->supports($token)) {
            return null;
        }

        try {
            $tokenString = $token->getToken();
            $client = $this->clientManager->getClientByApiToken($tokenString);
            $scope = $client->getScope();

            $roles = array();
            if (!empty($scope)) {
                foreach (explode(' ', $scope) as $role) {
                    $roles[] = 'ROLE_' . strtoupper($role);
                }
            }

            $token = new ApiToken($roles);
            $token->setAuthenticated(true);
            $token->setToken($tokenString);

            return $token;
        } catch (\Exception $e) {
            if ($e instanceof InvalidApiTokenException) {
                throw $e;
            }
            throw new AuthenticationException('API client authentication failed', 0, $e);
        }
    }

    /**
     * {@inheritdoc}
     */
    public function supports(TokenInterface $token)
    {
        return $token instanceof ApiToken;
    }
}<commit_msg>Change method name<commit_after><?php

/**
 * This file is part of the Da Project.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Da\ApiServerBundle\Security\Authentication\Provider;

use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Da\ApiServerBundle\Security\Authentication\Token\ApiToken;
use Da\AuthModelBundle\Model\ClientManagerInterface;
use Da\AuthModelBundle\Exception\InvalidApiTokenException;

/**
 * ApiAuthProvider class.
 *
 * @author <NAME>
 */
class ApiAuthProvider implements AuthenticationProviderInterface
{
    /**
     * The user provider.
     *
     * @var \Symfony\Component\Security\Core\User\UserProviderInterface
     */
    protected $userProvider;

    /**
     * The client manager.
     *
     * @var ClientManagerInterface
     */
    protected $clientManager;

    /**
     * @param UserProviderInterface  $userProvider  The user provider.
     * @param ClientManagerInterface $clientManager The client manager.
     */
    public function __construct(UserProviderInterface $userProvider, ClientManagerInterface $clientManager)
    {
        $this->userProvider  = $userProvider;
        $this->clientManager = $clientManager;
    }

    /**
     * {@inheritdoc}
     */
    public function authenticate(TokenInterface $token)
    {
        if (!$this->supports($token)) {
            return null;
        }

        try {
            $tokenString = $token->getToken();
            $client = $this->clientManager->findClientByApiToken($tokenString);
            $scope = $client->getScope();

            $roles = array();
            if (!empty($scope)) {
                foreach (explode(' ', $scope) as $role) {
                    $roles[] = 'ROLE_' . strtoupper($role);
                }
            }

            $token = new ApiToken($roles);
            $token->setAuthenticated(true);
            $token->setToken($tokenString);

            return $token;
        } catch (\Exception $e) {
            if ($e instanceof InvalidApiTokenException) {
                throw $e;
            }
            throw new AuthenticationException('API client authentication failed', 0, $e);
        }
    }

    /**
     * {@inheritdoc}
     */
    public function supports(TokenInterface $token)
    {
        return $token instanceof ApiToken;
    }
}
{'fraction_non_alphanumeric': 0.11049723756906077, 'fraction_numerical': 0.00036832412523020257, 'mean_word_length': 2.5706771860618014, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '7315498', 'n_tokens_mistral': 1429, 'n_tokens_neox': 1411, 'n_words': 310}