Skip to main content

Middleware to require login for all Django URLs

Project description

Django Require Login

Build Status codecov PyPI Code style: black License

Forked from django-stronghold

Require login on all your django URLs by default

Supported Versions

  • Python 3.10, 3.11, 3.12
  • Django 4.2, 5.0

Installation and Setup

Install via pip.

pip install django-require-login

Then add the middleware to your MIDDLEWARE in your Django settings file

MIDDLEWARE = [
    #...
    "django_require_login.middleware.LoginRequiredMiddleware",
]

After adding the middleware, all your Django views will default to login required.

If your LOGIN_URL and LOGOUT_REDIRECT_URL contain a named URL pattern add REQUIRE_LOGIN_PUBLIC_NAMED_URLS to your settings file with your LOGIN_URL and LOGOUT_REDIRECT_URL

REQUIRE_LOGIN_PUBLIC_NAMED_URLS = (LOGIN_URL, LOGOUT_REDIRECT_URL)

If your LOGIN_URL and LOGOUT_REDIRECT_URL don't contain a named URL pattern add REQUIRE_LOGIN_PUBLIC_URLS to your settings file with your LOGIN_URL and LOGOUT_REDIRECT_URL

REQUIRE_LOGIN_PUBLIC_URLS = (LOGIN_URL, LOGOUT_REDIRECT_URL)

Usage

To make a view public again you can use the public decorator:

For function based views

from django_require_login.decorators import public
from django.http import HttpResponse


@public
def my_view(request):
    return HttpResponse("Public")

For class based views (decorator)

from django.utils.decorators import method_decorator
from django_require_login.decorators import public
from django.views.generic import View
from django.http import HttpResponse


class SomeView(View):
    def get(self, request, *args, **kwargs):
        return HttpResponse("Public view")
    
    @method_decorator(public)
    def dispatch(self, *args, **kwargs):
        return super().dispatch(*args, **kwargs)

For class based views (mixin)

from django_require_login.mixins import PublicViewMixin
from django.views.generic import View


class SomeView(PublicViewMixin, View):
	pass

Configuration (optional)

You can add a tuple of url regexes in your settings file with the REQUIRE_LOGIN_PUBLIC_URLS setting. Any url that matches against these patterns will be made public without using the @public decorator.

REQUIRE_LOGIN_PUBLIC_URLS

Default:

REQUIRE_LOGIN_PUBLIC_URLS = ()

Development Defaults

If DEBUG is True, REQUIRE_LOGIN_PUBLIC_URLS contains:

from django.conf import settings

(
    r'{}.+$'.format(settings.STATIC_URL),
    r'{}.+$'.format(settings.MEDIA_URL),
)

This is additive to your settings to support serving static files and media files from the development server. It does not replace any settings you may have in REQUIRE_LOGIN_PUBLIC_URLS.

Note: Public URL regexes are matched against HttpRequest.path_info.

REQUIRE_LOGIN_PUBLIC_NAMED_URLS

You can add a tuple of url names in your settings file with the REQUIRE_LOGIN_PUBLIC_NAMED_URLS setting. Names in this setting will be reversed using django.urls.reverse and any url matching the output of the reverse call will be made public without using the @public decorator:

Default:

REQUIRE_LOGIN_PUBLIC_NAMED_URLS = ()

REQUIRE_LOGIN_USER_TEST_FUNC

Optionally, set REQUIRE_LOGIN_USER_TEST_FUNC to a callable to limit access to users that pass a custom test. The callback receives a User object and should return True if the user is authorized. This is equivalent to decorating a view with user_passes_test.

Example:

REQUIRE_LOGIN_USER_TEST_FUNC = lambda user: user.is_staff

Default:

REQUIRE_LOGIN_USER_TEST_FUNC = lambda user: user.is_authenticated

Integration with Django REST Framework

Django REST Framework is not part of Django and uses its own authentication system. For this reason, you need to make all of your DRF views public and rely on DRF's authentication system.

Example

Assuming all your DRF views live under /api/ you can make them all public using a regex:

REQUIRE_LOGIN_PUBLIC_URLS = (r"^/api/.*",)

Security

If you believe you've found a bug with security implications, please do not disclose this issue in a public forum.

Email us at support@laac.dev

Contribute

See CONTRIBUTING.md

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_require_login-1.1.4.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_require_login-1.1.4-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file django_require_login-1.1.4.tar.gz.

File metadata

  • Download URL: django_require_login-1.1.4.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.11.3 Linux/5.15.0-92-generic

File hashes

Hashes for django_require_login-1.1.4.tar.gz
Algorithm Hash digest
SHA256 0320128ace94561afbca3f3580bce6df4b8a90ba20313b43971bc26ed2e4cce0
MD5 d2019126241bcc387c9cd849788d9e9f
BLAKE2b-256 3890ebe23c4e2cb9c0d883b92a98aae9b1d45b8e0e7e385cbe7b684300a36588

See more details on using hashes here.

File details

Details for the file django_require_login-1.1.4-py3-none-any.whl.

File metadata

  • Download URL: django_require_login-1.1.4-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.11.3 Linux/5.15.0-92-generic

File hashes

Hashes for django_require_login-1.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 71e2e85c6148985afa2cc8a52afa9e3a0ec3f77bb6766d4d9c618529e43f7856
MD5 ad4b6ba0509a6667391bc9c72eeaea82
BLAKE2b-256 c0edb0cf23e38b2fc0f5c19d1d1a169b2103558149fefae5efbd03d7c948f4a1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page