# Path: webphamerator/app/sqlalchemy_ext/models.py
# class Database(db.Model):
# class Job(db.Model):
# class JobOrganismToDelete(db.Model):
# class GenbankFile(db.Model):
# class Password(db.Model):
#     def url(self):
#     def mysql_name_for(cls, name):
#     def phamerator_name_for(cls, name):
#     def mysql_name(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):


import os
import binascii
from flask import Blueprint, request, redirect, session
from webphamerator.app.sqlalchemy_ext import models
from backports.pbkdf2 import pbkdf2_hmac, compare_digest
def require_authentication():
    """Require authentication on most routes.
    """
    for path in AUTHENTICATION_NOT_NEEDED:
        if request.path.startswith(path):
            return

    if not is_authenticated() and is_password_required():
        return redirect('/signin')


def is_authenticated():
    if session.get("authenticated"):
        return True


def is_password_required():
    return models.Password.query.count() > 0


def set_password(password):
    salt = binascii.hexlify(os.urandom(16))
    hex_digest = binascii.hexlify(digest(password, salt))

    passwords = models.Password.query.all()
    for item in passwords:
        models.db.session.delete(item)

    new_item = models.Password(digest_key=hex_digest, salt=salt)
    models.db.session.add(new_item)

# Path: webphamerator/app/sqlalchemy_ext/models.py
# class Database(db.Model):
# class Job(db.Model):
# class JobOrganismToDelete(db.Model):
# class GenbankFile(db.Model):
# class Password(db.Model):
#     def url(self):
#     def mysql_name_for(cls, name):
#     def phamerator_name_for(cls, name):
#     def mysql_name(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):


# Path: webphamerator/app/auth.py
import os
import binascii
from flask import Blueprint, request, redirect, session
from webphamerator.app.sqlalchemy_ext import models
from backports.pbkdf2 import pbkdf2_hmac, compare_digest

def require_authentication():
    """Require authentication on most routes.
    """
    for path in AUTHENTICATION_NOT_NEEDED:
        if request.path.startswith(path):
            return

    if not is_authenticated() and is_password_required():
        return redirect('/signin')


def is_authenticated():
    if session.get("authenticated"):
        return True


def is_password_required():
    return models.Password.query.count() > 0


def set_password(password):
    salt = binascii.hexlify(os.urandom(16))
    hex_digest = binascii.hexlify(digest(password, salt))

    passwords = models.Password.query.all()
    for item in passwords:
        models.db.session.delete(item)

    new_item = models.Password(digest_key=hex_digest, salt=salt)
    models.db.session.add(new_item)

    models.db.session.commit()