Created
September 29, 2013 14:17
-
-
Save utek/6752902 to your computer and use it in GitHub Desktop.
Sqlalchemy usermodel with password checking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User(Base): | |
__tablename__ = 'users' | |
id = sa.Column(sa.Integer, primary_key=True) | |
username = sa.Column(sa.Unicode(255), | |
unique=True, | |
nullable=False, | |
index=True) | |
_password = sa.Column(sa.Unicode(255), nullable=False) | |
last_logged = sa.Column(sa.DateTime, default=datetime.datetime.utcnow) | |
def check_password(self, password): | |
if len(password) > 4096: | |
return False | |
pass_hash = crypt(password, self._password) | |
return self.password == pass_hash | |
@property | |
def password(self): | |
return self._password | |
@password.setter | |
def password(self, value): | |
if len(value) > 4096: | |
raise ValueError("Password too long") | |
salt = uuid.uuid4().hex | |
pass_hash = crypt(value, salt, 400) | |
self._password = pass_hash |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment