Skip to main content

falcon-auth2

build pypi Documentation Status codecov

Falcon authentication middleware that supports multiple authentication schemes.

Install

$ pip install falcon-auth2[jwt]

The above will install falcon-auth2 and also the dependencies to use the JWT authentication backend.
If you plan to use async falcon with ASGI run:

$ pip install falcon-auth2[jwt, async]

Usage

This package provides a falcon middleware to authenticate incoming requests using the selected authentication backend. The middleware allows excluding some routes or method from authentication. After a successful authentication the middleware adds the user identified by the request to the request context. When using falcon v3+, the middleware also supports async execution.

See readme_example and readme_example_async for complete examples.

import falcon
from falcon_auth2 import AuthMiddleware, RequestAttributes
from falcon_auth2.backends import BasicAuthBackend

def user_loader(attributes: RequestAttributes, user: str, password: str) -> dict[str, str] | None:
    if authenticate(user, password):
        return {"username": user}
    return None

auth_backend = BasicAuthBackend(user_loader)
auth_middleware = AuthMiddleware(auth_backend)
# use falcon.API in falcon 2
app = falcon.App(middleware=[auth_middleware])

class HelloResource:
    def on_get(self, req: falcon.Request, resp: falcon.Response) -> None:
        # req.context.auth is of the form:
        #
        #   {
        #       'backend': <instance of the backend that performed the authentication>,
        #       'user': <user object retrieved from the user_loader callable>,
        #       '<backend specific item>': <some extra data that may be added by the backend>,
        #       ...
        #   }
        user = req.context.auth["user"]
        resp.media = {"message": f"Hello {user['username']}"}

app.add_route('/hello', HelloResource())

Override Authentication for a resource

The middleware allows each resource to customize the backend used for authentication or the excluded methods. A resource can also specify that does not need authentication.

import falcon
from falcon_auth2 import HeaderGetter, RequestAttributes
from falcon_auth2.backends import GenericAuthBackend

def user_header_loader(attributes: RequestAttributes, user_header: str) -> str | None:
    # authenticate the user with the user_header
    return user_header

class GenericResource:
    auth = {
        "backend": GenericAuthBackend(user_header_loader, getter=HeaderGetter("User")),
        "exempt_methods": ["GET"],
    }

    def on_get(self, req: falcon.Request, resp: falcon.Response) -> None:
        resp.media = {"type": "No authentication for GET"}

    def on_post(self, req: falcon.Request, resp: falcon.Response) -> None:
        resp.media = {"info": f"User header {req.context.auth['user']}"}

app.add_route("/generic", GenericResource())

class NoAuthResource:
    auth = {"auth_disabled": True}

    def on_get(self, req: falcon.Request, resp: falcon.Response) -> None:
        resp.text = "No auth in this resource"

    def on_post(self, req: falcon.Request, resp: falcon.Response) -> None:
        resp.text = "No auth in this resource"

app.add_route("/no-auth", NoAuthResource())

Included Authentication backends

BasicAuthBackend

Implements HTTP Basic Authentication where clients should authenticate by passing the credential in the format username:password encoded in base64 in the Authorization HTTP header.

JWTAuthBackend

Implements JSON Web Token (JWT) standard where clients should authenticate by passing the token key in the Authorization HTTP header. This backend makes use of the joserfc library.

GenericAuthBackend

Generic authentication backend that delegates the verification of the authentication information from the request to the user_loader callable. This backend can be used to quickly implement custom authentication schemes or as an adapter to other authentication libraries.

NoAuthBackend

Backend that does not perform any authentication check and may be useful to provide a fallback for unauthenticated users when combined with MultiAuthBackend.

Meta Authentication backends

CallBackBackend

Notifies when another backend has success and/or fails to authenticate a request. This backend delegates all the authentication actions to the provided backend.

MultiAuthBackend

Backend used to combine multiple authentication backends. This backend successfully authenticates a request if one of the provided backends can authenticate the request.

About Falcon

Falcon is the minimalist web API framework for building reliable, correct, and high-performance REST APIs, microservices, proxies, and app backends in Python.

Thanks

This package was inspired by falcon-auth and falcon-authentication packages.

License

falcon-auth2 is distributed under the Apache-2.0 License.

Release files for falcon-auth2 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for falcon-auth2 0.2.0
File Size Uploaded
falcon_auth2-0.2.0.tar.gz 21.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for falcon-auth2 0.2.0
File Interpreter ABI Platform
falcon_auth2-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 46.7 kB

Release files / falcon_auth2-0.2.0.tar.gz

Download URL falcon_auth2-0.2.0.tar.gz
Size 21.3 kB
Tags Source
SHA-256 checksum
How to use checksums
4ceb3686a29b9ccc88e2e7b2f59968fcd1efb843202a3c3e307144162f184e95
BLAKE2b-256 checksum
How to use checksums
cd7acae23de962f8a8fb933feac3b62cea02d3b300d30c3a7fb52b8774877b76
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 29, 2026.

Transparency log

Release files / falcon_auth2-0.2.0-py3-none-any.whl

Download URL falcon_auth2-0.2.0-py3-none-any.whl
Size 25.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
15b18ff75e393273b06a1219eb1387ed887c7ba5b16dda5c51d717efe97f1c0c
BLAKE2b-256 checksum
How to use checksums
22ca19c8a4d4a60acb9e79aee740cc6ae5b93bb2aab08710daf83a8c9c3a283a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 29, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page