# Path: hashview/users/forms.py
# class LoginForm(FlaskForm):
#     email = StringField('Email', validators=[DataRequired(), Email()])
#     password = PasswordField('Password', validators=[DataRequired()])
#     remember = BooleanField('Remember Me')
#     submit = SubmitField('Login')
# 
# class UsersForm(FlaskForm):
#     first_name = StringField('First Name', validators=[DataRequired(), Length(min=1, max=20)])
#     last_name = StringField('Last Name', validators=[DataRequired(), Length(min=1, max=20)])
#     email = StringField('Email', validators=[DataRequired(), Email()])
#     is_admin = BooleanField('Is Admin')
#     password = PasswordField('Password', validators=[DataRequired(), Length(min=14)])
#     confirm_password = PasswordField('Confirm Password', validators=[DataRequired(), EqualTo('password')])
#     pushover_app_id = StringField('Pushover App Token (optional)')
#     pushover_user_key = StringField('Pushover User Key (optional)')
#     submit = SubmitField('Register')
# 
#     def validate_email(self, email):
#         user = Users.query.filter_by(email_address = email.data).first()
#         if user:
#             raise ValidationError('That email address is taken. Please choose a different one.')
# 
#     def validate_pushover(self, pushover_app_id, pushover_user_key):
#         if len(pushover_app_id.data) > 0 and len(pushover_user_key.data) == 0:
#             raise ValidationError('You must supply both options to use.')
#         if len(pushover_app_id.data) == 0 and len(pushover_user_key.data) > 0:
#             raise ValidationError('You must supply both options to use.')
# 
# class ProfileForm(FlaskForm):
#     first_name = StringField('First Name', validators=[DataRequired(), Length(min=1, max=20)])
#     last_name = StringField('Last Name', validators=[DataRequired(), Length(min=1, max=20)])
#     pushover_user_key = StringField('Pushover User Key (optional)')
#     pushover_app_id = StringField('Pushover App Id (optional)')
#     submit = SubmitField('Update')
# 
# class RequestResetForm(FlaskForm):
#     email = StringField('Email', validators=[DataRequired(), Email()])
#     submit = SubmitField('Request Password Reset')
# 
# class ResetPasswordForm(FlaskForm):
#     password = PasswordField('Password', validators=[DataRequired(), Length(min=14)])
#     confirm_password = PasswordField('Confirm Password', validators=[DataRequired(), EqualTo('password')])
#     submit = SubmitField('Reset Password')
# 
# Path: hashview/utils/utils.py
# def send_email(user, subject, message):
#     msg = Message(subject, recipients=[user.email_address])
#     msg.body = message
#     mail.send(msg)
# 
# def send_pushover(user, subject, message):
#     if user.pushover_user_key and user.pushover_app_id:
#         client = Client(user.pushover_user_key, api_token=user.pushover_app_id)
#         try:
#             client.send_message(message, title=subject)
#         except:
#             send_email(user, "Error Sending Push Notification", "Check your Pushover API keys in  your profile. Original Message: " + message)
# 
# Path: hashview/models.py
# class Users(db.Model, UserMixin):
#     id = db.Column(db.Integer, primary_key=True)
#     first_name = db.Column(db.String(20), nullable=False)
#     last_name = db.Column(db.String(20), nullable=False)
#     email_address = db.Column(db.String(50), unique=True, nullable=False)
#     password = db.Column(db.String(60), nullable=False)
#     admin = db.Column(db.Boolean, nullable=False, default=False)
#     pushover_app_id = db.Column(db.String(50), nullable=True)
#     pushover_user_key = db.Column(db.String(50), nullable=True)
#     wordlists = db.relationship('Wordlists', backref='tbd', lazy=True)
#     rules = db.relationship('Rules', backref='owner', lazy=True)
#     jobs = db.relationship('Jobs', backref='owner', lazy=True)
#     tasks = db.relationship('Tasks', backref='owner', lazy=True)
#     taskgroups = db.relationship('TaskGroups', backref='owner', lazy=True)
# 
#     def get_reset_token(self, expires_sec=1800):
#         s = Serializer(Config.SECRET_KEY, expires_sec)
#         return s.dumps({'user_id': self.id}).decode('utf-8')
# 
#     @staticmethod
#     def verify_reset_token(token):
#         s = Serializer(Config.SECRET_KEY)
#         try:
#             user_id = s.loads(token)['user_id']
#         except:
#             return None     
#         return Users.query.get(user_id)
# 
# Path: hashview.py
# def data_retention_cleanup():


