6

I'm having a problem using bcrypt with my Flask application on Heroku. When I deploy to Heroku and go to the login route I get 500 Internal server error. It works correctly locally. How do I get the bcrypt package working on Heroku?

ERROR in app: Exception on /login [POST]
Traceback (most recent call last):
  File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1639, in full_dispatch_request
    rv = self.dispatch_request()
  File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1625, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/app/.heroku/python/lib/python2.7/site-packages/flask_restful/__init__.py", line 477, in wrapper
    resp = resource(*args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/flask/views.py", line 84, in view
    return self.dispatch_request(*args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/flask_restful/__init__.py", line 587, in dispatch_request
    resp = meth(*args, **kwargs)
  File "/app/app.py", line 196, in post
    elif bcrypt.check_password_hash(user.password, password):
  File "/app/.heroku/python/lib/python2.7/site-packages/flask_bcrypt.py", line 193, in check_password_hash
    return safe_str_cmp(bcrypt.hashpw(password, pw_hash), pw_hash)
  File "/app/.heroku/python/lib/python2.7/site-packages/bcrypt/__init__.py", line 82, in hashpw
    hashed = _bcrypt.ffi.new("char[]", 128)
AttributeError: 'module' object has no attribute 'ffi'
davidism
  • 121,510
  • 29
  • 395
  • 339
Jean Silva
  • 141
  • 1
  • 7

5 Answers5

7

I encountered a similar issue. Here is a copy of the last part of my stack trace:

  self.password = User.hashed_password(password) 
File "/app/application/models.py", line 16, in hashed_password 
File "/app/.heroku/python/lib/python3.5/site-packages/flask_bcrypt.py", line 163, in generate_password_hash 
File "/app/.heroku/python/lib/python3.5/site-packages/bcrypt/__init__.py", line 50, in gensalt 
  output = _bcrypt.ffi.new("unsigned char[]", 30) 
AttributeError: module 'bcrypt._bcrypt' has no attribute 'ffi' 

I'm wondering if this issue is particular to Heroku. I was using some existing Flask boilerplate. But this issue with Bcrypt has also happened to me in previous projects when using a (different) boilerplate Flask project on Heroku.

Possible Solution 1

Play around with different dependency combinations. In one case, the issue went away when I included cryptography in my requirements.txt. But as Jean Silva had mentioned in this thread, it is possible that dependencies could be in conflict. So you might want to play with different combinations until something works.

Possible Solution 2

If using Flask, try having the werkzeug.security package/module to hash / check hashes as opposed to using the bcrypt package directly. In example below in my models.py, commenting out such lines and adding new ones solved the issue for me.

# from index import db, bcrypt
from index import db
from werkzeug.security import generate_password_hash, check_password_hash


class User(db.Model):
    id = db.Column(db.Integer(), primary_key=True)
    email = db.Column(db.String(255), unique=True)
    password = db.Column(db.String(255))

    def __init__(self, email, password):
        self.email = email
        self.active = True
        self.password = User.hashed_password(password)

    @staticmethod
    def hashed_password(password):
        # return bcrypt.generate_password_hash(password)
        return generate_password_hash(password)

    @staticmethod
    def get_user_with_email_and_password(email, password):
        user = User.query.filter_by(email=email).first()
        # if user and bcrypt.check_password_hash(user.password, password):
        if user and check_password_hash(user.password, password):
            return user
        else:
            return None
Joe Flack
  • 866
  • 8
  • 14
5

By installing bcrypt==3.1.2 it's work for me

pip install bcrypt==3.1.2
Manjunath Raddi
  • 421
  • 6
  • 15
3

I've found the solution, I was using the following packages: bcrypt, flask_bcrypt and py-crypt. So I uninstall the py-bcrypt, probably this package was in conflict with bcrypt package.

pip uninstall py-bcrypt
Jean Silva
  • 141
  • 1
  • 7
  • 2
    are you aware of any other packages that could be causing this issue? I do not have `py-bcrypt` installed but am also facing the same error message. Thanks – archienorman Jan 25 '17 at 11:26
2

uninstall both py-bcrypt and bcrypt is you installed it previously. Then install py-bcrypt afresh.

pip install py-bcrypt

Jeff C
  • 319
  • 3
  • 9
  • Ya, this worked for me. I installed `bcrypt` first, didn't work as I wanted, then added `py-bcrypt` and removed `bcrypt`. I guess it didn't like that, but once I uninstalled and reinstalled `py-bcrypt`, it all worked. – mrchestnut Dec 21 '19 at 00:22
0

With Python 3.7, below is the sequence of commands that resolved the error in my case:

pip uninstall py-bcrypt && pip uninstall flask-bcrypt && pip uninstall bcrypt && pip install flask-bcrypt
Alexz
  • 741
  • 1
  • 8
  • 20