{"id":28956842,"url":"https://github.com/developmentseed/fastapi-authorization-gateway","last_synced_at":"2025-06-23T21:40:59.437Z","repository":{"id":216882899,"uuid":"742604459","full_name":"developmentseed/fastapi-authorization-gateway","owner":"developmentseed","description":null,"archived":false,"fork":false,"pushed_at":"2024-08-28T11:00:17.000Z","size":61,"stargazers_count":17,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-11T22:19:37.362Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/developmentseed.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-12T21:10:29.000Z","updated_at":"2025-01-21T21:15:08.000Z","dependencies_parsed_at":"2024-01-25T14:49:12.561Z","dependency_job_id":"b45aee8c-b148-4eb3-9f74-e4fa4b9603ea","html_url":"https://github.com/developmentseed/fastapi-authorization-gateway","commit_stats":null,"previous_names":["edkeeble/stac_fastapi_authorization","edkeeble/fastapi_authorization_gateway","developmentseed/fastapi_authorization_gateway"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/developmentseed/fastapi-authorization-gateway","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developmentseed%2Ffastapi-authorization-gateway","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developmentseed%2Ffastapi-authorization-gateway/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developmentseed%2Ffastapi-authorization-gateway/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developmentseed%2Ffastapi-authorization-gateway/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/developmentseed","download_url":"https://codeload.github.com/developmentseed/fastapi-authorization-gateway/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developmentseed%2Ffastapi-authorization-gateway/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261561183,"owners_count":23177545,"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":[],"created_at":"2025-06-23T21:40:59.158Z","updated_at":"2025-06-23T21:40:59.423Z","avatar_url":"https://github.com/developmentseed.png","language":"Python","readme":"# FastAPI Authorization Gateway\n\n[![Python package CI](https://img.shields.io/github/actions/workflow/status/developmentseed/fastapi-authorization-gateway/test.yaml)](https://github.com/developmentseed/fastapi-authorization-gateway/actions/workflows/test.yaml)\n[![PyPI - Version](https://img.shields.io/pypi/v/fastapi-authorization-gateway)](https://pypi.org/project/fastapi-authorization-gateway/)\n\nThis library enables route-level authorization for FastAPI apps. It is particularly useful in cases where you need to limit access to routes that you do not directly control. For example, if you make use of a library which sets up a range of routes on your behalf (it was designed with [stac-fastapi](https://github.com/stac-utils/stac-fastapi) in mind), you can use this library to restrict access to any of those routes using authorization policies. These policies can be evaluated against a combination of route paths, methods, path parameters and query parameters. It also provides a mechanism for mutating requests before passing them on to downstream endpoints, for cases where you need to pre-emptively filter a request.\n\n## Setup\n\nInstall via pip\n\n```bash\npython -m pip install fastapi-authorization-gateway\n\n# or from source\npython -m pip install git+https://github.com/developmentseed/fastapi-authorization-gateway.git`\n```\n\n## Usage\n\nIf you are just starting out and want an end-to-end explanation of how this library works and how to integrate it into your app, please check out the [Tutorial](./docs/tutorial.md).\n\nIf you are looking for a recipe to solve a specific issue, please check out the [How To](./docs/howto.md).\n\n\n## Quickstart\n\nIf you don't want the full tutorial and just want to plug this right into your app with minimal explanation, you can use the code snippet below.\n\n```python\nfrom fastapi import Depends, Request\nfrom typing import Annotated, Optional\nfrom fastapi_authorization_gateway.auth import build_authorization_dependency\nfrom fastapi_authorization_gateway.types import Policy, RoutePermission\n\n\nasync def get_user(request: Request):\n    \"\"\"\n    Replace this with a function to retrieve a real user\n    (from a token, for example).\n    \"\"\"\n    return {\n        \"username\": \"test\"\n    }\n\n\nasync def policy_generator(request: Request, user: Annotated[dict, Depends(get_user)]) -\u003e Policy:\n    \"\"\"\n    Define your policies here based on the requesting user or, really,\n    whatever you like. This function will be injected as a dependency\n    into the authorization dependency and must return a Policy.\n    \"\"\"\n\n    # We will generate some policies that cover all routes for the app,\n    # so we need to enumerate them here.\n    all_routes: list[APIRoute] = request.app.routes\n\n    # A permission matching write access to all routes, with no constraints\n    # on path or query parameters\n    all_write = RoutePermission(\n        paths=[route.path_format for route in all_routes],\n        methods=[\"POST\", \"PUT\", \"PATCH\", \"DELETE\"],\n    )\n\n    # a permission matching read access to all routes, with no constraints\n    # on path or query parameters\n    all_read = RoutePermission(\n        paths=[route.path_format for route in all_routes], methods=[\"GET\"]\n    )\n\n    # read only policy allows read requests on all routes and denies write requests\n    # falling back to denying a request if it matches none of the permissions\n    read_only_policy = Policy(allow=[all_read], deny=[all_write], default_deny=True)\n\n    # a more permissive policy granting write and read access on all routes, falling back\n    # to approving a request if it matches none of the permissions\n    authorized_policy = Policy(allow=[all_write, all_read], default_deny=False)\n\n    if not user:\n        # anonymous requests get read only permissions\n        return read_only_policy\n    else:\n        # authenticated requests get full permissions\n        return authorized_policy\n\n\n# build the authorization dependency\nauthorization = build_authorization_dependency(\n    policy_generator=policy_generator,\n)\n\n\napp = FastAPI(dependencies=[Depends(authorization)])\n\n\n@app.get(\"/test\")\ndef get_test(request: Request):\n    return {\"status\": \"ok\"}\n\n\n@app.post(\"/test\")\ndef post_test(request: Request):\n    print(\"Should not be able to reach this endpoint with read-only policy\")\n    return {\"status\": \"ok\"}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevelopmentseed%2Ffastapi-authorization-gateway","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevelopmentseed%2Ffastapi-authorization-gateway","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevelopmentseed%2Ffastapi-authorization-gateway/lists"}