Skip to content

Commit

Permalink
fix the wiki2 tutorial to set the password as unicode
Browse files Browse the repository at this point in the history
Something really weird is happening but this fixes it. SQLAlchemy is
returning the "password_hash" from queries as the type that it was
inserted as. Not consistently unicode or bytes. If I insert bytes, then
I get bytes back out. If I insert unicode then I get unicode back out.
It's unclear why, as the type is Text, the data we're storing is
unambiguously US-ASCII and the connection is using a consistent
text_factory for unicode conversions of "str" on Python 3.

Here, we ensure that we always insert the value as unicode which appears
to fix downstream issues like those mentioned in Pylons#2605. I was able to
reproduce that bug and confirm this fixes it if the original database is
initialized using this fix.

Obsoletes Pylons#2623.
  • Loading branch information
mmerickel committed Jul 16, 2016
1 parent 4c3faf8 commit 79376e5
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ class User(Base):

def set_password(self, pw):
pwhash = bcrypt.hashpw(pw.encode('utf8'), bcrypt.gensalt())
self.password_hash = pwhash
self.password_hash = pwhash.decode('utf8')

def check_password(self, pw):
if self.password_hash is not None:
expected_hash = self.password_hash.encode('utf8')
actual_hash = bcrypt.hashpw(pw.encode('utf8'), expected_hash)
return expected_hash == actual_hash
return bcrypt.checkpw(pw.encode('utf8'), expected_hash)
return False
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ class User(Base):

def set_password(self, pw):
pwhash = bcrypt.hashpw(pw.encode('utf8'), bcrypt.gensalt())
self.password_hash = pwhash
self.password_hash = pwhash.decode('utf8')

def check_password(self, pw):
if self.password_hash is not None:
expected_hash = self.password_hash.encode('utf8')
actual_hash = bcrypt.hashpw(pw.encode('utf8'), expected_hash)
return expected_hash == actual_hash
return bcrypt.checkpw(pw.encode('utf8'), expected_hash)
return False
5 changes: 2 additions & 3 deletions docs/tutorials/wiki2/src/models/tutorial/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ class User(Base):

def set_password(self, pw):
pwhash = bcrypt.hashpw(pw.encode('utf8'), bcrypt.gensalt())
self.password_hash = pwhash
self.password_hash = pwhash.decode('utf8')

def check_password(self, pw):
if self.password_hash is not None:
expected_hash = self.password_hash.encode('utf8')
actual_hash = bcrypt.hashpw(pw.encode('utf8'), expected_hash)
return expected_hash == actual_hash
return bcrypt.checkpw(pw.encode('utf8'), expected_hash)
return False
5 changes: 2 additions & 3 deletions docs/tutorials/wiki2/src/tests/tutorial/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ class User(Base):

def set_password(self, pw):
pwhash = bcrypt.hashpw(pw.encode('utf8'), bcrypt.gensalt())
self.password_hash = pwhash
self.password_hash = pwhash.decode('utf8')

def check_password(self, pw):
if self.password_hash is not None:
expected_hash = self.password_hash.encode('utf8')
actual_hash = bcrypt.hashpw(pw.encode('utf8'), expected_hash)
return expected_hash == actual_hash
return bcrypt.checkpw(pw.encode('utf8'), expected_hash)
return False
5 changes: 2 additions & 3 deletions docs/tutorials/wiki2/src/views/tutorial/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ class User(Base):

def set_password(self, pw):
pwhash = bcrypt.hashpw(pw.encode('utf8'), bcrypt.gensalt())
self.password_hash = pwhash
self.password_hash = pwhash.decode('utf8')

def check_password(self, pw):
if self.password_hash is not None:
expected_hash = self.password_hash.encode('utf8')
actual_hash = bcrypt.hashpw(pw.encode('utf8'), expected_hash)
return expected_hash == actual_hash
return bcrypt.checkpw(pw.encode('utf8'), expected_hash)
return False

0 comments on commit 79376e5

Please sign in to comment.