<commit_before><?php
namespace Hook\Model;

/**
 * Auth
 *
 * @uses Collection
 */
class Auth extends Collection
{
    const FORGOT_PASSWORD_FIELD = '<PASSWORD>_password_token';
    const FORGOT_PASSWORD_EXPIRATION_FIELD = 'forgot_password_expiration';
    const FORGOT_PASSWORD_EXPIRATION_TIME = 14400; // (60 * 60 * 4) = 4 hours

    protected $table = 'auths';

    // protect from mass-assignment.
    protected $guarded = array('email', 'password', 'password_salt', 'forgot_password_token', 'forgot_password_expiration', 'deleted_at');
    protected $hidden = array('password', 'password_salt', 'forgot_password_token', 'forgot_password_expiration', 'deleted_at');

    static $_current = null;

    /**
     * current - get current active Auth instance
     * @static
     * @return Auth|null
     */
    public static function current()
    {
        if (static::$_current === null) {
            if ($token = AuthToken::current()) {
                static::$_current = $token->auth;
            }
        }

        return static::$_current;
    }

    public function tokens()
    {
        return $this->hasMany('Hook\Model\AuthToken', 'auth_id');
    }

    /**
     * generateToken
     * @return AuthToken
     */
    public function generateToken()
    {
        return $this->tokens()->create(array());
    }

    public function generateForgotPasswordToken()
    {
        $this->setAttribute(self::FORGOT_PASSWORD_FIELD, sha1(uniqid(rand(), true)));
        $this->setAttribute(self::FORGOT_PASSWORD_EXPIRATION_FIELD, time() + self::FORGOT_PASSWORD_EXPIRATION_TIME);
        $this->save();

        return $this;
    }

    /**
     * resetPassword
     * @param  mixed $newPassword <PASSWORD>
     * @return void
     */
    public function resetPassword($newPassword)
    {
        $success = false;
        if (!$this->isForgotPasswordTokenExpired()) {
            $this->password = $<PASSWORD>;
            $this->setAttribute(self::FORGOT_PASSWORD_EXPIRATION_FIELD, time()); // expire token
            $success = $this->save();
        }

        return $success;
    }

    protected function isForgotPasswordTokenExpired()
    {
        return time() > $this->getAttribute(self::FORGOT_PASSWORD_EXPIRATION_FIELD);
    }

    public function toArray()
    {
        $arr = parent::toArray();

        // only display email for the logged user
        $auth_token = AuthToken::current();
        if (!$auth_token || $auth_token->auth_id != $this->_id) {
            unset($arr['email']);
        }

        return $arr;
    }

    /**
     * dataWithToken
     * @return array
     */
    public function dataWithToken()
    {
        $data = $this->toArray();
        $data['token'] = $this->generateToken()->toArray();

        return $data;
    }

    //
    // Hooks
    //

    public function beforeSave()
    {
        if ($this->isDirty('password')) {
            $this->password_salt = sha1(uniqid(rand(), true));
            $this->password = static::password_hash($this->password, $this->password_salt);
        }
        return parent::beforeSave();
    }

    public static function password_hash($password, $salt)
    {
        $app_auth_pepper = AppConfig::get('auth_pepper') ?: '';
        return sha1($password . $salt . $app_auth_pepper);
    }

}<commit_msg>add comments to Auth::password_hash.<commit_after><?php
namespace Hook\Model;

/**
 * Auth
 *
 * @uses Collection
 */
class Auth extends Collection
{
    const FORGOT_PASSWORD_FIELD = '<PASSWORD>';
    const FORGOT_PASSWORD_EXPIRATION_FIELD = '<PASSWORD>';
    const FORGOT_PASSWORD_EXPIRATION_TIME = 14400; // (60 * 60 * 4) = 4 hours

    protected $table = 'auths';

    // protect from mass-assignment.
    protected $guarded = array('email', 'password', 'password_salt', 'forgot_password_token', 'forgot_password_expiration', 'deleted_at');
    protected $hidden = array('password', 'password_salt', 'forgot_password_token', 'forgot_password_expiration', 'deleted_at');

    static $_current = null;

    /**
     * current - get current active Auth instance
     * @static
     * @return Auth|null
     */
    public static function current()
    {
        if (static::$_current === null) {
            if ($token = AuthToken::current()) {
                static::$_current = $token->auth;
            }
        }

        return static::$_current;
    }

    public function tokens()
    {
        return $this->hasMany('Hook\Model\AuthToken', 'auth_id');
    }

    /**
     * generateToken
     * @return AuthToken
     */
    public function generateToken()
    {
        return $this->tokens()->create(array());
    }

    public function generateForgotPasswordToken()
    {
        $this->setAttribute(self::FORGOT_PASSWORD_FIELD, sha1(uniqid(rand(), true)));
        $this->setAttribute(self::FORGOT_PASSWORD_EXPIRATION_FIELD, time() + self::FORGOT_PASSWORD_EXPIRATION_TIME);
        $this->save();

        return $this;
    }

    /**
     * resetPassword
     * @param  mixed $newPassword <PASSWORD>
     * @return void
     */
    public function resetPassword($newPassword)
    {
        $success = false;
        if (!$this->isForgotPasswordTokenExpired()) {
            $this->password = $<PASSWORD>;
            $this->setAttribute(self::FORGOT_PASSWORD_EXPIRATION_FIELD, time()); // expire token
            $success = $this->save();
        }

        return $success;
    }

    protected function isForgotPasswordTokenExpired()
    {
        return time() > $this->getAttribute(self::FORGOT_PASSWORD_EXPIRATION_FIELD);
    }

    public function toArray()
    {
        $arr = parent::toArray();

        // only display email for the logged user
        $auth_token = AuthToken::current();
        if (!$auth_token || $auth_token->auth_id != $this->_id) {
            unset($arr['email']);
        }

        return $arr;
    }

    /**
     * dataWithToken
     * @return array
     */
    public function dataWithToken()
    {
        $data = $this->toArray();
        $data['token'] = $this->generateToken()->toArray();

        return $data;
    }

    //
    // Hooks
    //

    public function beforeSave()
    {
        if ($this->isDirty('password')) {
            $this->password_salt = sha1(uniqid(rand(), true));
            $this->password = static::password_hash($this->password, $this->password_salt);
        }
        return parent::beforeSave();
    }

    /**
     * Generate sha1 hash of a password, using 'salt' and 'pepper' (AppConfig)
     *
     * @static
     *
     * @param string $password
     * @param string $salt
     *
     * @return string
     */
    public static function password_hash($password, $salt)
    {
        $app_auth_pepper = AppConfig::get('auth_pepper') ?: '';
        return sha1($password . $salt . $app_auth_pepper);
    }

}
{'fraction_non_alphanumeric': 0.1409672203439659, 'fraction_numerical': 0.004262825224165809, 'mean_word_length': 2.4433198380566803, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 56, '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': '3173622', 'n_tokens_mistral': 2093, 'n_tokens_neox': 2008, 'n_words': 393}