GithubHelp home page GithubHelp logo

crmin / drf-tutorial Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 20 KB

Django Rest Framework Tutorial + JWT Authentication

Python 100.00%
tutorial-code django django-rest-framework jwt jwt-authentication djangorestframework-jwt

drf-tutorial's Introduction

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
$ pip install pipenv
  • using homebrew (in mac)
$ brew install pipenv
  • using apt (in ubuntu)
$ 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

$ pipenv install

Then, pipenv install packages:

  • django
  • djangorestframework
  • pygments
  • djangorestframework-jwt

Install Packages for Development Environment

$ pipenv install --dev

Then, pipenv install packages:

  • [production packages]
  • httpie

If you want to run examples in README, you have to install packages for dev env.

JWT Authentication

Obtain JWT Token

  • Request
$ 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": "<token>"
}

Request w/ JWT Token

  • Request

Send request with Authorization header

$ http http://localhost:8000/ 'Authorization: JWT <token>'
  • 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=<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": "<new token>"
}

Verify Token

  • Request
$ http -j POST http://localhost:8000/auth/verify/ token=<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": "<token>"
}

References

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.