README.rst 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. Django Axes
  2. ===========
  3. .. image:: https://secure.travis-ci.org/django-pci/django-axes.png?branch=master
  4. :alt: Build Status
  5. :target: http://travis-ci.org/django-pci/django-axes
  6. ``django-axes`` is a very simple way for you to keep track of failed login
  7. attempts, both for the Django admin and for the rest of your site. The name is
  8. sort of a geeky pun, since ``axes`` can be read interpreted as:
  9. * "access", as in monitoring access attempts
  10. * "axes", as in tools you can use hack (generally on wood). In this case,
  11. however, the "hacking" part of it can be taken a bit further: ``django-axes``
  12. is intended to help you *stop* people from hacking (popular media
  13. definition) your website. Hilarious, right? That's what I thought too!
  14. Requirements
  15. ============
  16. ``django-axes`` requires Django 1.5 or later. The application is intended to
  17. work around the Django admin and the regular ``django.contrib.auth``
  18. login-powered pages.
  19. Installation
  20. ============
  21. You can install the latest stable package running this command::
  22. $ pip install django-axes
  23. Also you can install the development version running this command::
  24. $ pip install -e git+http://github.com/django-pci/django-axes.git#egg=django_axes-dev
  25. Development
  26. ===========
  27. You can contribute to this project forking it from github and sending pull requests.
  28. Running tests
  29. -------------
  30. Tests can be run, after you clone the repository and having django installed, like::
  31. $ PYTHONPATH=$PYTHONPATH:$PWD django-admin.py test axes --settings=axes.test_settings
  32. Configuration
  33. =============
  34. First of all, you must add this project to your list of ``INSTALLED_APPS`` in
  35. ``settings.py``::
  36. INSTALLED_APPS = (
  37. 'django.contrib.admin',
  38. 'django.contrib.auth',
  39. 'django.contrib.contenttypes',
  40. 'django.contrib.sessions',
  41. 'django.contrib.sites',
  42. ...
  43. 'axes',
  44. ...
  45. )
  46. Next, install the ``FailedLoginMiddleware`` middleware::
  47. MIDDLEWARE_CLASSES = (
  48. 'django.middleware.common.CommonMiddleware',
  49. 'django.contrib.sessions.middleware.SessionMiddleware',
  50. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  51. 'axes.middleware.FailedLoginMiddleware'
  52. )
  53. Run ``python manage.py syncdb``. This creates the appropriate tables in your database
  54. that are necessary for operation.
  55. Customizing Axes
  56. ----------------
  57. You have a couple options available to you to customize ``django-axes`` a bit.
  58. These should be defined in your ``settings.py`` file.
  59. * ``AXES_LOGIN_FAILURE_LIMIT``: The number of login attempts allowed before a
  60. record is created for the failed logins. Default: ``3``
  61. * ``AXES_LOCK_OUT_AT_FAILURE``: After the number of allowed login attempts
  62. are exceeded, should we lock out this IP (and optional user agent)?
  63. Default: ``True``
  64. * ``AXES_USE_USER_AGENT``: If ``True``, lock out / log based on an IP address
  65. AND a user agent. This means requests from different user agents but from
  66. the same IP are treated differently. Default: ``False``
  67. * ``AXES_COOLOFF_TIME``: If set, defines a period of inactivity after which
  68. old failed login attempts will be forgotten. Can be set to a python
  69. timedelta object or an integer. If an integer, will be interpreted as a
  70. number of hours. Default: ``None``
  71. * ``AXES_LOGGER``: If set, specifies a logging mechanism for axes to use.
  72. Default: ``'axes.watch_login'``
  73. * ``AXES_LOCKOUT_TEMPLATE``: If set, specifies a template to render when a
  74. user is locked out. Template receives cooloff_time and failure_limit as
  75. context variables. Default: ``None``
  76. * ``AXES_LOCKOUT_URL``: If set, specifies a URL to redirect to on lockout. If
  77. both AXES_LOCKOUT_TEMPLATE and AXES_LOCKOUT_URL are set, the template will
  78. be used. Default: ``None``
  79. * ``AXES_VERBOSE``: If ``True``, you'll see slightly more logging for Axes.
  80. Default: ``True``
  81. * ``AXES_USERNAME_FORM_FIELD``: the name of the form field that contains your
  82. users usernames. Default: ``username``
  83. * ``AXES_LOCK_OUT_BY_COMBINATION_USER_AND_IP``: If ``True`` prevents to login
  84. from IP under particular user if attempts limit exceed, otherwise lock out
  85. based on IP.
  86. Default: ``False``
  87. Usage
  88. =====
  89. Using ``django-axes`` is extremely simple. Once you install the application
  90. and the middleware, all you need to do is periodically check the Access
  91. Attempts section of the admin.
  92. By default, django-axes will lock out repeated attempts from the same IP
  93. address. You can allow this IP to attempt again by deleting the relevant
  94. ``AccessAttempt`` records in the admin.
  95. You can also use the ``axes_reset`` management command using Django's
  96. ``manage.py``.
  97. * ``manage.py axes_reset`` will reset all lockouts and access records.
  98. * ``manage.py axes_reset ip`` will clear lockout/records for ip
  99. In your code, you can use ``from axes.utils import reset``.
  100. * ``reset()`` will reset all lockouts and access records.
  101. * ``reset(ip=ip)`` will clear lockout/records for ip
  102. * ``reset(username=username)`` will clear lockout/records for username
  103. Issues
  104. ======
  105. Not being locked out after failed attempts
  106. ------------------------------------------
  107. You may find that Axes is not capturing your failed login attempts. It may be that you need to manually add watch_login to your login url.
  108. For example, in your urls.py::
  109. ...
  110. from django.contrib.auth.views import login, logout, password_change
  111. from axes.decorators import watch_login
  112. ...
  113. urlpatterns = patterns('',
  114. (r'^login/$', watch_login(login), {'template_name': 'auth/login.html'}),
  115. ...
  116. Locked out without reason
  117. -------------------------
  118. It may happen that you have suddenly become locked out without a single failed
  119. attempt. One possible reason is that you are using some custom login form and the
  120. username field is named something different than "username", e.g. "email". This
  121. leads to all users attempts being lumped together. To fix this add the following
  122. to your settings:
  123. AXES_USERNAME_FORM_FIELD = "email"