Snippet List
Requires to install "basicauth" package, which does basic-auth header encoding/decoding cleanly according to RFCs.
Could be improved to return a "realm" in case of http401, like in https://djangosnippets.org/snippets/1720/, although I'm not sure it's really useful in django usecases.
- middleware
- basic
- authentication
- auth
- http-authorization
A very basic Basic Auth middleware that uses a username/password defined in your settings.py as `BASICAUTH_USERNAME` and `BASICAUTH_PASSWORD`. Does not use Django auth. Handy for quickly securing an entire site during development, for example.
In settings.py:
BASICAUTH_USERNAME = 'user'
BASICAUTH_PASSWORD = 'pass'
MIDDLEWARE_CLASSES = (
'app.module.BasicAuthMiddleware',
#all other middleware
)
- middleware
- basic
- authentication
- http-authorization
This is a somewhat simpler alternative to [http://www.djangosnippets.org/snippets/243/](http://www.djangosnippets.org/snippets/243/) that does not return a 401 response. It's meant to be used along with the login_required decorator as an alternative way to authenticate to REST-enabled views.
Usage:
@http_basic_auth
@login_required
def my_view(request):
...
If an HTTP basic auth header is provided, the request will be authenticated before the login_required check happens. Otherwise, the normal redirect to login page occurs.
- basic
- authentication
- decorator
- auth
A simple decorator that requires a user to be logged in. If they are not logged in the request is examined for a 'authorization' header.
If the header is present it is tested for basic authentication and the user is logged in with the provided credentials.
If the header is not present a http 401 is sent back to the requestor to provide credentials.
The purpose of this is that in several django projects I have needed several specific views that need to support basic authentication, yet the web site as a whole used django's provided authentication.
The uses for this are for urls that are access programmatically such as by rss feed readers, yet the view requires a user to be logged in. Many rss readers support supplying the authentication credentials via http basic auth (and they do NOT support a redirect to a form where they post a username/password.)
Use is simple:
`
@logged_in_or_basicauth
def your_view:
...
`
You can provide the name of the realm to ask for authentication within.
- basic
- authentication
- decorator
A patch (against django svn trunk [4649](http://code.djangoproject.com/browser/django/trunk/?rev=4649)) that allows users to log in with Basic HTTP Authentication i.s.o. login forms using some simple middleware (entire patch is ~50 lines). I was unaware of http://code.djangoproject.com/wiki/GenericAuthorization so I'm not sure about its usefulness in the long run.
You can enable it by including 'django.contrib.auth.middleware.BasicAuthenticationMiddleware' in your MIDDLEWARE_CLASSES and then adding the following lines in your settings.py:
BASIC_WWW_AUTHENTICATION = True
WWW_AUTHENTICATION_REALM = "djangolures.com"
Updated: See also http://code.djangoproject.com/ticket/3609 (patch now availble here as well).
- middleware
- django
- http
- basic
- authentication
6 snippets posted so far.