Python Eve Auth无法返回401异常

时间:2019-03-28 12:03:12

标签: python authentication flask eve

我正在运行Python Eve的demo code,并且在Auth部分遇到麻烦。除了settings.py中的MongoDB URI外,我运行的代码与存储库相同。

# 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'

如果我在Basic Auth中放入正确的凭据(user=admin passwd=secret),一切都可以正常工作,但是如果我没有放置Authorization标头或正确的凭据,那么我不会获得401,因为我可以预期,但是应用程序崩溃并返回500。这是回溯。

[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'

1 个答案:

答案 0 :(得分:2)

我只用烧瓶就能重现问题。似乎与最新的werkzeug版本有关。 Flask没有指定要使用哪个werkzeug,只是说出高于0.14的字样,因此您会自动获得最新版本(截至今天为0.15.1)。版本0.15.0改变了http异常的实现,特别是this bit right here引入了Unauthorized http异常构造函数,因此现在不接受该response参数。在此之前,只需扩展HTTPException

因此,我建议您在项目需求中简单固定werkzeug的特定版本。低于0.15版本的任何东西,例如0.14.1都应该适合您。

您还能做什么:

  • 将问题报告给夏娃开发者
  • 也许与werkzeug团队一起筹集资金也会有帮助