from flask import Blueprint, render_template, url_for, flash, abort, redirect, request
from flask_login import login_required, logout_user, current_user, login_user
from hashview.users.forms import LoginForm, UsersForm, ProfileForm, RequestResetForm, ResetPasswordForm
from hashview.utils.utils import send_email, send_pushover
from hashview.models import Users
from hashview import db, bcrypt
def reset_request():

    form = RequestResetForm()
    if form.validate_on_submit():
        user = Users.query.filter_by(email_address=form.email.data).first()
        if user:
            token = user.get_reset_token()
            subject = 'Password Reset Request.'
            message = f'''To reset your password, vist the following link:
    {url_for('users.reset_token', token=token, _external=True)}

    If you did not make this request... then something phishy is going on.
    '''
            send_email(user, subject, message)
        flash('An email has been sent to '+  form.email.data, 'info')
        return redirect(url_for('users.login')) 
    return render_template('reset_request.html', title='Reset Password', form=form)

@users.route("/admin_reset_password/<int:user_id>", methods=['GET', 'POST'])
@login_required
def admin_reset(user_id):
    if current_user.admin:
        user = Users.query.get(user_id)
        token = user.get_reset_token()
        subject = 'Password Reset Request.'
        message = f'''To reset your password, vist the following link:
{url_for('users.reset_token', token=token, _external=True)}

If you did not make this request... then something phishy is going on.
'''

