{"id":13501260,"url":"https://github.com/code-specialist/fastapi-auth-middleware","last_synced_at":"2025-04-09T17:22:38.696Z","repository":{"id":40543157,"uuid":"454062018","full_name":"code-specialist/fastapi-auth-middleware","owner":"code-specialist","description":"Lightweight auth middleware for FastAPI that just works. Fits most auth workflows with only a few lines of code","archived":false,"fork":false,"pushed_at":"2022-12-21T09:23:58.000Z","size":526,"stargazers_count":92,"open_issues_count":6,"forks_count":7,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-02T03:35:43.839Z","etag":null,"topics":["auth","authentication","authorization","fastapi","middleware"],"latest_commit_sha":null,"homepage":"https://code-specialist.github.io/fastapi-auth-middleware/","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/code-specialist.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2022-01-31T15:33:02.000Z","updated_at":"2025-03-15T05:18:13.000Z","dependencies_parsed_at":"2023-01-30T03:15:40.650Z","dependency_job_id":null,"html_url":"https://github.com/code-specialist/fastapi-auth-middleware","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/code-specialist%2Ffastapi-auth-middleware","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/code-specialist%2Ffastapi-auth-middleware/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/code-specialist%2Ffastapi-auth-middleware/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/code-specialist%2Ffastapi-auth-middleware/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/code-specialist","download_url":"https://codeload.github.com/code-specialist/fastapi-auth-middleware/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248075268,"owners_count":21043556,"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","authorization","fastapi","middleware"],"created_at":"2024-07-31T22:01:30.856Z","updated_at":"2025-04-09T17:22:38.658Z","avatar_url":"https://github.com/code-specialist.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# FastAPI Auth Middleware\n\n[![PyTest](https://github.com/code-specialist/fastapi-auth-middleware/actions/workflows/testing.yaml/badge.svg)](https://github.com/code-specialist/fastapi-auth-middleware/actions/workflows/testing.yaml)\n[![codecov](https://codecov.io/gh/code-specialist/fastapi-auth-middleware/branch/main/graph/badge.svg?token=JS7C57FCCD)](https://codecov.io/gh/code-specialist/fastapi-auth-middleware)\n![CodeFactor](https://www.codefactor.io/repository/github/code-specialist/fastapi-auth-middleware/badge)\n![Py3.7](https://img.shields.io/badge/-Python%203.7-brightgreen)\n![Py3.8](https://img.shields.io/badge/-Python%203.8-brightgreen)\n![Py3.9](https://img.shields.io/badge/-Python%203.9-brightgreen)\n![Py3.10](https://img.shields.io/badge/-Python%203.10-brightgreen)\n\nWe at [Code Specialist](https://code-specialist.com) love FastAPI for its simplicity and feature-richness. Though we were a bit staggered by the poor documentation and integration\nof auth-concepts. That's why we wrote a **FastAPI Auth Middleware**. It integrates seamlessly into FastAPI applications and requires minimum configuration. It is built\nupon [Starlette](https://www.starlette.io/) and thereby requires **no dependencies** you do not have included anyway.\n\n**Caution**: This is a middleware to plug in existing authentication. Even though we offer some sample code, this package assumes you already have a way to generate and verify\nwhatever you use, to authenticate your users. In most of the usual cases this will be an access token or bearer. For instance as in **OAuth2** or **Open ID Connect**.\n\n## Install\n\n```shell\npip install fastapi_auth_middleware\n```\n\n## Documentation\nMore detailed docs are available at [https://fastapi-auth-middleware.code-specialist.com](https://fastapi-auth-middleware.code-specialist.com).\n\n## Why FastAPI Auth Middlware?\n\n- Application or Route scoped automatic authorization and authentication with the perks of dependency injection (But without inflated signatures due to `Depends()`)\n- Lightweight without additional dependencies\n- Easy to configure\n- Easy to extend and adjust to specific needs\n- Plug-and-Play feeling\n\n## Usage\n\nThe usage of this middleware requires you to provide a single function that validates a given authorization header. The middleware will extract the content of the `Authorization`\nHTTP header and inject it into your function that returns a list of scopes and a user object. The list of scopes may be empty if you do not use any scope based concepts. The user\nobject must be a `BaseUser` or any inheriting class such as `FastAPIUser`. Thereby, your `verify_authorization_header` function must implement a signature that contains a string as\nan input and a `Tuple` of a `List of strings` and a `BaseUser` as output:\n\n```python\nfrom typing import Tuple, List\nfrom fastapi_auth_middleware import FastAPIUser\nfrom starlette.authentication import BaseUser\n\n...\n# Takes a string that will look like 'Bearer eyJhbGc...'\ndef verify_authorization_header(auth_header: str) -\u003e Tuple[List[str], BaseUser]: # Returns a Tuple of a List of scopes (string) and a BaseUser\n    user = FastAPIUser(first_name=\"Code\", last_name=\"Specialist\", user_id=1)  # Usually you would decode the JWT here and verify its signature to extract the 'sub'\n    scopes = []  # You could for instance use the scopes provided in the JWT or request them by looking up the scopes with the 'sub' somewhere\n    return scopes, user\n```\n\nThis function is then included as an keyword argument when adding the middleware to the app.\n\n```python\nfrom fastapi import FastAPI\nfrom fastapi_auth_middleware import AuthMiddleware\n\n...\n\napp = FastAPI()\napp.add_middleware(AuthMiddleware, verify_header=verify_authorization_header)\n```\n\nAfter adding this middleware, all requests will pass the `verify_authorization_header` function and contain the **scopes** as well as the **user object** as injected dependencies.\nAll requests now pass the `verify_authorization_header` method. You may also verify that users posses scopes with `requires`:\n\n```python\nfrom starlette.authentication import requires\n\n...\n\n@app.get(\"/\")\n@requires([\"admin\"])  # Will result in an HTTP 401 if the scope is not matched\ndef some_endpoint():\n    ...\n```\n\nYou are also able to use the `user` object you injected on the `request` object:\n\n```python\nfrom starlette.requests import Request\n\n...\n\n@app.get('/')\ndef home(request: Request):\n    return f\"Hello {request.user.first_name}\"  # Assuming you use the FastAPIUser object\n```\n\n## Examples\n\nVarious examples on how to use this middleware are available\nat [https://fastapi-auth-middleware.code-specialist.com/examples](https://fastapi-auth-middleware.code-specialist.com/examples)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcode-specialist%2Ffastapi-auth-middleware","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcode-specialist%2Ffastapi-auth-middleware","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcode-specialist%2Ffastapi-auth-middleware/lists"}