401 when trying to authenticate user in Eve flask framework

假装没事ソ 提交于 2019-12-08 14:03:27
Nicola Iarocci

I tested your code but, for simplicity, I got rid of jwt.encode:

def add_token(documents):
    for document in documents:
        payload = {'username': document['username']}
        document["token"] = 'hello'

I POSTed a new user to /users and then did a GET to the same endpoint with Postman: it works like a charm (200). Then I did the same test with curl:

curl -H "Authorization: Basic Y2lhbzo=" -i http://localhost:5000/users

That also retuns a 200. As you can see the Auth header is encoded (copy & paste from Postman request preview). Just make sure you are passing the right stuff, maybe set a breakpoint in check_auth to validate what's coming into it and wether the db lookup is successful or not.

Hope this helps in diagnosing your issue.

PS: please note that in curl the Authorization header also has a Basic statement before the token, which seems to be lacking in your code snippet.

UPDATE

For the sake of it I also installed PyJWT and tested your code and... it works just fine on my end. Must be something wrong with your request.

UPDATE2

One thing that might not be so obvious is that you still need to append an (encoded) : to your auth header (it's Basic Auth and it parses both username and pw). That's why in the above example you have a = at the end of the encoded 'hello'.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!