<commit_before><?php
namespace Bolt\AccessControl;

use Bolt\Application;
use Bolt\Storage\Entity;
use Bolt\Storage\Repository;
use Bolt\Translation\Translator as Trans;
use Doctrine\DBAL\DBALException;
use Hautelook\Phpass\PasswordHash;
use Symfony\Component\HttpFoundation\Request;
use UAParser;

/**
 * Password handling.
 *
 * @author <NAME> <<EMAIL>>
 */
class Password
{
    /** @var \Bolt\Application $app */
    protected $app;

    public function __construct(Application $app)
    {
        /*
         * $this->app['config']
         * $this->app['logger.system']
         * $this->app['logger.flash']
         * $this->app['mailer']
         * $this->app['randomgenerator']
         * $this->app['resources']
         * $this->app['render']
         */
        $this->app = $app;
    }

    /**
     * Set a random password for user.
     *
     * @param string $username User specified by ID, username or email address.
     *
     * @return string|boolean New password or FALSE when no match for username.
     */
    public function setRandomPassword($username)
    {
        $password = false;

        if ($userEntity = $this->app['storage']->getRepository('Bolt\Storage\Entity\Users')->getUser($username)) {
            $password = $this->app['randomgenerator']->generateString(12);

            $hashStrength = max($this->app['config']->get('general/hash_strength'), 8);
            $hasher = new PasswordHash($hashStrength, true);
            $hashedpassword = $hasher->HashPassword($password);

            $userEntity->setPassword($hashedpassword);
            $userEntity->setShadowpassword('');
            $userEntity->setShadowtoken('');
            $userEntity->setShadowvalidity(null);

            $this->app['storage']->getRepository('Bolt\Storage\Entity\Users')->save($userEntity);

            $this->app['logger.system']->info(
                "Password for user '{$userEntity->getUsername()}' was reset via Nut.",
                ['event' => 'authentication']
            );
        }

        return $password;
    }

    /**
     * Handle a password reset confirmation
     *
     * @param string $token
     * @param string $remoteIP
     *
     * @return void
     */
    public function resetPasswordConfirm($token, $remoteIP)
    {
        // Hash the remote caller's IP with the token
        $tokenHash = md5($token . '-' . str_replace('.', '-', $remoteIP));

        if ($userEntity = $this->app['storage']->getRepository('Bolt\Storage\Entity\Users')->getUserShadowAuth($tokenHash)) {
            // Update entries
            $userEntity->setPassword($userEntity->getShadowpassword());
            $userEntity->setShadowpassword('');
            $userEntity->setShadowtoken('');
            $userEntity->setShadowvalidity(null);
            $this->app['storage']->getRepository('Bolt\Storage\Entity\Users')->save($userEntity);

            $this->app['logger.flash']->success(Trans::__('Password reset successful! You can now log on with the password that was sent to you via email.'));
        } else {
            // That was not a valid token, or too late, or not from the correct IP.
            $this->app['logger.system']->error('Somebody tried to reset a password with an invalid token.', ['event' => 'authentication']);
            $this->app['logger.flash']->error(Trans::__('Password reset not successful! Either the token was incorrect, or you were too late, or you tried to reset the password from a different IP-address.'));
        }
    }

