# Path: allchat/database/sql.py
# def get_session():
#     return db.session
# 
# Path: allchat/database/models.py
# class UserAuth(db.Model):
#     __tablename__ = "userauth"
#     __table_args__ = {
#         'mysql_engine': 'InnoDB',
#         'mysql_charset': 'utf8',
#         'mysql_collate': 'utf8_bin'
#     }
# 
#     id = db.Column(db.Integer, primary_key = True)
#     account = db.Column(db.String(50), index = True, unique = True, nullable = False)
#     password = db.Column(db.String(64), nullable = False)
#     salt = db.Column(db.String(16), nullable = False)
#     prev_token = db.Column(db.String(32))
#     token = db.Column(db.String(32))
#     created = db.Column(db.DateTime(timezone = True), nullable = False)
#     updated = db.Column(db.DateTime(timezone = True), nullable = False)
#     deleted = db.Column(db.Boolean, nullable = False, default = False)
# 
# 
#     def __init__(self, account, password):
#         self.account = account
#         self.salt = ''.join(random.sample(string.ascii_letters + string.digits, 8))
#         self.password = hashlib.sha256((self.salt+password).encode("utf-8")).hexdigest()
#         self.token = None
#         self.prev_token = None
#         self.created = datetime.datetime.utcnow()
#         self.updated = self.created
#         self.deleted = False
# 
#     def is_authenticated(self, password):
#         if self.deleted:
#             return False
#         if self.password == hashlib.sha256((self.salt+password).encode("utf-8")).hexdigest():
#             # db.session.begin(subtransactions=True)
#             # try:
#             #     self.prev_token = self.token
#             #     self.token = ''.join(random.sample(string.ascii_letters + string.digits, 32))
#             #     self.updated = datetime.datetime.utcnow()
#             #     db.session.add(self)
#             #     db.session.commit()
#             # except:
#             #     db.session.rollback()
#             #     return False
#             return True
#         else:
#             return False
#     def is_token(self, token):
#         if self.deleted or (token is None):
#             return False
#         if token == self.token:
#             return True
#         else:
#             return False
#     def is_prev_token(self, token):
#         if self.deleted or (token is None):
#             return False
#         if token == self.prev_token:
#             return True
#         else:
#             return False
#     def is_token_timeout(self):
#         now = datetime.datetime.utcnow()
#         return (now - self.updated).seconds >= 86400
#     def fresh(self, token = None):
#         if (self.deleted == False) and (self.is_token(token) or token is None):
#             db.session.begin(subtransactions=True)
#             try:
#                 self.prev_token = self.token
#                 self.token = ''.join(random.sample(string.ascii_letters + string.digits, 32))
#                 self.updated = datetime.datetime.utcnow()
#                 db.session.add(self)
#                 db.session.commit()
#             except:
#                 db.session.rollback()
#                 return False
#         return True
# 
#     def clear(self):
#         db.session.begin(subtransactions=True)
#         try:
#             self.prev_token = self.token
#             self.token = None
#             self.updated = datetime.datetime.utcnow()
#             db.session.add(self)
#             db.session.commit()
#         except Exception:
#             db.session.rollback()
#             return False
#     def modify(self, password, new):
#         if self.is_authenticated(password):
#             db.session.begin(subtransactions=True)
#             try:
#                 self.password = hashlib.sha256((self.salt+new).encode("utf-8")).hexdigest()
#                 self.prev_token = None
#                 self.token = ''.join(random.sample(string.ascii_letters + string.digits, 32))
#                 self.updated = datetime.datetime.utcnow()
#                 db.session.add(self)
#                 db.session.commit()
#             except Exception:
#                 db.session.rollback()
#                 return False
#             return True
#         else:
#             return False
#     def delete(self):
#         if self.deleted:
#             return True
#         db.session.begin(subtransactions=True)
#         try:
#             self.deleted = True
#             self.prev_token = None
#             self.token = None
#             self.updated = datetime.datetime.utcnow()
#             db.session.add(self)
#             db.session.commit()
#         except Exception:
#             db.session.rollback()
#             return False
#         return True
# 
# 
#     def activate(self, password = None):
#         if not self.deleted:
#             return True
#         if password is None:
#             db.session.begin(subtransactions=True)
#             try:
#                 self.deleted = False
#                 self.prev_token = None
#                 self.token = ''.join(random.sample(string.ascii_letters + string.digits, 32))
#                 self.updated = datetime.datetime.utcnow()
#                 db.session.add(self)
#                 db.session.commit()
#             except:
#                 db.session.rollback()
#                 return False
#             return True
#         if password == hashlib.sha256((self.salt+password).encode("utf-8")).hexdigest():
#             db.session.begin(subtransactions=True)
#             try:
#                 self.deleted = False
#                 self.prev_token = None
#                 self.token = ''.join(random.sample(string.ascii_letters + string.digits, 32))
#                 self.updated = datetime.datetime.utcnow()
#                 db.session.add(self)
#                 db.session.commit()
#             except:
#                 db.session.rollback()
#                 return False
#         else:
#             return False
#         return True


from flask import request, make_response, session
from allchat.database.sql import get_session
from allchat.database.models import UserAuth
from functools import wraps
# -*- coding: utf-8 -*-


def authorized(func):
    @wraps(func)
    def action(*args, **kwargs):
        token = request.headers.get('token', None)
        account = session.get('account', None)
        if token and account:
            db_session = get_session()
            try:

