Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dohvis committed Jul 14, 2018
0 parents commit 722d721
Show file tree
Hide file tree
Showing 20 changed files with 427 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[flake8]
ignore = E501
import-order-style = appnexus
application_import_names = app
application_package_names = django
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.pyc
__pycache__/

db.sqlite3
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Django JWT

Inspired by [flask-jwt-extended](https://github.com/vimalloc/flask-jwt-extended)

## Installation

`pip install git+https://github.com/nerogit/django-jwt.git`

## Usage

```python
from django_jwt import (
create_access_token,
jwt_required,
)

from django.contrib.auth import authenticate
from django.http.response import JsonResponse


@jwt_required
def func_view(request):
user = request.user
results = {"username": user.username}
return JsonResponse(results)



def sign_in(request):
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(request, username=username, password=password)
if user:
access_token = create_access_token(user)
else:
return JsonResponse({}, status=401)

return JsonResponse({"accessToken": access_token})
```

2 changes: 2 additions & 0 deletions django_jwt/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .decorators import jwt_required # NOQA
from .utils import create_access_token # NOQA
62 changes: 62 additions & 0 deletions django_jwt/decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import functools

from jwt import decode

from django.conf import settings
from django.contrib.auth import get_user_model

from .exceptions import (
InvalidHeaderError,
NoAuthorizationError,
)


def _load_user(identity):
user_model = get_user_model()
user = user_model.objects.filter(**identity).first()
return user


def _decode_jwt_from_request(request):
headers = request.META
header_name = 'Authorization'
header_type = 'Bearer'

# Verify we have the auth header
jwt_header = headers.get('Authorization', None)
if not jwt_header:
raise NoAuthorizationError("Missing {} Header".format(header_name))

# Make sure the header is in a valid format that we are expecting, ie
# <HeaderName>: <HeaderType(optional)> <JWT>
parts = jwt_header.split()
if not header_type:
if len(parts) != 1:
msg = "Bad {} header. Expected value '<JWT>'".format(header_name)
raise InvalidHeaderError(msg)
encoded_token = parts[0]
else:
if parts[0] != header_type or len(parts) != 2:
msg = "Bad {} header. Expected value '{} <JWT>'".format(header_name, header_type)
raise InvalidHeaderError(msg)
encoded_token = parts[1]

decoded_token = decode(encoded_token, settings.SECRET_KEY)
if not decoded_token:
raise NoAuthorizationError()
return decoded_token


def jwt_required(view_func):
@functools.wraps(view_func)
def wrapper(request, *args, **kwargs):
from django.conf import settings
jwt_data = _decode_jwt_from_request(request)
identity_field = settings.JWT['IDENTITY_FIELD']
identity = jwt_data[identity_field]
# TODO: Improve error handling
user = _load_user({identity_field: identity})
request.user = user
return view_func(request, *args, **kwargs)

return wrapper
6 changes: 6 additions & 0 deletions django_jwt/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class NoAuthorizationError(Exception):
pass


class InvalidHeaderError(Exception):
pass
12 changes: 12 additions & 0 deletions django_jwt/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from jwt import encode

from django.conf import settings


def create_access_token(user):
identity_field = settings.JWT['IDENTITY_FIELD']
identity = getattr(user, identity_field)
return encode(
{identity_field: identity},
settings.SECRET_KEY,
).decode()
Empty file.
5 changes: 5 additions & 0 deletions examples/django20/app/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class AppConfig(AppConfig):
name = 'app'
Empty file.
Empty file added examples/django20/app/models.py
Empty file.
47 changes: 47 additions & 0 deletions examples/django20/app/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from django_jwt import create_access_token

from django.contrib.auth import get_user_model
from django.test import (
TestCase,
)


class ViewTestCase(TestCase):
def setUp(self):
user_model = get_user_model()
self.username = 'test'
self.password = 'passwd'
self.user = user_model.objects.create_user(
username=self.username,
email='[email protected]',
password=self.password
)
self.access_token = create_access_token(self.user)

def test_sign_in(self):
data = {
"username": self.username,
"password": self.password,
}
resp = self.client.post('/sign-in/', data=data, headers={''})
recv_data = resp.json()
self.assertEqual(200, resp.status_code)
self.assertEqual(['accessToken'], list(recv_data.keys()))

def test_fbv(self):
headers = {
"Authorization": f'Bearer {self.access_token}',
}
resp = self.client.get('/fbv', **headers)
self.assertEqual(200, resp.status_code)
recv_data = resp.json()
self.assertEqual({"username": self.username}, recv_data)

def test_cbv(self):
headers = {
"Authorization": f'Bearer {self.access_token}',
}
resp = self.client.get('/cbv', **headers)
self.assertEqual(200, resp.status_code)
recv_data = resp.json()
self.assertEqual({"username": self.username}, recv_data)
36 changes: 36 additions & 0 deletions examples/django20/app/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from django_jwt import (
create_access_token,
jwt_required,
)

from django.contrib.auth import authenticate
from django.http.response import JsonResponse
from django.utils.decorators import method_decorator
from django.views import View


@jwt_required
def func_view(request):
user = request.user
results = {"username": user.username}
return JsonResponse(results)


@method_decorator(jwt_required, name='dispatch')
class ClassBasedView(View):
def get(self, request):
user = request.user
results = {"username": user.username}
return JsonResponse(results)


def sign_in(request):
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(request, username=username, password=password)
if user:
access_token = create_access_token(user)
else:
return JsonResponse({}, status=401)

return JsonResponse({"accessToken": access_token})
Empty file.
125 changes: 125 additions & 0 deletions examples/django20/django20/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
"""
Django settings for django20 project.
Generated by 'django-admin startproject' using Django 2.0.7.
For more information on this file, see
https://docs.djangoproject.com/en/2.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.0/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'li9)3087fen&qm=hyzh^2+--k3h5!_2f1p=#wc(t6qcytfnu!o'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

'app',
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'django20.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'django20.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}

# Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]


# Internationalization
# https://docs.djangoproject.com/en/2.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/

STATIC_URL = '/static/'

JWT = {
"IDENTITY_FIELD": "email",
}
13 changes: 13 additions & 0 deletions examples/django20/django20/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.urls import path

from app.views import (
ClassBasedView,
func_view,
sign_in,
)

urlpatterns = [
path('cbv', ClassBasedView.as_view()),
path('fbv', func_view),
path('sign-in/', sign_in),
]
16 changes: 16 additions & 0 deletions examples/django20/django20/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
WSGI config for django20 project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django20.settings")

application = get_wsgi_application()
Loading

0 comments on commit 722d721

Please sign in to comment.