lingthio / Flask-User

Customizable User Authorization & User Management: Register, Confirm, Login, Change username/password, Forgot password and more.
http://flask-user.readthedocs.io/
MIT License
1.06k stars 292 forks source link

Bcrypt/passlib encoding error #258

Open set321go opened 5 years ago

set321go commented 5 years ago

Hello,

I'm not entirely sure which lib/what is wrong but have been having some issues with encoding a password I keep getting TypeError: Unicode-objects must be encoded before hashing

I am passing b'Password1' (but using'Password1'.encode()or'Password1'.encode('utf-8')does not change the result.print(password)` reports that the string is bytes.

I dug into the backtrace and printed out the password and salt from the bycrypt hashpw function. That password string is 'test'. Following this back this is defined in passlib/handlers/ bycrypt.py:L374

Edit 1: I should mention that I am trying to call current_app.user_manager.password_manager.hash_password(password)

Any idea what's going on here?

Python

Python 3.7.2

Flask-Migrate==2.3.1
  alembic==1.0.7
    Mako==1.0.7
      MarkupSafe==1.1.0
    python-dateutil==2.7.5
      six==1.12.0
    python-editor==1.0.3
    SQLAlchemy==1.2.17
  Flask==1.0.2
    Click==7.0
    itsdangerous==1.1.0
    Jinja2==2.10
      MarkupSafe==1.1.0
    Werkzeug==0.14.1
  Flask-SQLAlchemy==2.3.2
    Flask==1.0.2
      Click==7.0
      itsdangerous==1.1.0
      Jinja2==2.10
        MarkupSafe==1.1.0
      Werkzeug==0.14.1
    SQLAlchemy==1.2.17
Flask-Script==2.0.6
  Flask==1.0.2
    Click==7.0
    itsdangerous==1.1.0
    Jinja2==2.10
      MarkupSafe==1.1.0
    Werkzeug==0.14.1
Flask-User==1.0.1.5
  bcrypt==3.1.6
    cffi==1.11.5
      pycparser==2.19
    six==1.12.0
  cryptography==2.5
    asn1crypto==0.24.0
    cffi==1.11.5
      pycparser==2.19
    six==1.12.0
  Flask==1.0.2
    Click==7.0
    itsdangerous==1.1.0
    Jinja2==2.10
      MarkupSafe==1.1.0
    Werkzeug==0.14.1
  Flask-Login==0.4.1
    Flask==1.0.2
      Click==7.0
      itsdangerous==1.1.0
      Jinja2==2.10
        MarkupSafe==1.1.0
      Werkzeug==0.14.1
  Flask-Mail==0.9.1
    blinker==1.4
    Flask==1.0.2
      Click==7.0
      itsdangerous==1.1.0
      Jinja2==2.10
        MarkupSafe==1.1.0
      Werkzeug==0.14.1
  Flask-SQLAlchemy==2.3.2
    Flask==1.0.2
      Click==7.0
      itsdangerous==1.1.0
      Jinja2==2.10
        MarkupSafe==1.1.0
      Werkzeug==0.14.1
    SQLAlchemy==1.2.17
  Flask-WTF==0.14.2
    Flask==1.0.2
      Click==7.0
      itsdangerous==1.1.0
      Jinja2==2.10
        MarkupSafe==1.1.0
      Werkzeug==0.14.1
    WTForms==2.2.1
  passlib==1.7.1
pipdeptree==0.13.2
  pip==18.1
pipenv==2018.11.26
  certifi==2018.11.29
  pip==18.1
  setuptools==40.6.2
  virtualenv==16.3.0
    setuptools==40.6.2
  virtualenv-clone==0.5.1
psycopg2==2.7.7
psycopg2-binary==2.7.7
py-bcrypt==0.4
pycrypto==2.6.1
pycryptodome==3.7.3
pytest-cov==2.4.0
  coverage==4.5.2
  pytest==3.0.5
    py==1.7.0

(i've made the paths relative to the venv in the project dir)

b'test'
$2$04$5BJqKfqMQvV7nS.yUguNcu
Traceback (most recent call last):
  File "app_env/bin/flask", line 11, in <module>
    sys.exit(main())
  File "app_env/lib/python3.7/site-packages/flask/cli.py", line 894, in main
    cli.main(args=args, prog_name=name)
  File "app_env/lib/python3.7/site-packages/flask/cli.py", line 557, in main
    return super(FlaskGroup, self).main(*args, **kwargs)
  File "app_env/lib/python3.7/site-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "app_env/lib/python3.7/site-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "app_env/lib/python3.7/site-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "app_env/lib/python3.7/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "app_env/lib/python3.7/site-packages/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "app_env/lib/python3.7/site-packages/flask/cli.py", line 412, in decorator
    return __ctx.invoke(f, *args, **kwargs)
  File "app_env/lib/python3.7/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "presale/__init__.py", line 73, in seed_data
    seed_db()
  File "presale/commands/init_db.py", line 19, in seed_db
    create_users()
  File "presale/commands/init_db.py", line 30, in create_users
    user = find_or_create_user(u'Admin', u'Example', u'admin@example.com', b'Password1', admin_role)
  File "presale/commands/init_db.py", line 82, in find_or_create_user
    print(current_app.user_manager.password_manager.hash_password(password))
  File "app_env/lib/python3.7/site-packages/flask_user/password_manager.py", line 50, in hash_password
    password_hash = self.password_crypt_context.encrypt(password)
  File "app_env/lib/python3.7/site-packages/passlib/utils/decor.py", line 191, in wrapper
    return func(*args, **kwds)
  File "app_env/lib/python3.7/site-packages/passlib/context.py", line 2265, in encrypt
    return self.hash(*args, **kwds)
  File "app_env/lib/python3.7/site-packages/passlib/context.py", line 2253, in hash
    return record.hash(secret, **kwds)
  File "app_env/lib/python3.7/site-packages/passlib/utils/handlers.py", line 748, in hash
    self.checksum = self._calc_checksum(secret)
  File "app_env/lib/python3.7/site-packages/passlib/handlers/bcrypt.py", line 530, in _calc_checksum
    self._stub_requires_backend()
  File "app_env/lib/python3.7/site-packages/passlib/utils/handlers.py", line 2221, in _stub_requires_backend
    cls.set_backend()
  File "app_env/lib/python3.7/site-packages/passlib/utils/handlers.py", line 2123, in set_backend
    return owner.set_backend(name, dryrun=dryrun)
  File "app_env/lib/python3.7/site-packages/passlib/utils/handlers.py", line 2130, in set_backend
    return cls.set_backend(name, dryrun=dryrun)
  File "app_env/lib/python3.7/site-packages/passlib/utils/handlers.py", line 2155, in set_backend
    cls._set_backend(name, dryrun)
  File "app_env/lib/python3.7/site-packages/passlib/utils/handlers.py", line 2278, in _set_backend
    super(SubclassBackendMixin, cls)._set_backend(name, dryrun)
  File "app_env/lib/python3.7/site-packages/passlib/utils/handlers.py", line 2191, in _set_backend
    ok = loader(**kwds)
  File "app_env/lib/python3.7/site-packages/passlib/handlers/bcrypt.py", line 671, in _load_backend_mixin
    return mixin_cls._finalize_backend_mixin(name, dryrun)
  File "app_env/lib/python3.7/site-packages/passlib/handlers/bcrypt.py", line 374, in _finalize_backend_mixin
    result = safe_verify("test", test_hash_20)
  File "app_env/lib/python3.7/site-packages/passlib/handlers/bcrypt.py", line 293, in safe_verify
    return verify(secret, hash)
  File "app_env/lib/python3.7/site-packages/passlib/utils/handlers.py", line 761, in verify
    return consteq(self._calc_checksum(secret), chk)
  File "app_env/lib/python3.7/site-packages/passlib/handlers/bcrypt.py", line 688, in _calc_checksum_raw
    hash = _pybcrypt.hashpw(secret, config)
  File "app_env/lib/python3.7/site-packages/bcrypt/__init__.py", line 63, in hashpw
    raise TypeError("Unicode-objects must be encoded before hashing")
TypeError: Unicode-objects must be encoded before hashing