    /**
     * Sends email with password request. Accepts email or username.
     *
     * @param string $username
     * @param string $remoteIP
     *
     * @return boolean
     */
    public function resetPasswordRequest($username, $remoteIP)
    {
        $userEntity = $this->app['storage']->getRepository('Bolt\Storage\Entity\Users')->getUser($username);

        if (!$userEntity) {
            // For safety, this is the message we display, regardless of whether user exists.
            $this->app['logger.flash']->info(Trans::__("A password reset link has been sent to '%user%'.", ['%user%' => $username]));

            return false;
        }

        $shadowPassword = $this->app['randomgenerator']->generateString(12);
        $shadowToken = $this->app['randomgenerator']->generateString(32);
        $shadowTokenHash = md5($shadowToken . '-' . str_replace('.', '-', $remoteIP));
        $hasher = new PasswordHash($this->hashStrength, true);<commit_msg>Return an indicative boolean from Password::resetPasswordConfirm()<commit_after><?php
namespace Bolt\AccessControl;

use Bolt\Application;
use Bolt\Storage\Entity;
use Bolt\Storage\Repository;
use Bolt\Translation\Translator as Trans;
use Doctrine\DBAL\DBALException;
use Hautelook\Phpass\PasswordHash;
use Symfony\Component\HttpFoundation\Request;
use UAParser;

/**
 * Password handling.
 *
 * @author <NAME> <<EMAIL>>
 */
class Password
{
    /** @var \Bolt\Application $app */
    protected $app;

    public function __construct(Application $app)
    {
        /*
         * $this->app['config']
         * $this->app['logger.system']
         * $this->app['logger.flash']
         * $this->app['mailer']
         * $this->app['randomgenerator']
         * $this->app['resources']
         * $this->app['render']
         */
        $this->app = $app;
    }

    /**
     * Set a random password for user.
     *
     * @param string $username User specified by ID, username or email address.
     *
     * @return string|boolean New password or FALSE when no match for username.
     */
    public function setRandomPassword($username)
    {
        $password = false;

        if ($userEntity = $this->app['storage']->getRepository('Bolt\Storage\Entity\Users')->getUser($username)) {
            $password = $this->app['randomgenerator']->generateString(12);

            $hashStrength = max($this->app['config']->get('general/hash_strength'), 8);
            $hasher = new PasswordHash($hashStrength, true);
            $hashedpassword = $hasher->HashPassword($password);

            $userEntity->setPassword($hashedpassword);
            $userEntity->setShadowpassword('');
            $userEntity->setShadowtoken('');
            $userEntity->setShadowvalidity(null);

            $this->app['storage']->getRepository('Bolt\Storage\Entity\Users')->save($userEntity);

            $this->app['logger.system']->info(
                "Password for user '{$userEntity->getUsername()}' was reset via Nut.",
                ['event' => 'authentication']
            );
        }

        return $password;
    }

    /**
     * Handle a password reset confirmation
     *
     * @param string $token
     * @param string $remoteIP
     *
     * @return boolean
     */
    public function resetPasswordConfirm($token, $remoteIP)
    {
        // Hash the remote caller's IP with the token
        $tokenHash = md5($token . '-' . str_replace('.', '-', $remoteIP));

        if ($userEntity = $this->app['storage']->getRepository('Bolt\Storage\Entity\Users')->getUserShadowAuth($tokenHash)) {
            // Update entries
            $userEntity->setPassword($user<PASSWORD>->getShadowpassword());
            $userEntity->setShadowpassword('');
            $userEntity->setShadowtoken('');
            $userEntity->setShadowvalidity(null);
            $this->app['storage']->getRepository('Bolt\Storage\Entity\Users')->save($userEntity);

            $this->app['logger.flash']->success(Trans::__('Password reset successful! You can now log on with the password that was sent to you via email.'));

            return true;
        } else {
            // That was not a valid token, or too late, or not from the correct IP.
            $this->app['logger.system']->error('Somebody tried to reset a password with an invalid token.', ['event' => 'authentication']);
            $this->app['logger.flash']->error(Trans::__('Password reset not successful! Either the token was incorrect, or you were too late, or you tried to reset the password from a different IP-address.'));

            return false;
        }
    }

    /**
     * Sends email with password request. Accepts email or username.
     *
     * @param string $username
     * @param string $remoteIP
     *
     * @return boolean
     */
    public function resetPasswordRequest($username, $remoteIP)
    {
        $userEntity = $this->app['storage']->getRepository('Bolt\Storage\Entity\Users')->getUser($username);

        if (!$userEntity) {
            // For safety, this is the message we display, regardless of whether user exists.
            $this->app['logger.flash']->info(Trans::__("A password reset link has been sent to '%user%'.", ['%user%' => $username]));

            return false;
        }

        $shadowPassword = $this->app['randomgenerator']->generateString(12);
        $shadowToken = $this->app['randomgenerator']->generateString(32);
        $shadowTokenHash = md5($shadowToken . '-' . str_replace('.', '-', $remoteIP));
        $hasher = new PasswordHash($this->hashStrength, true);
{'fraction_non_alphanumeric': 0.15161896342137252, 'fraction_numerical': 0.0019891700740413305, 'mean_word_length': 2.637459807073955, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 122, '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': '4714805', 'n_tokens_mistral': 2504, 'n_tokens_neox': 2439, 'n_words': 576}