# Path: allchat/database/sql.py
# def get_session():
#     return db.session
# 
# Path: allchat/database/models.py
# class UserAuth(db.Model):
#     __tablename__ = "userauth"
#     __table_args__ = {
#         'mysql_engine': 'InnoDB',
#         'mysql_charset': 'utf8',
#         'mysql_collate': 'utf8_bin'
#     }
# 
#     id = db.Column(db.Integer, primary_key = True)
#     account = db.Column(db.String(50), index = True, unique = True, nullable = False)
#     password = db.Column(db.String(64), nullable = False)
#     salt = db.Column(db.String(16), nullable = False)
#     prev_token = db.Column(db.String(32))
#     token = db.Column(db.String(32))
#     created = db.Column(db.DateTime(timezone = True), nullable = False)
#     updated = db.Column(db.DateTime(timezone = True), nullable = False)
#     deleted = db.Column(db.Boolean, nullable = False, default = False)
# 
# 
#     def __init__(self, account, password):
#         self.account = account
#         self.salt = ''.join(random.sample(string.ascii_letters + string.digits, 8))
#         self.password = hashlib.sha256((self.salt+password).encode("utf-8")).hexdigest()
#         self.token = None
#         self.prev_token = None
#         self.created = datetime.datetime.utcnow()
#         self.updated = self.created
#         self.deleted = False
# 
#     def is_authenticated(self, password):
#         if self.deleted:
#             return False
#         if self.password == hashlib.sha256((self.salt+password).encode("utf-8")).hexdigest():
#             # db.session.begin(subtransactions=True)
#             # try:
#             #     self.prev_token = self.token
#             #     self.token = ''.join(random.sample(string.ascii_letters + string.digits, 32))
#             #     self.updated = datetime.datetime.utcnow()
#             #     db.session.add(self)
#             #     db.session.commit()
#             # except:
#             #     db.session.rollback()
#             #     return False
#             return True
#         else:
#             return False
#     def is_token(self, token):
#         if self.deleted or (token is None):
#             return False
#         if token == self.token:
#             return True
#         else:
#             return False
#     def is_prev_token(self, token):
#         if self.deleted or (token is None):
#             return False
#         if token == self.prev_token:
#             return True
#         else:
#             return False
#     def is_token_timeout(self):
#         now = datetime.datetime.utcnow()
#         return (now - self.updated).seconds >= 86400
#     def fresh(self, token = None):
#         if (self.deleted == False) and (self.is_token(token) or token is None):
#             db.session.begin(subtransactions=True)
#             try:
#                 self.prev_token = self.token
#                 self.token = ''.join(random.sample(string.ascii_letters + string.digits, 32))
#                 self.updated = datetime.datetime.utcnow()
#                 db.session.add(self)
#                 db.session.commit()
#             except:
#                 db.session.rollback()
#                 return False
#         return True
# 
#     def clear(self):
#         db.session.begin(subtransactions=True)
#         try:
#             self.prev_token = self.token
#             self.token = None
#             self.updated = datetime.datetime.utcnow()
#             db.session.add(self)
#             db.session.commit()
#         except Exception:
#             db.session.rollback()
#             return False
#     def modify(self, password, new):
#         if self.is_authenticated(password):
#             db.session.begin(subtransactions=True)
#             try:
#                 self.password = hashlib.sha256((self.salt+new).encode("utf-8")).hexdigest()
#                 self.prev_token = None
#                 self.token = ''.join(random.sample(string.ascii_letters + string.digits, 32))
#                 self.updated = datetime.datetime.utcnow()
#                 db.session.add(self)
#                 db.session.commit()
#             except Exception:
#                 db.session.rollback()
#                 return False
#             return True
#         else:
#             return False
#     def delete(self):
#         if self.deleted:
#             return True
#         db.session.begin(subtransactions=True)
#         try:
#             self.deleted = True
#             self.prev_token = None
#             self.token = None
#             self.updated = datetime.datetime.utcnow()
#             db.session.add(self)
#             db.session.commit()
#         except Exception:
#             db.session.rollback()
#             return False
#         return True
# 
# 
#     def activate(self, password = None):
#         if not self.deleted:
#             return True
#         if password is None:
#             db.session.begin(subtransactions=True)
#             try:
#                 self.deleted = False
#                 self.prev_token = None
#                 self.token = ''.join(random.sample(string.ascii_letters + string.digits, 32))
#                 self.updated = datetime.datetime.utcnow()
#                 db.session.add(self)
#                 db.session.commit()
#             except:
#                 db.session.rollback()
#                 return False
#             return True
#         if password == hashlib.sha256((self.salt+password).encode("utf-8")).hexdigest():
#             db.session.begin(subtransactions=True)
#             try:
#                 self.deleted = False
#                 self.prev_token = None
#                 self.token = ''.join(random.sample(string.ascii_letters + string.digits, 32))
#                 self.updated = datetime.datetime.utcnow()
#                 db.session.add(self)
#                 db.session.commit()
#             except:
#                 db.session.rollback()
#                 return False
#         else:
#             return False
#         return True


# Path: allchat/authentication.py
from flask import request, make_response, session
from allchat.database.sql import get_session
from allchat.database.models import UserAuth
from functools import wraps

# -*- coding: utf-8 -*-


def authorized(func):
    @wraps(func)
    def action(*args, **kwargs):
        token = request.headers.get('token', None)
        account = session.get('account', None)
        if token and account:
            db_session = get_session()
            try:

                auth = db_session.query(UserAuth).filter(UserAuth.account == account).one()