I'm using the awesome Eve REST-framework for creating a CRUD API with JWT authentication. I've looked at the tutorials posted here but I'm receiving a 401 error when doing a POST request to my endpoints that require token auth.
I've read this SO question: Issue with the Python Eve TokenAuth Feature but I'm pretty sure the token is Base64 encoded.
This is the response I'm getting back from the server when doing a cURL GET request:
curl -H "Authorization: <MYTOKEN>" -i http://MY_IP/users/548f6ecd64e6d12236c9576b
---- Response ----
HTTP/1.1 401 UNAUTHORIZED
Server: nginx/1.4.6 (Ubuntu)
Date: Tue, 16 Dec 2014 10:49:25 GMT
Content-Type: application/json
Content-Length: 91
Connection: keep-alive
WWW-Authenticate: Basic realm:"eve"
{"_status": "ERR", "_error": {"message": "Please provide proper credentials", "code": 401}}
Below is my code:
app.py
from eve import Eve
from eve.auth import TokenAuth
import jwt
class RolesAuth(TokenAuth):
def check_auth(self, token, allowed_roles, resource, method):
users = app.data.driver.db['users']
# Add check of user credentials by decoding JWT
user = users.find_one({'token': token})
return user
def add_token(documents):
for document in documents:
payload = {'username': document['username']}
document["token"] = jwt.encode(payload, 'secret')
if __name__ == '__main__':
app = Eve(auth=RolesAuth)
app.on_insert_users += add_token
app.run()
settings.py
users_schema = {
'username': {
'type': 'string',
'required': True,
},
'password': {
'type': 'string',
'required': True,
},
'email': {
'type': 'string',
'minlength': 1,
'maxlength': 200,
'required': True,
},
'token': {
'type': 'string',
},
'created': {
'type': 'datetime',
}
}
users = {
'cache_control': '',
'cache_expires': 0,
'extra_response_fields': ['token'],
'public_methods': ['POST'],
'schema': users_schema
}
DOMAIN = {
'users': users,
}
I have a token stored in my MongoDB for the user and I'm making the request with Postman and I'm including the token in the Authorization header like so:
Authorization: <USERTOKEN>
Any thoughts on why this gives me a 401.
Thanks!