<commit_before><?php namespace Anomaly\ThrottleSecurityCheckExtension;

use Anomaly\UsersModule\Authenticator\Authenticator;
use Anomaly\UsersModule\User\Contract\UserInterface;
use Illuminate\Cache\Repository;
use Illuminate\Http\Request;

/**
 * Class ThrottleSecurityCheckHandler
 *
 * @link          http://anomaly.is/streams-platform
 * @author        AnomalyLabs, Inc. <<EMAIL>>
 * @author        <NAME> <<EMAIL>>
 * @package       Anomaly\ThrottleSecurityCheckExtension
 */
class ThrottleSecurityCheckHandler
{

    /**
     * The cache repository.
     *
     * @var Repository
     */
    protected $cache;

    /**
     * The request object.
     *
     * @var Request
     */
    protected $request;

    /**
     * The throttle extension instance.
     *
     * @var ThrottleSecurityCheckExtension
     */
    protected $extension;

    /**
     * The authenticator utility.
     *
     * @var Authenticator
     */
    protected $authenticator;

    /**
     * Create a new ActivationSecurityCheckHandler instance.
     *
     * @param Repository                     $cache
     * @param Request                        $request
     * @param Authenticator                  $authenticator
     * @param ThrottleSecurityCheckExtension $extension
     */
    public function __construct(
        Repository $cache,
        Request $request,
        Authenticator $authenticator,
        ThrottleSecurityCheckExtension $extension
    ) {
        $this->cache         = $cache;
        $this->request       = $request;
        $this->extension     = $extension;
        $this->authenticator = $authenticator;
    }

    /**
     * Handle the security check.
     *
     * @param UserInterface $user
     * @return bool
     */
    public function handle(UserInterface $user = null)
    {
        /**
         * If the user is present
         * or this is not a post request
         * then we don't need to throttle.
         */
        if ($user || $this->request->method() !== 'POST') {
            return true;
        }

        $attempts   = $this->getLoginAttempts();
        $expiration = $this->getExpirationTime();

        if ($expiration || $attempts > 5) {

            $this->setLoginAttempts($attempts + 1);

            return false;
        }

        $this->setLoginAttempts($attempts + 1);

        return true;
    }

    /**
     * Get the login attempts.
     *
     * @return int
     */
    protected function getLoginAttempts()
    {
        return $this->cache->get($this->extension->getNamespace('attempts'), 0);
    }

    /**
     * Set the login attempts.
     *
     * @param $attempts
     */
    protected function setLoginAttempts($attempts)
    {
        $this->cache->put($this->extension->getNamespace('attempts'), $attempts, 1);
    }

    /**
     * Get the expiration time.
     *
     * @return null|string
     */
    protected function getExpirationTime()
    {
        return $this->cache->get($this->extension->getNamespace('expiration'));
    }

    /**
     * Set the expiration time.
     */
    public function setExpirationTime()
    {
        $this->cache->put($this->extension->getNamespace('expiration'), time() + 60, 1);
    }
}
<commit_msg>Add IP to the cache key<commit_after><?php namespace Anomaly\ThrottleSecurityCheckExtension;

use Anomaly\UsersModule\Authenticator\Authenticator;
use Anomaly\UsersModule\User\Contract\UserInterface;
use Illuminate\Cache\Repository;
use Illuminate\Http\Request;

/**
 * Class ThrottleSecurityCheckHandler
 *
 * @link          http://anomaly.is/streams-platform
 * @author        AnomalyLabs, Inc. <<EMAIL>>
 * @author        <NAME> <<EMAIL>>
 * @package       Anomaly\ThrottleSecurityCheckExtension
 */
class ThrottleSecurityCheckHandler
{

    /**
     * The cache repository.
     *
     * @var Repository
     */
    protected $cache;

    /**
     * The request object.
     *
     * @var Request
     */
    protected $request;

    /**
     * The throttle extension instance.
     *
     * @var ThrottleSecurityCheckExtension
     */
    protected $extension;

    /**
     * The authenticator utility.
     *
     * @var Authenticator
     */
    protected $authenticator;

    /**
     * Create a new ActivationSecurityCheckHandler instance.
     *
     * @param Repository                     $cache
     * @param Request                        $request
     * @param Authenticator                  $authenticator
     * @param ThrottleSecurityCheckExtension $extension
     */
    public function __construct(
        Repository $cache,
        Request $request,
        Authenticator $authenticator,
        ThrottleSecurityCheckExtension $extension
    ) {
        $this->cache         = $cache;
        $this->request       = $request;
        $this->extension     = $extension;
        $this->authenticator = $authenticator;
    }

    /**
     * Handle the security check.
     *
     * @param UserInterface $user
     * @return bool
     */
    public function handle(UserInterface $user = null)
    {
        /**
         * If the user is present
         * or this is not a post request
         * then we don't need to throttle.
         */
        if ($user || $this->request->method() !== 'POST') {
            return true;
        }

        $attempts   = $this->getLoginAttempts();
        $expiration = $this->getExpirationTime();

        if ($expiration || $attempts > 5) {

            $this->setLoginAttempts($attempts + 1);

            return false;
        }

        $this->setLoginAttempts($attempts + 1);

        return true;
    }

    /**
     * Get the login attempts.
     *
     * @return int
     */
    protected function getLoginAttempts()
    {
        return $this->cache->get($this->extension->getNamespace('attempts:' . $this->request->ip()), 0);
    }

    /**
     * Set the login attempts.
     *
     * @param $attempts
     */
    protected function setLoginAttempts($attempts)
    {
        $this->cache->put($this->extension->getNamespace('attempts:' . $this->request->ip()), $attempts, 1);
    }

    /**
     * Get the expiration time.
     *
     * @return null|string
     */
    protected function getExpirationTime()
    {
        return $this->cache->get($this->extension->getNamespace('expiration:' . $this->request->ip()));
    }

    /**
     * Set the expiration time.
     */
    public function setExpirationTime()
    {
        $this->cache->put($this->extension->getNamespace('expiration:' . $this->request->ip()), time() + 60, 1);
    }
}

{'fraction_non_alphanumeric': 0.12407862407862408, 'fraction_numerical': 0.002457002457002457, 'mean_word_length': 1.976691042047532, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 75, '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': '2348101', 'n_tokens_mistral': 1908, 'n_tokens_neox': 1809, 'n_words': 391}