Skip to content
This repository was archived by the owner on May 26, 2020. It is now read-only.

Commit 4c3cdce

Browse files
committed
[refresh-token] add documentation on refresh token
Update README.md [refresh-token] add curl example fix
1 parent 5107b62 commit 4c3cdce

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

README.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ Now in order to access protected api urls you must include the `Authorization: J
6161
$ curl -H "Authorization: JWT <your_token>" http://localhost:8000/protected-url/
6262
```
6363

64+
## Refresh Token
65+
If `JWT_ALLOW_TOKEN_RENEWAL` is True, issued tokens can be "refreshed" to obtain a new brand token with renewed expiration time. Add a URL pattern like this:
66+
```python
67+
url(r'^api-token-refresh/', 'rest_framework_jwt.views.refresh_jwt_token'),
68+
```
69+
70+
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}`.
71+
72+
```bash
73+
$ curl -X POST -H "Content-Type: application/json" -d '{"token":"<EXISTING_TOKEN>}' http://localhost:8000/api-token-refresh/
74+
```
75+
76+
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_TOKEN_RENEWAL_LIMIT`.
77+
78+
6479
## Additional Settings
6580
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.
6681

@@ -74,13 +89,19 @@ JWT_AUTH = {
7489

7590
'JWT_PAYLOAD_HANDLER':
7691
'rest_framework_jwt.utils.jwt_payload_handler',
92+
93+
'JWT_PAYLOAD_GET_USER_ID_HANDLER':
94+
'rest_framework_jwt.utils.jwt_get_user_id_from_payload_handler',
7795

7896
'JWT_SECRET_KEY': settings.SECRET_KEY,
7997
'JWT_ALGORITHM': 'HS256',
8098
'JWT_VERIFY': True,
8199
'JWT_VERIFY_EXPIRATION': True,
82100
'JWT_LEEWAY': 0,
83-
'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=300)
101+
'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=300),
102+
103+
'JWT_ALLOW_TOKEN_RENEWAL': False,
104+
'JWT_TOKEN_RENEWAL_LIMIT': datetime.timedelta(days=7),
84105
}
85106
```
86107
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.
@@ -126,8 +147,24 @@ Default is `True`.
126147
127148
Default is `0` seconds.
128149

129-
130150
### JWT_EXPIRATION_DELTA
131151
This is an instance of Python's `datetime.timedelta`. This will be added to `datetime.utcnow()` to set the expiration time.
132152

133153
Default is `datetime.timedelta(seconds=300)`(5 minutes).
154+
155+
### JWT_ALLOW_TOKEN_RENEWAL
156+
Enable token renewal functionality. Token issued from `rest_framework_jwt.views.obtain_jwt_token` will have an `orig_iat` field. Default is `False`
157+
158+
### JWT_TOKEN_RENEWAL_LIMIT
159+
Limit on token renewal, is a `datetime.timedelta` instance. This is how much time after the original token that future tokens can be refreshed from.
160+
161+
Default is `datetime.timedelta(days=7)` (7 days).
162+
163+
### JWT_PAYLOAD_HANDLER
164+
Specify a custom function to generate the token payload
165+
166+
### JWT_PAYLOAD_GET_USER_ID_HANDLER
167+
If you store `user_id` differently than the default payload handler does, implement this function to fetch `user_id` from the payload.
168+
169+
170+

0 commit comments

Comments
 (0)