Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update authentication scheme from Token to JWT #1038

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,12 @@ pytest-xdist==1.31.0
cssselect==1.1.0

# PostgreSQL database adapter for the Python
psycopg2-binary==2.8.5
psycopg2-binary==2.8.5

# JSON Web Token implementation in Python
# https://pyjwt.readthedocs.io/en/stable/
PyJWT==1.7.1

# A JSON Web Token authentication plugin for the Django REST Framework.
# https://django-rest-framework-simplejwt.readthedocs.io/en/stable/
djangorestframework-simplejwt==4.4.0
4 changes: 2 additions & 2 deletions src/attendee/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
from rest_framework.response import Response
from registry.helper import reg
from rest_framework.permissions import IsAuthenticated
from rest_framework_simplejwt.authentication import JWTAuthentication

from core.authentication import TokenAuthentication
from attendee.models import Attendee


class AttendeeAPIView(views.APIView):
authentication_classes = [TokenAuthentication]
authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated]

model = Attendee
Expand Down
10 changes: 5 additions & 5 deletions src/events/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from rest_framework_simplejwt.authentication import JWTAuthentication

from django.conf import settings
from django.db.models import Count
from django.http import Http404

from core.authentication import TokenAuthentication
from events.models import (
CustomEvent, Location, ProposedTalkEvent,
ProposedTutorialEvent, SponsoredEvent, Time, KeynoteEvent
Expand All @@ -34,7 +34,7 @@ class TutorialListAPIView(ListAPIView):


class SpeechListAPIView(APIView):
authentication_classes = [TokenAuthentication]
authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated]

def get(self, request, *args, **kwargs):
Expand Down Expand Up @@ -76,7 +76,7 @@ class TutorialDetailAPIView(RetrieveAPIView):


class SpeechDetailAPIView(APIView):
authentication_classes = [TokenAuthentication]
authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated]

def get(self, request, *args, **kwargs):
Expand Down Expand Up @@ -213,7 +213,7 @@ def display(self):


class ScheduleAPIView(APIView):
authentication_classes = [TokenAuthentication]
authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated]

event_querysets = [
Expand Down Expand Up @@ -294,7 +294,7 @@ def get(self, request):


class KeynoteEventListAPIView(ListAPIView):
authentication_classes = [TokenAuthentication]
authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated]

queryset = KeynoteEvent.objects.all()
Expand Down
6 changes: 6 additions & 0 deletions src/pycontw2016/settings/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
env,
)
from .base import * # NOQA
from datetime import timedelta

import logging.config
import os
Expand Down Expand Up @@ -99,3 +100,8 @@


DJANGO_Q_DEBUG = True

SIMPLE_JWT = {
'ACCESS_TOKEN_LIFETIME': timedelta(minutes=10),
'AUTH_HEADER_TYPES': ('Token',),
}
3 changes: 2 additions & 1 deletion src/pycontw2016/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
url(r'^api/events/', include('events.api.urls', namespace="events")),
url(r'^set-language/$', set_language, name='set_language'),
url(r'^admin/', admin.site.urls),
url(r'^api/attendee/', include('attendee.api.urls'))
url(r'^api/attendee/', include('attendee.api.urls')),
url(r'api/token/', include('security.urls'))
]

# User-uploaded files like profile pics need to be served in development.
Expand Down
Empty file added src/security/__init__.py
Empty file.
11 changes: 11 additions & 0 deletions src/security/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.urls import path
from rest_framework_simplejwt.views import (
TokenObtainPairView,
TokenRefreshView,
)


urlpatterns = [
path('', TokenObtainPairView.as_view(), name='token_obtain_pair'),
path('refresh/', TokenRefreshView.as_view(), name='token_refresh')
]
6 changes: 3 additions & 3 deletions src/sponsors/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
from rest_framework import views
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from rest_framework_simplejwt.authentication import JWTAuthentication

from core.authentication import TokenAuthentication
from sponsors.models import Sponsor, OpenRole


class SponsorAPIView(views.APIView):
authentication_classes = [TokenAuthentication]
authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated]

def get(self, request):
Expand Down Expand Up @@ -42,7 +42,7 @@ def get(self, request):


class JobAPIView(views.APIView):
authentication_classes = [TokenAuthentication]
authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated]

def get(self, request):
Expand Down