Skip to main content

Falcon authentication middleware that supports multiple authentication types.

Project description

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
from falcon_auth2.backends import BasicAuthBackend

def user_loader(attributes, user, password):
    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, resp):
        # 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.

from falcon_auth2 import HeaderGetter
from falcon_auth2.backends import GenericAuthBackend

def user_header_loader(attr, user_header):
    # 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, resp):
        resp.media = {"type": "No authentication for GET"}

    def on_post(self, req, resp):
        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, resp):
        resp.text = "No auth in this resource"

    def on_post(self, req, resp):
        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 Authlib 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.

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

falcon-auth2-0.1.0.tar.gz (24.5 kB view details)

Uploaded Source

Built Distribution

falcon_auth2-0.1.0-py3-none-any.whl (24.9 kB view details)

Uploaded Python 3

File details

Details for the file falcon-auth2-0.1.0.tar.gz.

File metadata

  • Download URL: falcon-auth2-0.1.0.tar.gz
  • Upload date:
  • Size: 24.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for falcon-auth2-0.1.0.tar.gz
Algorithm Hash digest
SHA256 87ada4fcbe34a5768f7eac13e8b1897d4532b90cfe7af44f47bf11d070b5eed4
MD5 b941e2751a17728a2a4cb929b7b0cfad
BLAKE2b-256 ecf2fc65ee7245915b3ac5c2ac3759441b96935964bdb4e446a1a9e573335593

See more details on using hashes here.

File details

Details for the file falcon_auth2-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: falcon_auth2-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 24.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for falcon_auth2-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3b86fc6001941a95ad9ba18953017237c5cda1fdd35b6fd4209d7a8870bebccf
MD5 b9dc6cb1baa963145dad991c1943fc0a
BLAKE2b-256 56df08aacf0c33ce887932842f7d0d8c677fd68b8e8bee72428979a78a9f5436

See more details on using hashes here.

Supported by

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