You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 26, 2020. It is now read-only.
**JSON Web Token Authentication support for Django REST Framework**
7
+
8
+
Full documentation for the project is available at [docs][docs].
4
9
5
10
## Overview
6
-
This package provides [JSON Web Token Authentication](http://tools.ietf.org/html/draft-ietf-oauth-json-web-token) support for [Django REST framework](http://django-rest-framework.org/).
7
11
8
-
If you want to read more about JWT, here's a great blog post by the guys at Auth0 that talks about [Cookie vs Token based authentication](http://blog.auth0.com/2014/01/07/angularjs-authentication-with-cookies-vs-token/).
12
+
This package provides [JSON Web Token Authentication][jwt-auth-spec] support for [Django REST framework][drf].
13
+
14
+
If you want to know more about JWT, check out the following resources:
15
+
16
+
- DjangoCon 2014 - JSON Web Tokens [Video][jwt-video] | [Slides][jwt-slides]
17
+
-[Auth with JSON Web Tokens][auth-jwt]
18
+
-[JWT.io][jwt-io]
9
19
10
20
## Requirements
11
21
@@ -17,186 +27,25 @@ If you want to read more about JWT, here's a great blog post by the guys at Auth
17
27
18
28
Install using `pip`...
19
29
20
-
```
21
-
$ pip install djangorestframework-jwt
22
-
```
23
-
24
-
## Usage
25
-
26
-
In your `settings.py`, add `JSONWebTokenAuthentication` to Django REST framework's `DEFAULT_AUTHENTICATION_CLASSES`.
You can easily test if the endpoint is working by doing the following in your terminal, if you had a user created with the username **admin** and password **abc123**.
53
-
54
30
```bash
55
-
$ curl -X POST -d "username=admin&password=abc123" http://localhost:8000/api-token-auth/
56
-
```
57
-
58
-
Alternatively, you can use all the content types supported by the Django REST framework to obtain the auth token. For example:
59
-
60
-
```bash
61
-
$ curl -X POST -H "Content-Type: application/json" -d '{"username":"admin","password":"abc123"}' http://localhost:8000/api-token-auth/
62
-
```
63
-
64
-
Now in order to access protected api urls you must include the `Authorization: JWT <your_token>` header.
If `JWT_ALLOW_REFRESH` is True, issued tokens can be "refreshed" to obtain a new brand token with renewed expiration time. Add a URL pattern like this:
Pass in an existing token to the refresh endpoint as follows: `{"token": EXISTING_TOKEN}`. Note that only non-expired tokens will work. The JSON response looks the same as the normal obtain token endpoint `{"token": NEW_TOKEN}`.
77
-
78
-
```bash
79
-
$ curl -X POST -H "Content-Type: application/json" -d '{"token":"<EXISTING_TOKEN>"}' http://localhost:8000/api-token-refresh/
80
-
```
81
-
82
-
Refresh with tokens can be repeated (token1 -> token2 -> token3), but this chain of token stores the time that the original token (obtained with username/password credentials), as `orig_iat`. You can only keep refreshing tokens up to `JWT_REFRESH_EXPIRATION_DELTA`.
83
-
84
-
A typical use case might be a web app where you'd like to keep the user "logged in" the site without having to re-enter their password, or get kicked out by surprise before their token expired. Imagine they had a 1-hour token and are just at the last minute while they're still doing something. With mobile you could perhaps store the username/password to get a new token, but this is not a great idea in a browser. Each time the user loads the page, you can check if there is an existing non-expired token and if it's close to being expired, refresh it to extend their session. In other words, if a user is actively using your site, they can keep their "session" alive.
85
-
86
-
## Additional Settings
87
-
There are some additional settings that you can override similar to how you'd do it with Django REST framework itself. Here are all the available defaults.
This packages uses the JSON Web Token Python implementation, [PyJWT](https://github.com/progrium/pyjwt) and allows to modify some of it's available options.
120
-
121
-
### JWT_SECRET_KEY
122
-
This is the secret key used to sign the JWT. Make sure this is safe and not shared or public.
123
-
124
-
Default is your project's `settings.SECRET_KEY`.
125
-
126
-
### JWT_ALGORITHM
127
-
128
-
Possible values:
129
-
130
-
> * HS256 - HMAC using SHA-256 hash algorithm (default)
> For the RSASSA-PKCS1-v1_5 algorithms, the "secret" argument in jwt.encode is supposed to be a private RSA key as
139
-
> imported with Crypto.PublicKey.RSA.importKey. Likewise, the "secret" argument in jwt.decode is supposed to be the
140
-
> public RSA key imported with the same method.
141
-
142
-
Default is `"HS256"`.
143
-
144
-
### JWT_VERIFY
145
-
146
-
If the secret is wrong, it will raise a jwt.DecodeError telling you as such. You can still get at the payload by setting the `JWT_VERIFY` to `False`.
147
-
148
-
Default is `True`.
149
-
150
-
### JWT_VERIFY_EXPIRATION
151
-
152
-
You can turn off expiration time verification with by setting `JWT_VERIFY_EXPIRATION` to `False`.
153
-
154
-
Default is `True`.
155
-
156
-
### JWT_LEEWAY
157
-
158
-
> This allows you to validate an expiration time which is in the past but no very far. For example, if you have a JWT payload with an expiration time set to 30 seconds after creation but you know that sometimes you will process it after 30 seconds, you can set a leeway of 10 seconds in order to have some margin.
159
-
160
-
Default is `0` seconds.
161
-
162
-
### JWT_EXPIRATION_DELTA
163
-
This is an instance of Python's `datetime.timedelta`. This will be added to `datetime.utcnow()` to set the expiration time.
164
-
165
-
Default is `datetime.timedelta(seconds=300)`(5 minutes).
166
-
167
-
### JWT_ALLOW_REFRESH
168
-
Enable token refresh functionality. Token issued from `rest_framework_jwt.views.obtain_jwt_token` will have an `orig_iat` field. Default is `False`
169
-
170
-
### JWT_REFRESH_EXPIRATION_DELTA
171
-
Limit on token refresh, is a `datetime.timedelta` instance. This is how much time after the original token that future tokens can be refreshed from.
172
-
173
-
Default is `datetime.timedelta(days=7)` (7 days).
174
-
175
-
### JWT_PAYLOAD_HANDLER
176
-
Specify a custom function to generate the token payload
177
-
178
-
### JWT_PAYLOAD_GET_USER_ID_HANDLER
179
-
If you store `user_id` differently than the default payload handler does, implement this function to fetch `user_id` from the payload.
180
-
181
-
### JWT_RESPONSE_PAYLOAD_HANDLER
182
-
Responsible for controlling the response data returned after login or refresh. Override to return a custom response such as including the serialized representation of the User.
You can modify the Authorization header value prefix that is required to be sent together with the token. The default value is `JWT`. This decision was introduced in PR [#4](https://github.com/GetBlimp/django-rest-framework-jwt/pull/4) to allow using both this package and OAuth2 in DRF.
36
+
Full documentation for the project is available at [docs][docs].
199
37
200
-
Another common value used for tokens and Authorization headers is `Bearer`.
38
+
You may also want to follow the [author][blimp] on Twitter.
0 commit comments