Enable token authentication for the API, add API token request view

This commit is contained in:
Roberto Rosario
2014-07-24 01:03:51 -04:00
parent 8807c52ac3
commit 3c4ddecf80
4 changed files with 26 additions and 2 deletions

View File

@@ -0,0 +1,10 @@
from __future__ import absolute_import
from django.utils.translation import ugettext_lazy as _
from .classes import APIEndPoint
from .urls import api_urls
endpoint = APIEndPoint('rest_api')
endpoint.register_urls(api_urls)
endpoint.add_endpoint('auth_token_obtain', _(u'Obtain an API authentication token.'))

View File

@@ -2,7 +2,7 @@ from __future__ import absolute_import
from django.conf.urls import include, patterns, url
from .views import APIBase, Version_0, APIAppView
from .views import APIBase, Version_0, APIAppView, BrowseableObtainAuthToken
version_0_urlpatterns = patterns('',
url(r'^$', Version_0.as_view(), name='api-version-0'),
@@ -13,3 +13,7 @@ urlpatterns = patterns('',
url(r'^$', APIBase.as_view(), name='api-root'),
url(r'^v0/', include(version_0_urlpatterns)),
)
api_urls = patterns('',
url(r'^auth/token/obtain/', BrowseableObtainAuthToken.as_view(), name='auth_token_obtain'),
)

View File

@@ -3,7 +3,8 @@ from __future__ import absolute_import
import logging
from rest_framework import generics
from rest_framework import generics, renderers
from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.response import Response
from rest_framework.reverse import reverse
@@ -66,3 +67,10 @@ class APIAppView(generics.GenericAPIView):
return Response({
'endpoints': result
})
class BrowseableObtainAuthToken(ObtainAuthToken):
"""
Obtain an API authentication token.
"""
renderer_classes = (renderers.JSONRenderer, renderers.BrowsableAPIRenderer)

View File

@@ -52,6 +52,7 @@ INSTALLED_APPS = (
'mptt',
'compressor',
'rest_framework',
'rest_framework.authtoken',
'solo',
# Base generic
'permissions',
@@ -255,6 +256,7 @@ REST_FRAMEWORK = {
'MAX_PAGINATE_BY': 100,
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.TokenAuthentication',
'rest_framework.authentication.SessionAuthentication',
)
}