{"id":16878288,"url":"https://github.com/caselit/falcon-auth2","last_synced_at":"2025-03-22T07:32:06.000Z","repository":{"id":45560896,"uuid":"260670809","full_name":"CaselIT/falcon-auth2","owner":"CaselIT","description":"Falcon authentication middleware that supports multiple authentication schemes.","archived":false,"fork":false,"pushed_at":"2021-12-08T17:29:38.000Z","size":149,"stargazers_count":13,"open_issues_count":2,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-18T09:37:43.432Z","etag":null,"topics":["auth","authentication","falcon","middleware"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CaselIT.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-05-02T11:16:54.000Z","updated_at":"2025-01-17T12:23:38.000Z","dependencies_parsed_at":"2022-08-19T11:53:10.138Z","dependency_job_id":null,"html_url":"https://github.com/CaselIT/falcon-auth2","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CaselIT%2Ffalcon-auth2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CaselIT%2Ffalcon-auth2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CaselIT%2Ffalcon-auth2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CaselIT%2Ffalcon-auth2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CaselIT","download_url":"https://codeload.github.com/CaselIT/falcon-auth2/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244925175,"owners_count":20532873,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["auth","authentication","falcon","middleware"],"created_at":"2024-10-13T15:47:58.607Z","updated_at":"2025-03-22T07:32:05.631Z","avatar_url":"https://github.com/CaselIT.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# falcon-auth2\n\n[![build](https://github.com/CaselIT/falcon-auth2/workflows/Run%20tests/badge.svg)](https://github.com/CaselIT/falcon-auth2/actions?query=workflow%3A%22Run+tests%22)\n[![pypi](https://img.shields.io/pypi/v/falcon-auth2.svg)](https://pypi.python.org/pypi/falcon-auth2)\n[![Documentation Status](https://readthedocs.org/projects/falcon-auth2/badge/?version=latest)](https://falcon-auth2.readthedocs.io/en/latest/?badge=latest)\n[![codecov](https://codecov.io/gh/CaselIT/falcon-auth2/branch/master/graph/badge.svg)](https://codecov.io/gh/CaselIT/falcon-auth2)\n\nFalcon authentication middleware that supports multiple authentication schemes. \n\n## Install\n\n```sh\n$ pip install falcon-auth2[jwt]\n```\nThe above will install `falcon-auth2` and also the dependencies to use the ``JWT`` authentication backend.  \nIf you plan to use async falcon with ASGI run:\n```sh\n$ pip install falcon-auth2[jwt, async]\n```\n\n## Usage\n\nThis 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``.\nWhen using falcon v3+, the middleware also supports async execution.\n\nSee [readme_example](./examples/readme_example.py) and [readme_example_async](./examples/readme_example_async.py) for complete examples.\n\n```py\nimport falcon\nfrom falcon_auth2 import AuthMiddleware\nfrom falcon_auth2.backends import BasicAuthBackend\n\ndef user_loader(attributes, user, password):\n    if authenticate(user, password):\n        return {\"username\": user}\n    return None\n\nauth_backend = BasicAuthBackend(user_loader)\nauth_middleware = AuthMiddleware(auth_backend)\n# use falcon.API in falcon 2\napp = falcon.App(middleware=[auth_middleware])\n\nclass HelloResource:\n    def on_get(self, req, resp):\n        # req.context.auth is of the form:\n        #\n        #   {\n        #       'backend': \u003cinstance of the backend that performed the authentication\u003e,\n        #       'user': \u003cuser object retrieved from the user_loader callable\u003e,\n        #       '\u003cbackend specific item\u003e': \u003csome extra data that may be added by the backend\u003e,\n        #       ...\n        #   }\n        user = req.context.auth[\"user\"]\n        resp.media = {\"message\": f\"Hello {user['username']}\"}\n\napp.add_route('/hello', HelloResource())\n```\n\n### Override Authentication for a resource\n\nThe 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.\n\n```py\nfrom falcon_auth2 import HeaderGetter\nfrom falcon_auth2.backends import GenericAuthBackend\n\ndef user_header_loader(attr, user_header):\n    # authenticate the user with the user_header\n    return user_header\n\nclass GenericResource:\n    auth = {\n        \"backend\": GenericAuthBackend(user_header_loader, getter=HeaderGetter(\"User\")),\n        \"exempt_methods\": [\"GET\"],\n    }\n\n    def on_get(self, req, resp):\n        resp.media = {\"type\": \"No authentication for GET\"}\n\n    def on_post(self, req, resp):\n        resp.media = {\"info\": f\"User header {req.context.auth['user']}\"}\n\napp.add_route(\"/generic\", GenericResource())\n\nclass NoAuthResource:\n    auth = {\"auth_disabled\": True}\n\n    def on_get(self, req, resp):\n        resp.text = \"No auth in this resource\"\n\n    def on_post(self, req, resp):\n        resp.text = \"No auth in this resource\"\n\napp.add_route(\"/no-auth\", NoAuthResource())\n\n```\n\n## Included Authentication backends\n\n#### `BasicAuthBackend`\n\nImplements [HTTP Basic Authentication](https://tools.ietf.org/html/rfc7617) where clients should authenticate by passing the credential in the format ``username:password`` encoded in ``base64`` in the ``Authorization`` HTTP header.\n\n#### `JWTAuthBackend`\n\nImplements [JSON Web Token (JWT) standard](https://tools.ietf.org/html/rfc7519) where clients should authenticate by passing the token key in the `Authorization` HTTP header. This backend makes use of the\n[Authlib](https://authlib.org) library.\n\n#### `GenericAuthBackend`\n\nGeneric 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.\n\n#### `NoAuthBackend`\n\nBackend that does not perform any authentication check and may be useful to provide a fallback for unauthenticated users when combined with `MultiAuthBackend`.\n\n### Meta Authentication backends\n\n#### `CallBackBackend`\n\nNotifies when another backend has success and/or fails to authenticate a request. This backend delegates all the authentication actions to the provided ``backend``.\n\n#### `MultiAuthBackend`\n\nBackend used to combine multiple authentication backends.\nThis backend successfully authenticates a request if one of the provided backends can authenticate the request.\n\n## About Falcon\n\n[Falcon](https://falconframework.org) is the minimalist web API framework\nfor building reliable, correct, and high-performance REST APIs, microservices,\nproxies, and app backends in Python.\n\n## Thanks\n\nThis package was inspired by [falcon-auth](https://github.com/loanzen/falcon-auth) and [falcon-authentication](https://github.com/jcwilson/falcon-authentication) packages.\n\n## License\n\n`falcon-auth2` is distributed under the [Apache-2.0 License](https://github.com/CaselIT/falcon-auth2/blob/master/LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcaselit%2Ffalcon-auth2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcaselit%2Ffalcon-auth2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcaselit%2Ffalcon-auth2/lists"}