Skip to content

Instantly share code, notes, and snippets.

@utek
Created September 29, 2013 14:17
Show Gist options
  • Save utek/6752902 to your computer and use it in GitHub Desktop.
Save utek/6752902 to your computer and use it in GitHub Desktop.
Sqlalchemy usermodel with password checking
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