https://github.com/crmin/drf-tutorial
Django Rest Framework Tutorial + JWT Authentication
https://github.com/crmin/drf-tutorial
django django-rest-framework djangorestframework-jwt jwt jwt-authentication tutorial-code
Last synced: 4 months ago
JSON representation
Django Rest Framework Tutorial + JWT Authentication
- Host: GitHub
- URL: https://github.com/crmin/drf-tutorial
- Owner: crmin
- Created: 2018-11-22T10:19:57.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-11-23T03:27:14.000Z (over 6 years ago)
- Last Synced: 2024-12-27T19:38:10.674Z (6 months ago)
- Topics: django, django-rest-framework, djangorestframework-jwt, jwt, jwt-authentication, tutorial-code
- Language: Python
- Homepage:
- Size: 19.5 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# DRF Tutorial
## Test Environment Setting
### Install pipenv
> This project using pipenv so, if you want to run, you have to install pipenv before run
* using pip
```bash
$ pip install pipenv
```* using homebrew (in mac)
```bash
$ brew install pipenv
```* using apt (in ubuntu)
```bash
$ sudo apt install software-properties-common python-software-properties
$ sudo add-apt-repository ppa:pypa/ppa
$ sudo apt update
$ sudo apt install pipenv
```### Install Packages for Production Environment
```bash
$ pipenv install
```
Then, pipenv install packages:
* django
* djangorestframework
* pygments
* djangorestframework-jwt### Install Packages for Development Environment
```bash
$ pipenv install --dev
```
Then, pipenv install packages:
* [production packages]
* httpieIf you want to run examples in README, you have to install packages for dev env.
## JWT Authentication
### Obtain JWT Token
* Request
```bash
$ http -f POST http://localhost:8000/auth/ username='username' password='password'
```
* Response
```
HTTP/1.1 200 OK
Allow: POST, OPTIONS
Content-Length: 193
Content-Type: application/json
Date: Fri, 23 Nov 2018 02:12:44 GMT
Server: WSGIServer/0.2 CPython/3.7.0
Vary: Accept
X-Frame-Options: SAMEORIGIN{
"token": ""
}
```### Request w/ JWT Token
* RequestSend request with `Authorization` header
```bash
$ http http://localhost:8000/ 'Authorization: JWT '
```* Response
```
HTTP/1.1 200 OK
Allow: GET, HEAD, OPTIONS
Content-Length: 85
Content-Type: application/json
Date: Fri, 23 Nov 2018 02:21:46 GMT
Server: WSGIServer/0.2 CPython/3.7.0
Vary: Accept
X-Frame-Options: SAMEORIGIN{
"snippets": "http://localhost:8000/snippets/",
"users": "http://localhost:8000/users/"
}
```* If wrong token
```
HTTP/1.1 401 Unauthorized
Allow: GET, HEAD, OPTIONS
Content-Length: 38
Content-Type: application/json
Date: Fri, 23 Nov 2018 02:20:59 GMT
Server: WSGIServer/0.2 CPython/3.7.0
Vary: Accept
WWW-Authenticate: JWT realm="api"
X-Frame-Options: SAMEORIGIN{
"detail": "Error decoding signature."
}
```* If signature was expired
```
HTTP/1.1 401 Unauthorized
Allow: GET, HEAD, OPTIONS
Content-Length: 35
Content-Type: application/json
Date: Fri, 23 Nov 2018 02:21:14 GMT
Server: WSGIServer/0.2 CPython/3.7.0
Vary: Accept
WWW-Authenticate: JWT realm="api"
X-Frame-Options: SAMEORIGIN{
"detail": "Signature has expired."
}
```### Refresh Token
* Request
```
$ http -j POST http://localhost:8000/auth/refresh/ token=
```* Response
```
HTTP/1.1 200 OK
Allow: POST, OPTIONS
Content-Length: 223
Content-Type: application/json
Date: Fri, 23 Nov 2018 02:46:38 GMT
Server: WSGIServer/0.2 CPython/3.7.0
Vary: Accept
X-Frame-Options: SAMEORIGIN{
"token": ""
}
```### Verify Token
* Request
```
$ http -j POST http://localhost:8000/auth/verify/ token=
```* Response
```
HTTP/1.1 200 OK
Allow: POST, OPTIONS
Content-Length: 223
Content-Type: application/json
Date: Fri, 23 Nov 2018 02:47:03 GMT
Server: WSGIServer/0.2 CPython/3.7.0
Vary: Accept
X-Frame-Options: SAMEORIGIN{
"token": ""
}
```## References
* Base codes: [http://raccoonyy.github.io/drf3-tutorial-1.html](http://raccoonyy.github.io/drf3-tutorial-1.html)
* DRF Reference: [https://www.django-rest-framework.org/api-guide/pagination/](https://www.django-rest-framework.org/api-guide/pagination/)
* JWT Authentication: [https://getblimp.github.io/django-rest-framework-jwt/](https://getblimp.github.io/django-rest-framework-jwt/)