Basic auth utilities for Django.
Project description
Basic auth utilities for Django.
Requires
Tested under…
Python
2.7
3.6
3.7
Django
1.11
2.1
2.2
Installation
pip install django-basicauth
Usage
from basicauth.decorators import basic_auth_required
@basic_auth_required
def myview(request):
...
or by a middleware.
MIDDLEWARE = (
'basicauth.middleware.BasicAuthMiddleware',
...
)
The name of MIDDLEWARE settings is MIDDLEWARE_CLASSES on Django 1.8.
Basic Auth for specific requestno only
To apply basic auth for specific requests, Use target_test argument.
In the below code, anonymous users will be required Basic Auth Authenticated users can pass it without Basic … header.
from basicauth.decorators import basic_auth_required
@basic_auth_required(
target_test=lambda request: not request.user.is_authenticated
)
def myview(request):
...
target_test accepts typing.Callable[[HttpRequest], bool], and if the callable returns True, Basic Auth will be required.
Applying decorator to CBVs
To apply @basic_auth_required decorator to Class Based Views, use django.utils.decorators.method_decorator.
from django.utils.decorators import method_decorator
from basicauth.decorators import basic_auth_required
@method_decorator(basic_auth_required, name='dispatch')
class YourView(TemplateView):
template_name = "my-template.html"
Settings
BASICAUTH_USERS (required): Dictionary including keys as username and values as passwords.
BASICAUTH_REALM: realm string, default is “Secure resource”.
BASICAUTH_DISABLE: Disable all of barriers by this library.
Changes
0.5.1 (2018-08-06)
- Added supporting Django2.1
Thanks @rhymes
0.5 (2018-05-09)
- Added supporting Django2.0
Thanks @timheap
0.4.2 (2017-11-27)
- Fixed to avoid timing attacks
Lots of thanks for Hugo Castilho
0.4.1 (2017-10-20)
Added target_test argument for the decorator.
0.4 (2017-09-30)
Supported Python 2.7
Dropped Django1.9
Supported Django 1.10+ style middleware
0.3 (2017-08-28)
Added BASICAUTH_DISABLE setting
- Changed API of basicauthutils.validate_request
Fixed to return True/False
Adding REMOTE_USER by this function
0.2.1 (2017-08-28)
Officially supported Django 1.11, 1.10, 1.9
0.2 (2016-03-31)
Added BasicAuthMiddleware https://github.com/hirokiky/django-basicauth/pull/3
0.1 (2015-04-20)
Initial
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for django_basicauth-0.5.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 295adfbf6d30ab03d2000131da00f388e43d31f2ee7e9a0b51c4d7eb51896f7f |
|
MD5 | c61327f72c2a5da0fe8c2976e54beabf |
|
BLAKE2b-256 | fca02dddd7a31aebd54120c11893513b4b8d1002dad3e121cdc600197c588603 |
Hashes for django_basicauth-0.5.2-py2-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d272a9b6b807d96b1e3b07ebf123bac90c1c72dada517ca9e1e4238a1236cdaf |
|
MD5 | 8896f5b4c0568f8889522f53cc97f732 |
|
BLAKE2b-256 | dc1b7467865abe59c320e979b134fc1691f3891bcbd13045911f77a5fe416211 |