Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix password is not bytes in mysql #55

Merged
merged 3 commits into from
Feb 14, 2019

Conversation

00Kai0
Copy link
Contributor

@00Kai0 00Kai0 commented Feb 12, 2019

Hi @leportella . I use mysql database as backend.
The log:

 HTTPServerRequest(protocol='http', host='localhost:8000', method='POST', uri='/hub/login?next=', version='HTTP/1.1', remote_ip='::ffff:127.0.0.1')
    Traceback (most recent call last):
      File "/home/kai/work/embark3_env/venv/lib/python3.5/site-packages/tornado/web.py", line 1592, in _execute
        result = yield result
      File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
        raise self._exception
      File "/home/kai/work/embark3_env/venv/lib/python3.5/site-packages/jupyterhub/handlers/login.py", line 82, in post
        user = yield self.login_user(data)
      File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
        raise self._exception
      File "/home/kai/work/embark3_env/venv/lib/python3.5/site-packages/jupyterhub/handlers/base.py", line 327, in login_user
        authenticated = yield self.authenticate(data)
      File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
        raise self._exception
      File "/home/kai/work/embark3_env/venv/lib/python3.5/site-packages/jupyterhub/auth.py", line 221, in get_authenticated_user
        authenticated = yield self.authenticate(handler, data)
      File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
        raise self._exception
      File "/home/kai/work/embark3_env/venv/lib/python3.5/types.py", line 243, in wrapped
        coro = func(*args, **kwargs)
      File "/home/kai/work/embark3_env/nativeauthenticator/nativeauthenticator/nativeauthenticator.py", line 114, in authenticate
        if user.is_authorized and user.is_valid_password(password):
      File "/home/kai/work/embark3_env/nativeauthenticator/nativeauthenticator/orm.py", line 26, in is_valid_password
        encoded_pw = bcrypt.hashpw(password.encode(), self.password)
      File "/home/kai/work/embark3_env/venv/lib/python3.5/site-packages/bcrypt/__init__.py", line 61, in hashpw
        raise TypeError("Unicode-objects must be encoded before hashing")
    TypeError: Unicode-objects must be encoded before hashing

The issue is that mysql can not save password as bytes although the return of bcrypt.hashpw is bytes in nativeauthenticator.py

encoded_pw = bcrypt.hashpw(pw.encode(), bcrypt.gensalt())
infos = {'username': username, 'password': encoded_pw}
infos.update(kwargs)

@yuvipanda
Copy link
Contributor

Thanks for opening this, @00Kai0.

I think the problem is that the password hash is a binary byte string but we're storing it as string. I think the right solution is to change the type of the password column in https://github.com/jupyterhub/nativeauthenticator/blob/master/nativeauthenticator/orm.py#L13 to https://docs.sqlalchemy.org/en/latest/core/type_basics.html#sqlalchemy.types.BINARY or LARGEBINARY.

@yuvipanda
Copy link
Contributor

This is not a problem for sqlite because it doesn't really enforce types, but would be for most other databases.

@00Kai0
Copy link
Contributor Author

00Kai0 commented Feb 13, 2019

@yuvipanda , I think it works.

@leportella leportella merged commit 9a243eb into jupyterhub:master Feb 14, 2019
@leportella
Copy link
Collaborator

Thank you @00Kai0 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants