{"id":44419075,"url":"https://github.com/fastapi-practices/api_key","last_synced_at":"2026-04-08T08:03:01.724Z","repository":{"id":334727427,"uuid":"1134355625","full_name":"fastapi-practices/api_key","owner":"fastapi-practices","description":"FastAPI Best Architecture API Key 插件","archived":false,"fork":false,"pushed_at":"2026-01-26T14:07:42.000Z","size":11,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2026-02-12T16:40:34.080Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fastapi-practices.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-01-14T15:54:47.000Z","updated_at":"2026-01-26T14:08:06.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/fastapi-practices/api_key","commit_stats":null,"previous_names":["fastapi-practices/api_key"],"tags_count":0,"template":false,"template_full_name":"fastapi-practices/fba_plugin_template","purl":"pkg:github/fastapi-practices/api_key","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastapi-practices%2Fapi_key","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastapi-practices%2Fapi_key/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastapi-practices%2Fapi_key/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastapi-practices%2Fapi_key/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fastapi-practices","download_url":"https://codeload.github.com/fastapi-practices/api_key/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastapi-practices%2Fapi_key/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31545909,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-07T16:28:08.000Z","status":"online","status_checked_at":"2026-04-08T02:00:06.127Z","response_time":54,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":"2026-02-12T09:08:45.178Z","updated_at":"2026-04-08T08:03:01.719Z","avatar_url":"https://github.com/fastapi-practices.png","language":"Python","readme":"# API Key\n\n用户自定义 API Key 管理，支持生成、管理和使用 API Key 进行接口认证\n\n## 全局配置\n\n在 `backend/core/conf.py` 中添加以下内容：\n\n```python\n##################################################\n# [ Plugin ] api_key\n##################################################\n# 基础配置（in plugin.toml）\nAPI_KEY_GENERATE_PREFIX: str\n```\n\n## 使用方式\n\n编辑 `backend/core/registrar.py`，替换 JWT 认证中间件：\n\n```python\n# 原来的导入\n# from backend.middleware.jwt_auth_middleware import JwtAuthMiddleware\n\n# 替换为\nfrom backend.plugin.api_key.middleware import JwtApiKeyAuthMiddleware\n\n# 原来的 JWT auth\n# app.add_middleware(\n#     AuthenticationMiddleware,\n#     backend=JwtAuthMiddleware(),\n#     on_error=JwtAuthMiddleware.auth_exception_handler,\n# )\n\n# 替换为\napp.add_middleware(\n    AuthenticationMiddleware,\n    backend=JwtApiKeyAuthMiddleware(),\n    on_error=JwtApiKeyAuthMiddleware.auth_exception_handler,\n)\n```\n\n## 认证流程\n\n```mermaid\nflowchart TD\n    A[Authorization: Bearer token] --\u003e B[token.startswith 'fba-' ?]\n    B --\u003e|Yes| C[API Key 认证]\n    B --\u003e|No| D[JWT Token 认证]\n    C --\u003e I[RBAC 权限校验]\n    D --\u003e I\n```\n\n## 权限控制\n\nAPI Key 完全继承用户权限\n\n如需限制权限，只需创建专门的 API 用户：\n\n1. 创建受限角色（如 `API 只读角色`），分配必要权限\n2. 创建 API 用户，分配该角色\n3. 使用该用户创建 API Key\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffastapi-practices%2Fapi_key","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffastapi-practices%2Fapi_key","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffastapi-practices%2Fapi_key/lists"}