Skip to content

Commit

Permalink
add authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
devjunhong committed Mar 21, 2020
1 parent f4709b2 commit 4a68564
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
6 changes: 6 additions & 0 deletions mysite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,9 @@
# https://docs.djangoproject.com/en/3.0/howto/static-files/

STATIC_URL = '/static/'

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'polls.api_authentication.AdminOnlyAuth',
)
}
18 changes: 18 additions & 0 deletions polls/api_authentication.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.contrib.auth import authenticate

from rest_framework import authentication
from rest_framework import exceptions


class AdminOnlyAuth(authentication.BaseAuthentication):
def authenticate(self, request):
try:
username = request.query_params.get('username')
password = request.query_params.get('password')
print(username, password)
user = authenticate(username=username, password=password)
if user is None:
raise exceptions.AuthenticationFailed('No such user!')
return (user, None)
except:
raise exceptions.AuthenticationFailed('No such user!')
1 change: 1 addition & 0 deletions polls/api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from .models import Question, Choice
from .serializers import QuestionSerializer, ChoiceSerializer
from .api_authentication import AdminOnlyAuth


class QuestionViewSet(viewsets.ModelViewSet):
Expand Down

0 comments on commit 4a68564

Please sign in to comment.