Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hirokiky/django-basicauth
Basic auth utilities for Django.
https://github.com/hirokiky/django-basicauth
Last synced: 2 months ago
JSON representation
Basic auth utilities for Django.
- Host: GitHub
- URL: https://github.com/hirokiky/django-basicauth
- Owner: hirokiky
- License: mit
- Created: 2015-04-20T06:37:35.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2021-07-08T12:52:33.000Z (over 3 years ago)
- Last Synced: 2024-08-08T20:39:43.993Z (6 months ago)
- Language: Python
- Size: 33.2 KB
- Stars: 66
- Watchers: 3
- Forks: 27
- Open Issues: 1
-
Metadata Files:
- Readme: README.rst
- Changelog: CHANGES.txt
- License: LICENSE
Awesome Lists containing this project
- starred-awesome - django-basicauth - Basic auth utilities for Django. (Python)
README
================
django-basicauth
================Basic auth utilities for Django.
Requires
========Tested under...
* Python
* 3.6
* 3.7
* 3.8
* 3.9* Django
* 2.2
* 3.0
* 3.1Installation
============::
pip install django-basicauth
Usage
=====.. code-block:: python
from basicauth.decorators import basic_auth_required
@basic_auth_required
def myview(request):
...or by a middleware.
.. code-block:: python
MIDDLEWARE = (
'basicauth.middleware.BasicAuthMiddleware',
...
)Basic Auth for specific requests 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... code-block:: python
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``... code-block:: python
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.