# Path: hashview/users/forms.py
# class LoginForm(FlaskForm):
#     email = StringField('Email', validators=[DataRequired(), Email()])
#     password = PasswordField('Password', validators=[DataRequired()])
#     remember = BooleanField('Remember Me')
#     submit = SubmitField('Login')
# 
# class UsersForm(FlaskForm):
#     first_name = StringField('First Name', validators=[DataRequired(), Length(min=1, max=20)])
#     last_name = StringField('Last Name', validators=[DataRequired(), Length(min=1, max=20)])
#     email = StringField('Email', validators=[DataRequired(), Email()])
#     is_admin = BooleanField('Is Admin')
#     password = PasswordField('Password', validators=[DataRequired(), Length(min=14)])
#     confirm_password = PasswordField('Confirm Password', validators=[DataRequired(), EqualTo('password')])
#     pushover_app_id = StringField('Pushover App Token (optional)')
#     pushover_user_key = StringField('Pushover User Key (optional)')
#     submit = SubmitField('Register')
# 
#     def validate_email(self, email):
#         user = Users.query.filter_by(email_address = email.data).first()
#         if user:
#             raise ValidationError('That email address is taken. Please choose a different one.')
# 
#     def validate_pushover(self, pushover_app_id, pushover_user_key):
#         if len(pushover_app_id.data) > 0 and len(pushover_user_key.data) == 0:
#             raise ValidationError('You must supply both options to use.')
#         if len(pushover_app_id.data) == 0 and len(pushover_user_key.data) > 0:
#             raise ValidationError('You must supply both options to use.')
# 
# class ProfileForm(FlaskForm):
#     first_name = StringField('First Name', validators=[DataRequired(), Length(min=1, max=20)])
#     last_name = StringField('Last Name', validators=[DataRequired(), Length(min=1, max=20)])
#     pushover_user_key = StringField('Pushover User Key (optional)')
#     pushover_app_id = StringField('Pushover App Id (optional)')
#     submit = SubmitField('Update')
# 
# class RequestResetForm(FlaskForm):
#     email = StringField('Email', validators=[DataRequired(), Email()])
#     submit = SubmitField('Request Password Reset')
# 
# class ResetPasswordForm(FlaskForm):
#     password = PasswordField('Password', validators=[DataRequired(), Length(min=14)])
#     confirm_password = PasswordField('Confirm Password', validators=[DataRequired(), EqualTo('password')])
#     submit = SubmitField('Reset Password')
# 
# Path: hashview/utils/utils.py
# def send_email(user, subject, message):
#     msg = Message(subject, recipients=[user.email_address])
#     msg.body = message
#     mail.send(msg)
# 
# def send_pushover(user, subject, message):
#     if user.pushover_user_key and user.pushover_app_id:
#         client = Client(user.pushover_user_key, api_token=user.pushover_app_id)
#         try:
#             client.send_message(message, title=subject)
#         except:
#             send_email(user, "Error Sending Push Notification", "Check your Pushover API keys in  your profile. Original Message: " + message)
# 
# Path: hashview/models.py
# class Users(db.Model, UserMixin):
#     id = db.Column(db.Integer, primary_key=True)
#     first_name = db.Column(db.String(20), nullable=False)
#     last_name = db.Column(db.String(20), nullable=False)
#     email_address = db.Column(db.String(50), unique=True, nullable=False)
#     password = db.Column(db.String(60), nullable=False)
#     admin = db.Column(db.Boolean, nullable=False, default=False)
#     pushover_app_id = db.Column(db.String(50), nullable=True)
#     pushover_user_key = db.Column(db.String(50), nullable=True)
#     wordlists = db.relationship('Wordlists', backref='tbd', lazy=True)
#     rules = db.relationship('Rules', backref='owner', lazy=True)
#     jobs = db.relationship('Jobs', backref='owner', lazy=True)
#     tasks = db.relationship('Tasks', backref='owner', lazy=True)
#     taskgroups = db.relationship('TaskGroups', backref='owner', lazy=True)
# 
#     def get_reset_token(self, expires_sec=1800):
#         s = Serializer(Config.SECRET_KEY, expires_sec)
#         return s.dumps({'user_id': self.id}).decode('utf-8')
# 
#     @staticmethod
#     def verify_reset_token(token):
#         s = Serializer(Config.SECRET_KEY)
#         try:
#             user_id = s.loads(token)['user_id']
#         except:
#             return None     
#         return Users.query.get(user_id)
# 
# Path: hashview.py
# def data_retention_cleanup():


# Path: hashview/users/routes.py
from flask import Blueprint, render_template, url_for, flash, abort, redirect, request
from flask_login import login_required, logout_user, current_user, login_user
from hashview.users.forms import LoginForm, UsersForm, ProfileForm, RequestResetForm, ResetPasswordForm
from hashview.utils.utils import send_email, send_pushover
from hashview.models import Users
from hashview import db, bcrypt

def reset_request():

    form = RequestResetForm()
    if form.validate_on_submit():
        user = Users.query.filter_by(email_address=form.email.data).first()
        if user:
            token = user.get_reset_token()
            subject = 'Password Reset Request.'
            message = f'''To reset your password, vist the following link:
    {url_for('users.reset_token', token=token, _external=True)}

    If you did not make this request... then something phishy is going on.
    '''
            send_email(user, subject, message)
        flash('An email has been sent to '+  form.email.data, 'info')
        return redirect(url_for('users.login')) 
    return render_template('reset_request.html', title='Reset Password', form=form)

@users.route("/admin_reset_password/<int:user_id>", methods=['GET', 'POST'])
@login_required
def admin_reset(user_id):
    if current_user.admin:
        user = Users.query.get(user_id)
        token = user.get_reset_token()
        subject = 'Password Reset Request.'
        message = f'''To reset your password, vist the following link:
{url_for('users.reset_token', token=token, _external=True)}

If you did not make this request... then something phishy is going on.
'''

        send_email(user, subject, message)