Code Monkey home page Code Monkey logo

django-basicauth's Introduction

django-basicauth

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 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.

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.

django-basicauth's People

Contributors

hirokiky avatar rbarrois avatar therefromhere avatar c-bata avatar rhymes avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.