JWT Token

20 views
Skip to first unread message

Mohammad Kokhaee

unread,
Aug 21, 2019, 6:17:25 PM8/21/19
to Django users
Hello guys
I've created token by JWT and
My questions
1-how to access to user information by token ?
2-Is that the Right way and is that secure?

Ronit Mishra

unread,
Aug 21, 2019, 6:44:44 PM8/21/19
to django...@googlegroups.com
Hi,

First step is to authenticate and obtain the token. For instance, lets say your endpoint is /api/token, so it'll only accepts POST requests.  

>> post http://127.0.0.1:8000/api/token/ username=mohammad password=123

You can use cURL, or HTTPie or Python's requests module to test this.. Heck you can go full commando on this, by building an Angular front..

The response will be of form:

{
    "access": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNTQ1MjI0MjU5LCJqdGkiOiIyYmQ1NjI3MmIzYjI0YjNmOGI1MjJlNThjMzdjMTdlMSIsInVzZXJfaWQiOjF9.D92tTuVi_YcNkJtiLGHtcn6tBcxLCBxz9FKD3qzhUg8",

    "refresh": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTU0NTMxMDM1OSwianRpIjoiMjk2ZDc1ZDA3Nzc2NDE0ZjkxYjhiOTY4MzI4NGRmOTUiLCJ1c2VyX2lkIjoxfQ.rA-mnGRg71NEW_ga0sJoaMODS5ABjE5HnxJDb0F8xAo"
}

After that you are going to store both the access token and the refresh token on the client side, usually in the localStorage.

In order to access the protected views on the backend (i.e., the API endpoints that require authentication), you should include the access token in the header of all requests, like this:

http://127.0.0.1:8000/hello/ "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNTQ1MjI0MjAwLCJqdGkiOiJlMGQxZDY2MjE5ODc0ZTY3OWY0NjM0ZWU2NTQ2YTIwMCIsInVzZXJfaWQiOjF9.9eHat3CvRQYnb5EdcgYFzUyMobXzxlAVh_IAgqyvzCE"



Thats it!


And yes its the proper way of doing things!

Cheers, Ronnie
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b766250f-2fa7-4cb0-afc5-2e4a442dad1a%40googlegroups.com.

Mohammad Kokhaee

unread,
Aug 21, 2019, 7:44:20 PM8/21/19
to django...@googlegroups.com
Thanks for your explanation
 After the user send the token to server 
What Should I do with the token to access to user id and user name and etc .
This is  project is mostly like blog web and when the user authenticate API returns just post of this user .
I don't know how to reach user information with token in views or serializer.

Ronit Mishra

unread,
Aug 21, 2019, 8:07:23 PM8/21/19
to django...@googlegroups.com
You woulld be having some api/profile endpoint, in your project where user details would be available. Send a post request with access token just like I explained in the previous email and you should get the response with profile details.

Mohammad Kokhaee

unread,
Aug 21, 2019, 8:11:45 PM8/21/19
to django...@googlegroups.com
I get that clearly 
But how to get the Response 
Sorry I'm new

Suraj Thapa FC

unread,
Aug 22, 2019, 1:06:18 AM8/22/19
to django...@googlegroups.com
token = request. Meta['HTTP_AUTHORIZATION']
data = {'token' : token}
payload_decoded = jwt.decode(token, settings.SECRET_KEY)
try:
      valid_data = VerifyJSONWebTokenSerilaizer().validate(data)

     user = valid_data['user']
     self.request.user = user
except:
      pass



Reply all
Reply to author
Forward
0 new messages