MIT License

项目介绍

vibber-ai/fastapi-azure-auth 是一个托管在 GitHub 上的开源项目,主要使用 Python 编写,采用 MIT License 许可证,目前获得 681 个 Star、79 次 Fork。

仓库全名
vibber-ai/fastapi-azure-auth
开源许可证
MIT License
Star 数
681
Fork 数
79
未关闭 Issue
2

README 摘要

FastAPI-Azure-Auth Azure Entra ID Authentication for FastAPI apps made easy. 🚀 Description FastAPI is a modern, fast (high-performance), web framework for building APIs with Python, based on standard Python type hints. This package enables our developers (and you 😊) to create features without worrying about authentication and authorization. Made by the people behind Vibber. Vibber is the control plane for…

完整内容见 GitHub 原始 README 或本页 。

常见问题

fastapi-azure-auth 用什么语言开发?

主要使用 Python 编写,采用 MIT License 许可证,目前有 681 个 Star。

fastapi-azure-auth 可以免费商用吗?

仓库采用 MIT License 许可证,可免费使用与修改,商业使用请遵守许可证条款(例如保留版权声明与许可声明)。

如何获取 fastapi-azure-auth 的源码?

可以直接克隆仓库:git clone https://github.com/vibber-ai/fastapi-azure-auth.git,默认分支为 main;安装与运行步骤见仓库 README。

fastapi-azure-auth 最近还在维护吗?

仓库最近一次更新为 7月2日,当前有 2 个未关闭 Issue;Star 数为 681,可作为社区活跃度的参考。

关于

Easy and secure implementation of Azure Entra ID (previously AD) for your FastAPI APIs 🔒 B2C, single- and multi-tenant support.

描述取自 GitHub 官方 API,中文内容由 GitRead 中文社区 编辑团队整理。

README


FastAPI-Azure-Auth

Azure Entra ID Authentication for FastAPI apps made easy.

Python version FastAPI Version Package version
Codecov Pre-commit Ruff mypy

🚀 Description

FastAPI is a modern, fast (high-performance), web framework for building APIs with Python, based on standard Python type hints.

This package enables our developers (and you 😊) to create features without worrying about authentication and authorization.

Made by the people behind Vibber. Vibber is the control plane for enterprise AI. The single place to build, run and manage MCP servers. Production-ready in minutes, not months. Contact: [email protected]

📚 Resources

The documentation contains a full tutorial on how to configure Azure Entra ID and FastAPI for single- and multi-tenant applications as well as B2C apps. It includes examples on how to lock down your APIs to certain scopes, tenants, roles etc. For first time users it’s strongly advised to set up your application exactly how it’s described there, and then alter it to your needs later.

MIT License | Documentation | GitHub

⚡ Setup

This is a tl;dr intended to give you an idea of what this package does and how to use it. For a more in-depth tutorial and settings reference you should read the documentation.

1. Install this library:
uv add fastapi-azure-auth
2. Configure your FastAPI app

Include swagger_ui_oauth2_redirect_url and swagger_ui_init_oauth in your FastAPI app initialization:

# file: main.py
app = FastAPI(
    ...
    swagger_ui_oauth2_redirect_url='/oauth2-redirect',
    swagger_ui_init_oauth={
        'usePkceWithAuthorizationCodeGrant': True,
        'clientId': settings.OPENAPI_CLIENT_ID,
    },
)
3. Setup CORS

Ensure you have CORS enabled for your local environment, such as http://localhost:8000.

4. Configure FastAPI-Azure-Auth

Configure either SingleTenantAzureAuthorizationCodeBearer, MultiTenantAzureAuthorizationCodeBearer or B2CMultiTenantAuthorizationCodeBearer

# file: demoproj/api/dependencies.py
from fastapi_azure_auth.auth import SingleTenantAzureAuthorizationCodeBearer

azure_scheme = SingleTenantAzureAuthorizationCodeBearer(
    app_client_id=settings.APP_CLIENT_ID,
    tenant_id=settings.TENANT_ID,
    scopes={
        f'api://{settings.APP_CLIENT_ID}/user_impersonation': 'user_impersonation',
    }
)

or for multi-tenant applications:

# file: demoproj/api/dependencies.py
from fastapi_azure_auth.auth import MultiTenantAzureAuthorizationCodeBearer

azure_scheme = MultiTenantAzureAuthorizationCodeBearer(
    app_client_id=settings.APP_CLIENT_ID,
    scopes={
        f'api://{settings.APP_CLIENT_ID}/user_impersonation': 'user_impersonation',
    },
    validate_iss=False
)

To validate the iss, configure an iss_callable.

5. Configure dependencies

Add azure_scheme as a dependency for your views/routers, using either Security() or Depends().

# file: main.py
from demoproj.api.dependencies import azure_scheme

app.include_router(api_router, prefix=settings.API_V1_STR, dependencies=[Security(azure_scheme, scopes=['user_impersonation'])])
6. Load config on startup

Optional but recommended.

# file: main.py
@app.on_event('startup')
async def load_config() -> None:
    """
    Load OpenID config on startup.
    """
    await azure_scheme.openid_config.load_config()

📄 Example OpenAPI documentation

Your OpenAPI documentation will get an Authorize button, which can be used to authenticate. authorize

The user can select which scopes to authenticate with, based on your configuration. scopes

评论

暂无评论