Created
March 20, 2013 04:20
-
-
Save nitely/5202285 to your computer and use it in GitHub Desktop.
Django, django-ratelimit on login admin view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-*- coding: utf-8 -*- | |
from django.contrib import messages | |
from django.core.urlresolvers import reverse | |
from django.shortcuts import redirect | |
# https://github.com/jsocol/django-ratelimit | |
from ratelimit.decorators import ratelimit | |
def login_wrapper(login_func): | |
@ratelimit(method='POST', field='username', rate='5/5m') | |
def admin_login(request, **kwargs): | |
if getattr(request, 'limited', False): # was_limited | |
messages.error(request, 'Too many login attemps, please wait 5 minutes') | |
return redirect(reverse("admin:index")) | |
else: | |
return login_func(request, **kwargs) | |
return admin_login |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf.urls import patterns, include, url | |
from django.contrib import admin | |
from admin_login.decorators import login_wrapper | |
admin.autodiscover() | |
admin.site.login = login_wrapper(admin.site.login) # rate limit | |
urlpatterns = patterns('', | |
# ... | |
# Uncomment the next line to enable the admin: | |
url(r'^admin/', include(admin.site.urls)), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prevents brute force attacks to the admin login on Django sites.
Requires: https://github.com/jsocol/django-ratelimit
You should also "rate limit" your regular login view.