Cleanup user model.

This commit is contained in:
Alexis Lahouze 2017-05-19 16:45:55 +02:00
parent 4dfbe5dba2
commit a8672a8656

View File

@ -3,18 +3,12 @@
# vim: set tw=80 ts=4 sw=4 sts=4: # vim: set tw=80 ts=4 sw=4 sts=4:
from passlib.hash import sha256_crypt as crypt from passlib.hash import sha256_crypt as crypt
from itsdangerous import (URLSafeTimedSerializer as Serializer,
BadSignature, SignatureExpired)
from flask import current_app as app
from flask_login import UserMixin
# pylint: disable=no-member,too-few-public-methods
from . import db from . import db
class User(UserMixin, db.Model): # pylint: disable=no-member,too-few-public-methods
class User(db.Model):
"""Class used to handle users.""" """Class used to handle users."""
# pylint: disable=invalid-name # pylint: disable=invalid-name
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
@ -24,6 +18,7 @@ class User(UserMixin, db.Model):
server_default=db.true()) server_default=db.true())
def is_active(self): def is_active(self):
"""Return True if user is active."""
return self.active return self.active
@classmethod @classmethod
@ -39,24 +34,3 @@ class User(UserMixin, db.Model):
def verify_password(self, password): def verify_password(self, password):
"""Verify password passed in argument with the user's one.""" """Verify password passed in argument with the user's one."""
return crypt.verify(password, self.password) return crypt.verify(password, self.password)
def generate_auth_token(self):
"""Generate authentication token used to identify the session."""
serializer = Serializer(app.secret_key)
return serializer.dumps({'id': self.id})
@classmethod
def verify_auth_token(cls, token):
"""Verify the authentication token and return the associated user."""
serializer = Serializer(app.config['SECRET_KEY'])
try:
data = serializer.loads(token, max_age=app.config["SESSION_TTL"])
except SignatureExpired:
return None
except BadSignature:
return None
user = cls.query().get(data['id'])
return user