Package: falcon-auth2
falcon-auth2Falcon authentication middleware that supports multiple authentication schemes.Install$pipinstallfalcon-auth2[jwt]The above will installfalcon-auth2and also the dependencies to use theJWTauthentication backend.If you plan to use async falcon with ASGI run:$pipinstallfalcon-auth2[jwt,async]UsageThis 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 therequest context.
When using falcon v3+, the middleware also supports async execution.Seereadme_exampleandreadme_example_asyncfor complete examples.importfalconfromfalcon_auth2importAuthMiddlewarefromfalcon_auth2.backendsimportBasicAuthBackenddefuser_loader(attributes,user,password):ifauthenticate(user,password):return{"username":user}returnNoneauth_backend=BasicAuthBackend(user_loader)auth_middleware=AuthMiddleware(auth_backend)# use falcon.API in falcon 2app=falcon.App(middleware=[auth_middleware])classHelloResource:defon_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 resourceThe 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.fromfalcon_auth2importHeaderGetterfromfalcon_auth2.backendsimportGenericAuthBackenddefuser_header_loader(attr,user_header):# authenticate the user with the user_headerreturnuser_headerclassGenericResource:auth={"backend":GenericAuthBackend(user_header_loader,getter=HeaderGetter("User")),"exempt_methods":["GET"],}defon_get(self,req,resp):resp.media={"type":"No authentication for GET"}defon_post(self,req,resp):resp.media={"info":f"User header{req.context.auth['user']}"}app.add_route("/generic",GenericResource())classNoAuthResource:auth={"auth_disabled":True}defon_get(self,req,resp):resp.text="No auth in this resource"defon_post(self,req,resp):resp.text="No auth in this resource"app.add_route("/no-auth",NoAuthResource())Included Authentication backendsBasicAuthBackendImplementsHTTP Basic Authenticationwhere clients should authenticate by passing the credential in the formatusername:passwordencoded inbase64in theAuthorizationHTTP header.JWTAuthBackendImplementsJSON Web Token (JWT) standardwhere clients should authenticate by passing the token key in theAuthorizationHTTP header. This backend makes use of theAuthliblibrary.GenericAuthBackendGeneric authentication backend that delegates the verification of the authentication information from the request to theuser_loadercallable. This backend can be used to quickly implement custom authentication schemes or as an adapter to other authentication libraries.NoAuthBackendBackend that does not perform any authentication check and may be useful to provide a fallback for unauthenticated users when combined withMultiAuthBackend.Meta Authentication backendsCallBackBackendNotifies when another backend has success and/or fails to authenticate a request. This backend delegates all the authentication actions to the providedbackend.MultiAuthBackendBackend used to combine multiple authentication backends.
This backend successfully authenticates a request if one of the provided backends can authenticate the request.About FalconFalconis the minimalist web API framework
for building reliable, correct, and high-performance REST APIs, microservices,
proxies, and app backends in Python.ThanksThis package was inspired byfalcon-authandfalcon-authenticationpackages.Licensefalcon-auth2is distributed under theApache-2.0 License.