<commit_before><?php

namespace Cowtent\RestBundle\Security\Authentication\Provider;

use Cowtent\AccountBundle\Entity\Application;
use Cowtent\AccountBundle\Entity\User;
use Cowtent\RestBundle\Security\Authentication\Token\WsseUserToken;
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\Exception\NonceExpiredException;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Util\StringUtils;

class WsseProvider implements AuthenticationProviderInterface
{
    /**
     * @var UserProviderInterface
     */
    private $userProvider;

    /**
     * @var string
     */
    private $cacheDir;

    /**
     * @param UserProviderInterface $userProvider
     * @param string                $cacheDir
     */
    public function __construct(UserProviderInterface $userProvider, $cacheDir)
    {
        $this->userProvider = $userProvider;
        $this->cacheDir     = $cacheDir;
    }

    /**
     * @param TokenInterface $token
     *
     * @return WsseUserToken
     */
    public function authenticate(TokenInterface $token)
    {
        $user = $this->userProvider->loadUserByUsername($token->getUsername());

        if ($user instanceof User) {
            $secret = $user->getPassword();
        } elseif ($user instanceof Application) {
            $secret = $user->getApiKey() . '{' . $user->getSalt() . '}';
        }

        if ($user && $this->validateDigest($token->digest, $token->nonce, $token->created, $secret)) {
            $authenticatedToken = new WsseUserToken($user->getRoles());
            $authenticatedToken->setUser($user);

            return $authenticatedToken;
        }

        throw new AuthenticationException('The WSSE authentication failed.');
    }

    /**
     * This function is specific to Wsse authentication and is only used to help this example
     *
     * For more information specific to the logic here, see
     * https://github.com/symfony/symfony-docs/pull/3134#issuecomment-27699129
     *
     * @param string $digest
     * @param string $nonce
     * @param string $created<commit_msg>code rewrite<commit_after><?php

namespace Cowtent\RestBundle\Security\Authentication\Provider;

use Cowtent\AccountBundle\Entity\Application;
use Cowtent\AccountBundle\Entity\User;
use Cowtent\RestBundle\Security\Authentication\Token\WsseUserToken;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
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\Exception\NonceExpiredException;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Util\StringUtils;

class WsseProvider implements AuthenticationProviderInterface
{
    /**
     * @var UserProviderInterface
     */
    private $userProvider;

    /**
     * @var string
     */
    private $cacheDir;

    /**
     * @param UserProviderInterface $userProvider
     * @param string                $cacheDir
     */
    public function __construct(UserProviderInterface $userProvider, $cacheDir)
    {
        $this->userProvider = $userProvider;
        $this->cacheDir     = $cacheDir;
    }

    /**
     * @param TokenInterface $token
     *
     * @return WsseUserToken
     */
    public function authenticate(TokenInterface $token)
    {
        /** @var WsseUserToken $token */
        $user   = $this->userProvider->loadUserByUsername($token->getUsername());
        $secret = $user->getApiKey() . '{' . $user->getSalt() . '}';

        if ($this->validateDigest($token->digest, $token->nonce, $token->created, $secret)) {
            $authenticatedToken = new WsseUserToken($user->getRoles());
            $authenticatedToken->setUser($user);

            return $authenticatedToken;
        }

        throw new AuthenticationException('The WSSE authentication failed.');
    }

    /**
     * This function is specific to Wsse authentication and is only used to help this example
     *
     * For more information specific to the logic here, see
     * https://github.com/symfony/symfony-docs/pull/3134#issuecomment-27699129
     *
     * @param string $digest
     * @param string $nonce
     * @param string $created
{'fraction_non_alphanumeric': 0.11178906079755938, 'fraction_numerical': 0.005229897581172369, 'mean_word_length': 3.2110091743119265, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 30, 'https://': 2, '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': '5108718', 'n_tokens_mistral': 1282, 'n_tokens_neox': 1236, 'n_words': 242}