mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-24 08:14:16 +03:00
Renamed AuthTokenView to ObtainAuthToken, added obtain_auth_token var, updated tests & docs. Left authtoken.urls in place as example.
This commit is contained in:
parent
d3ee5080a0
commit
4fd590f96f
|
@ -97,21 +97,15 @@ If successfully authenticated, `TokenAuthentication` provides the following cred
|
||||||
|
|
||||||
**Note:** If you use `TokenAuthentication` in production you must ensure that your API is only available over `https` only.
|
**Note:** If you use `TokenAuthentication` in production you must ensure that your API is only available over `https` only.
|
||||||
|
|
||||||
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 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:
|
||||||
|
|
||||||
REST framework provides a built-in login view. To use it, add a pattern to include the token login view for clients as follows:
|
|
||||||
|
|
||||||
urlpatterns += patterns('',
|
urlpatterns += patterns('',
|
||||||
url(r'^api-token-auth/', include('rest_framework.authtoken.urls',
|
url(r'^api-token-auth/', 'rest_framework.authtoken.obtain_auth_token')
|
||||||
namespace='rest_framework'))
|
|
||||||
)
|
)
|
||||||
|
|
||||||
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 `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:
|
||||||
|
|
||||||
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' }
|
|
||||||
|
|
||||||
|
{ 'token' : '9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b' }
|
||||||
|
|
||||||
## OAuthAuthentication
|
## OAuthAuthentication
|
||||||
|
|
||||||
|
|
|
@ -59,6 +59,7 @@ The following people have helped make REST framework great.
|
||||||
* Toni Michel - [tonimichel]
|
* Toni Michel - [tonimichel]
|
||||||
* Ben Konrath - [benkonrath]
|
* Ben Konrath - [benkonrath]
|
||||||
* Marc Aymerich - [glic3rinu]
|
* Marc Aymerich - [glic3rinu]
|
||||||
|
* Rob Romano - [robromano]
|
||||||
|
|
||||||
Many thanks to everyone who's contributed to the project.
|
Many thanks to everyone who's contributed to the project.
|
||||||
|
|
||||||
|
@ -153,3 +154,4 @@ To contact the author directly:
|
||||||
[tonimichel]: https://github.com/tonimichel
|
[tonimichel]: https://github.com/tonimichel
|
||||||
[benkonrath]: https://github.com/benkonrath
|
[benkonrath]: https://github.com/benkonrath
|
||||||
[glic3rinu]: https://github.com/glic3rinu
|
[glic3rinu]: https://github.com/glic3rinu
|
||||||
|
[robromano]: https://github.com/robromano
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
## Master
|
## Master
|
||||||
|
|
||||||
* Support for `read_only_fields` on `ModelSerializer` classes.
|
* Support for `read_only_fields` on `ModelSerializer` classes.
|
||||||
|
* Add convenience login view to get tokens when using `TokenAuthentication`
|
||||||
|
|
||||||
## 2.1.2
|
## 2.1.2
|
||||||
|
|
||||||
|
|
|
@ -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.
|
your API requires authentication.
|
||||||
|
|
||||||
The urls must be namespaced as 'rest_framework', and you should make sure
|
You should make sure your authentication settings include
|
||||||
your authentication settings include `TokenAuthentication`.
|
`TokenAuthentication`.
|
||||||
|
|
||||||
urlpatterns = patterns('',
|
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 django.conf.urls.defaults import patterns, url
|
||||||
from rest_framework.authtoken.views import AuthTokenView
|
|
||||||
|
|
||||||
urlpatterns = patterns('rest_framework.authtoken.views',
|
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'),
|
||||||
)
|
)
|
||||||
|
|
|
@ -6,7 +6,7 @@ from rest_framework.response import Response
|
||||||
from rest_framework.authtoken.models import Token
|
from rest_framework.authtoken.models import Token
|
||||||
from rest_framework.authtoken.serializers import AuthTokenSerializer
|
from rest_framework.authtoken.serializers import AuthTokenSerializer
|
||||||
|
|
||||||
class AuthTokenView(APIView):
|
class ObtainAuthToken(APIView):
|
||||||
throttle_classes = ()
|
throttle_classes = ()
|
||||||
permission_classes = ()
|
permission_classes = ()
|
||||||
parser_classes = (parsers.FormParser, parsers.MultiPartParser, parsers.JSONParser,)
|
parser_classes = (parsers.FormParser, parsers.MultiPartParser, parsers.JSONParser,)
|
||||||
|
@ -20,3 +20,5 @@ class AuthTokenView(APIView):
|
||||||
return Response({'token': token.key})
|
return Response({'token': token.key})
|
||||||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
|
||||||
|
obtain_auth_token = ObtainAuthToken.as_view()
|
||||||
|
|
|
@ -27,7 +27,7 @@ MockView.authentication_classes += (TokenAuthentication,)
|
||||||
|
|
||||||
urlpatterns = patterns('',
|
urlpatterns = patterns('',
|
||||||
(r'^$', MockView.as_view()),
|
(r'^$', MockView.as_view()),
|
||||||
(r'^auth-token/', include('rest_framework.authtoken.urls')),
|
(r'^auth-token/', 'rest_framework.authtoken.views.obtain_auth_token'),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user