Search code examples
pythonauthenticationflaskeve

Python Eve Auth can't return 401 exceptions


I'm running the demo code of Python Eve and I'm having troubles with the Auth part. The code I'm running is identical as the repo, except for MongoDB URI in settings.py.

# We want to seamlessy run our API both locally and on Heroku. If running on
# Heroku, sensible DB connection settings are stored in environment variables.
MONGO_URI = 'mongodb://****:****@localhost:27017/admin'
MONGO_DBNAME = 'apitest'

Everything works fine if I put correct credentials in Basic Auth (which are user=admin passwd=secret) but if I don't put Authorization header or correct credentials I don't get a 401, as I would expect, but the application crashes returning 500. This is the traceback.

[2019-03-28 12:55:32,082] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
  File "/home/biscas/code/Tools/eve-demo/venv/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/biscas/code/Tools/eve-demo/venv/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/biscas/code/Tools/eve-demo/venv/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/biscas/code/Tools/eve-demo/venv/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/home/biscas/code/Tools/eve-demo/venv/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/biscas/code/Tools/eve-demo/venv/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/biscas/code/Tools/eve-demo/venv/lib/python3.6/site-packages/eve/methods/common.py", line 317, in rate_limited
    return f(*args, **kwargs)
  File "/home/biscas/code/Tools/eve-demo/venv/lib/python3.6/site-packages/eve/auth.py", line 79, in decorated
    return auth.authenticate()
  File "/home/biscas/code/Tools/eve-demo/venv/lib/python3.6/site-packages/eve/auth.py", line 152, in authenticate
    abort(401, description="Please provide proper credentials", response=resp)
  File "/home/biscas/code/Tools/eve-demo/venv/lib/python3.6/site-packages/werkzeug/exceptions.py", line 752, in abort
    return _aborter(status, *args, **kwargs)
  File "/home/biscas/code/Tools/eve-demo/venv/lib/python3.6/site-packages/werkzeug/exceptions.py", line 733, in __call__
    raise self.mapping[code](*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'response'

Solution

  • I was able to reproduce the issue with just flask. Looks like it has to do with latest werkzeug release. Flask doesn't specify which werkzeug to use and just says anything above 0.14 so you automatically get the latest version (0.15.1 as of today) and on. Version 0.15.0 changes implementation of http exceptions a little, specifically this bit right here introduced the Unauthorized http exception constructor, so it now does not accept that response argument. Before it simply extended HTTPException.

    So I suggest to simply pin a spicific version of werkzeug in your project requirements. Anything below version 0.15, say 0.14.1 should work for you.

    What else you can do:

    • Report the issue to eve deveopers
    • Perhaps raising it with werkzeug team can be helpful as well