Skip to content

Commit

Permalink
Add throttling protection
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Lenc committed May 30, 2014
1 parent 8f9e914 commit 81ab260
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Upcoming

* Protect auth login and password reset views against throttling.

## v1.1.4

* Add email field to PasswordResetEmail response to OPTIONS request
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,11 @@ with a selection from
url('', include('user_management.api.urls.register')),
...
)


### Throttling protection
The `/auth/` and `/auth/password_reset/` URLs are protected against throttling
using the built-in [DRF throttle module](http://www.django-rest-framework.org/api-guide/throttling).

You need to set the throttling rates in `DEFAULT_THROTTLE_RATES` setting for
`REST_FRAMEWORK` in your `settings.py`.
3 changes: 3 additions & 0 deletions user_management/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from rest_framework.authtoken.models import Token
from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.throttling import AnonRateThrottle, UserRateThrottle

from . import serializers, permissions

Expand All @@ -19,6 +20,7 @@

class GetToken(ObtainAuthToken):
renderer_classes = (renderers.JSONRenderer, renderers.BrowsableAPIRenderer)
throttle_classes = [AnonRateThrottle, UserRateThrottle]

def delete(self, request, *args, **kwargs):
try:
Expand Down Expand Up @@ -124,6 +126,7 @@ def initial(self, request, *args, **kwargs):

class PasswordReset(OneTimeUseAPIMixin, generics.UpdateAPIView):
permission_classes = [permissions.IsNotAuthenticated]
throttle_classes = [AnonRateThrottle, UserRateThrottle]
model = User
serializer_class = serializers.PasswordResetSerializer

Expand Down
4 changes: 4 additions & 0 deletions user_management/tests/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
),
'DEFAULT_THROTTLE_RATES': {
'anon': '10/day',
'user': '100/day'
},
},
)

Expand Down

0 comments on commit 81ab260

Please sign in to comment.