Renamed AuthTokenView to ObtainAuthToken, added obtain_auth_token var, updated tests & docs. Left authtoken.urls in place as example.

This commit is contained in:
Rob Romano 2012-11-13 16:49:13 -08:00
parent eb20b5663e
commit 321ba156ca
6 changed files with 19 additions and 16 deletions

View File

@ -114,16 +114,13 @@ If you've already created some User`'s, you can run a script like this.
When using TokenAuthentication, it may be useful to add a login view for clients to retrieve the token.
REST framework provides a built-in login view. To use it, add a pattern to include the token login view for clients as follows:
REST framework provides a built-in login view for clients to retrieve the token called `rest_framework.authtoken.obtain_auth_token`. To use it, add a pattern to include the token login view for clients as follows:
urlpatterns += patterns('',
url(r'^api-token-auth/', include('rest_framework.authtoken.urls',
namespace='rest_framework'))
url(r'^api-token-auth/', 'rest_framework.authtoken.obtain_auth_token')
)
The `r'^api-token-auth/'` part of pattern can actually be whatever URL you want to use. The only restriction is that the included urls must use the `'rest_framework'` namespace.
The authtoken login view will render a JSON response when a valid `username` and `password` fields are POST'ed to the view using forms or JSON:
The `r'^api-token-auth/'` part of pattern can actually be whatever URL you want to use. The authtoken login view will render a JSON response when a valid `username` and `password` fields are POST'ed to the view using forms or JSON:
{ 'token' : '9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b' }

View File

@ -60,6 +60,7 @@ The following people have helped make REST framework great.
* Ben Konrath - [benkonrath]
* Marc Aymerich - [glic3rinu]
* Ludwig Kraatz - [ludwigkraatz]
* Rob Romano - [robromano]
Many thanks to everyone who's contributed to the project.
@ -155,3 +156,5 @@ To contact the author directly:
[benkonrath]: https://github.com/benkonrath
[glic3rinu]: https://github.com/glic3rinu
[ludwigkraatz]: https://github.com/ludwigkraatz
[robromano]: https://github.com/robromano

View File

@ -7,6 +7,7 @@
## Master
* Support for `read_only_fields` on `ModelSerializer` classes.
* Add convenience login view to get tokens when using `TokenAuthentication`
## 2.1.2

View File

@ -1,20 +1,20 @@
"""
Login and logout views for token authentication.
Login view for token authentication.
Add these to your root URLconf if you're using token authentication
Add this to your root URLconf if you're using token authentication
your API requires authentication.
The urls must be namespaced as 'rest_framework', and you should make sure
your authentication settings include `TokenAuthentication`.
You should make sure your authentication settings include
`TokenAuthentication`.
urlpatterns = patterns('',
...
url(r'^auth-token', include('rest_framework.authtoken.urls', namespace='rest_framework'))
url(r'^auth-token/', 'rest_framework.authtoken.obtain_auth_token')
)
"""
from django.conf.urls.defaults import patterns, url
from rest_framework.authtoken.views import AuthTokenView
urlpatterns = patterns('rest_framework.authtoken.views',
url(r'^login/$', AuthTokenView.as_view(), name='token_login'),
url(r'^login/$', 'rest_framework.authtoken.views.obtain_auth_token', name='token_login'),
)

View File

@ -6,7 +6,7 @@ from rest_framework.response import Response
from rest_framework.authtoken.models import Token
from rest_framework.authtoken.serializers import AuthTokenSerializer
class AuthTokenView(APIView):
class ObtainAuthToken(APIView):
throttle_classes = ()
permission_classes = ()
parser_classes = (parsers.FormParser, parsers.MultiPartParser, parsers.JSONParser,)
@ -20,3 +20,5 @@ class AuthTokenView(APIView):
return Response({'token': token.key})
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
obtain_auth_token = ObtainAuthToken.as_view()

View File

@ -27,7 +27,7 @@ MockView.authentication_classes += (TokenAuthentication,)
urlpatterns = patterns('',
(r'^$', MockView.as_view()),
(r'^auth-token/', include('rest_framework.authtoken.urls')),
(r'^auth-token/', 'rest_framework.authtoken.views.obtain_auth_token'),
)