/README.rst

https://gitlab.com/pobk/django-urlcrypt · ReStructuredText · 100 lines · 63 code · 37 blank · 0 comment · 0 complexity · e5da881edf35d2d348cdbfde10235863 MD5 · raw file

  1. django-urlcrypt
  2. =================
  3. django-urlcrypt encrypts information in urls, such as login credentials.
  4. For example, assume I have url patterns that looks like this::
  5. urlpatterns = patterns('',
  6. url(r'^inbox/$', 'message_inbox', name='message_inbox'),
  7. (r'^r/', include('urlcrypt.urls')),
  8. )
  9. I can use django-urlcrypt to generate a url for a user that looks like::
  10. http://www.mydomain.com/r/TkNJBkNFAghDWkdFGPUAQEfcDUJfEBIREgEUFl1BQ18IQkdDUUcPSh4ADAYAWhYKHh8KHBsHEw
  11. and will automatically log that person in and redirects them to ``/inbox/``.
  12. Installation
  13. ************
  14. 1. ``easy_install django-urlcrypt`` or ``pip install django-urlcrypt``
  15. 2. Add ``urlcrypt`` to your ``INSTALLED_APPS``
  16. 3. In settings.py add ``'urlcrypt.auth_backends.UrlCryptBackend'`` to ``AUTHENTICATION_BACKENDS``
  17. 4. In urls.py add::
  18. (r'^r/', include('urlcrypt.urls')),
  19. 5. **(recommended)** If you wish to use RSA encryption on your tokens, generate a private key with ``ssh-keygen -t rsa -f <path to private key>`` if you don't already have one, and then set the path to the private key as URLCRYPT_PRIVATE_KEY_PATH. RSA encryption makes the token much longer but is more secure. The ``pycrypto`` library is required.
  20. Usage
  21. ******
  22. In a view::
  23. from django.core.urlresolvers import reverse
  24. from urlcrypt import lib as urlcrypt
  25. token = urlcrypt.generate_login_token(user, reverse('message_inbox'))
  26. encoded_url = reverse('urlcrypt_redirect', args=(token,))
  27. # yours will look slightly different because you have a different SECRET_KEY, but approximately
  28. # encoded_url == /r/TkNJBkNFAghDWkdFGPUAQEfcDUJfEBIREgEUFl1BQ18IQkdDUUcPSh4ADAYAWhYKHh8KHBsHEw
  29. In a template::
  30. {% load urlcrypt_tags %}
  31. <a href="{% encoded_url user message_inbox %}">click me to log in as {{user.username}} and go to {% url message_inbox %}</a>
  32. Advanced lib usage::
  33. from urlcrypt import lib as urlcrypt
  34. message = {
  35. 'url': u'/users/following/',
  36. 'user_id': '12345'
  37. }
  38. token = urlcrypt.encode_token((message['user_id'], message['url']))
  39. decoded_message = urlcrypt.decode_token(token, ('user_id', 'url', 'timestamp'))
  40. >>> print token
  41. TkNJBkNFAghDWkdFGPUAQEfcDUJfEBIREgEUFl1BQ18IQkdDUUcPSh4ADAYAWhYKHh8KHBsHEw
  42. >>> print decoded_message
  43. {'url': '/users/following/', 'user_id': '12345'}
  44. Settings
  45. ********
  46. - ``URLCRYPT_LOGIN_URL``
  47. - default: ``LOGIN_URL``
  48. - If urlcrypt authentication fails, redirects to ``URLCRYPT_LOGIN_URL``.
  49. - ``URLCRYPT_RATE_LIMIT``
  50. - default: ``60``
  51. - The number of urlcrypt requests a unique visitor is allowed to make per minute.
  52. - ``URLCRYPT_PRIVATE_KEY_PATH``
  53. - default: ``None``
  54. - The path to the RSA private key file in PEM format. If ``None``, RSA encryption will not be used.
  55. - ``RUNNING_TESTS``
  56. - default: ``False``
  57. - Set ``RUNNING_TESTS`` to True when running the urlcrypt tests.
  58. Credits
  59. ********
  60. `David Ziegler`_
  61. `Christopher Hesse`_
  62. .. _`David Ziegler`: http://github.com/dziegler
  63. .. _`Christopher Hesse`: http://github.com/cshesse