
在现代web应用开发中,集成第三方身份认证服务(如azure ad)是常见的需求。fastapi作为一个高性能的python web框架,结合authlib库可以方便地实现oauth2认证流程。然而,在实际操作中,开发者可能会遇到一些配置陷阱,导致认证失败。本文旨在提供一个全面的教程,指导开发者如何正确配置fastapi与authlib,以实现azure ad的oauth2认证,并解决常见的typeerror和keyerror问题。
成功集成Azure AD OAuth2认证的第一步是正确配置Authlib的OAuth客户端。这包括设置必要的环境变量和注册Azure AD作为OAuth提供商。
确保您的FastAPI项目已安装必要的依赖,特别是fastapi、uvicorn、authlib和python-dotenv(用于加载环境变量)。
pip install fastapi uvicorn authlib python-dotenv httpx starlette
同时,您需要在Azure AD中注册一个应用程序,并获取以下关键信息:
这些信息应作为环境变量加载,以提高安全性和灵活性。
# .env 文件示例 ASPEN_APP_AUTH_CLIENT_ID="your_client_id" ASPEN_APP_AUTH_TENANT_ID="your_tenant_id" ASPEN_APP_AUTH_SECRET="your_client_secret"
# auth_config.py (或您的认证配置文件)
import os
from authlib.integrations.starlette_client import OAuth
from fastapi import Depends, HTTPException, status
from fastapi.security import OAuth2AuthorizationCodeBearer
from starlette.requests import Request
from dotenv import load_dotenv
load_dotenv() # 加载 .env 文件中的环境变量
CLIENT_ID = os.getenv("ASPEN_APP_AUTH_CLIENT_ID")
TENANT_ID = os.getenv("ASPEN_APP_AUTH_TENANT_ID")
CLIENT_SECRET = os.getenv("ASPEN_APP_AUTH_SECRET")
# Azure AD 认证端点
AZURE_AUTHORIZE_URL = f'https://login.microsoftonline.com/{TENANT_ID}/oauth2/v2.0/authorize'
AZURE_TOKEN_URL = f'https://login.microsoftonline.com/{TENANT_ID}/oauth2/v2.0/token'
JWKS_URI = f"https://login.microsoftonline.com/{TENANT_ID}/discovery/v2.0/keys"
oauth = OAuth()
# OAuth2 Authorization Code Flow scheme for dependency injection
oauth2_scheme = OAuth2AuthorizationCodeBearer(
authorizationUrl=AZURE_AUTHORIZE_URL,
tokenUrl=AZURE_TOKEN_URL,
scheme_name="AzureADOAuth2"
)使用Authlib的oauth.register方法注册Azure AD作为OAuth服务提供商。这是配置的核心部分,也是解决TypeError问题的关键。
最初的错误TypeError: Invalid type for url. Expected str or httpx.URL, got <class 'NoneType'>: None通常发生在Authlib尝试获取访问令牌时,因为内部用于获取令牌的URL被错误地解析为None。这通常是由于oauth.register中token_url参数的命名不符合Authlib与特定OAuth提供商(如Azure AD)的内部期望所致。
Authlib在与某些OAuth提供商交互时,可能期望使用特定的参数名来指定令牌端点。尽管token_url是Authlib register方法的通用参数,但在某些情况下,尤其是在与Azure AD这种复杂的身份提供商集成时,可能需要更具体的参数名。当token_endpoint在内部解析为None时,httpx客户端尝试构建请求URL时会收到一个None值,从而抛出TypeError。
根据实践,解决此问题需要将令牌端点明确指定为access_token_url。此外,为了后续正确解析和验证ID Token,还需要提供jwks_uri(JSON Web Key Set URI)。jwks_uri指向一个包含公钥的端点,Authlib会用这些公钥来验证从Azure AD获得的ID Token的签名。
# auth_config.py (OAuth 注册的修正部分)
oauth.register(
name='azure',
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
authorize_url=AZURE_AUTHORIZE_URL,
# 关键修正:使用 access_token_url 替代 token_url 或 token_endpoint
access_token_url=AZURE_TOKEN_URL,
# 必须添加 jwks_uri 以正确解析 ID Token
jwks_uri=JWKS_URI,
client_kwargs={'scope': 'openid email profile'}
)注意:在某些Authlib版本或特定配置下,可能token_endpoint或token_url也能工作,但access_token_url被证实能有效解决TypeError问题,并且与jwks_uri一同使用时能确保ID Token的正确处理。
在解决了TypeError之后,您可能会遇到KeyError: 'id_token',这表明在尝试从令牌响应中获取id_token时失败。即使id_token存在,也可能因为缺少必要的nonce参数而无法正确解析。
id_token是一个JWT(JSON Web Token),它包含了用户的身份信息。为了验证其真实性和完整性,Authlib需要使用Azure AD提供的公钥。jwks_uri就是这些公钥的发布地址。在oauth.register中配置jwks_uri后,Authlib才能自动下载并缓存这些公钥,用于后续的ID Token验证。
当从Azure AD成功获取访问令牌后,令牌响应中通常会包含id_token。为了安全地解析和验证这个ID Token,authlib的parse_id_token方法是必需的。此外,如果您的认证流程涉及到防止重放攻击,Azure AD会在授权请求中包含一个nonce参数,这个nonce也需要在ID Token解析时提供。
# auth_config.py (get_current_user 依赖函数)
async def get_current_user(request: Request, token_str: str = Depends(oauth2_scheme)):
try:
# Authlib的parse_id_token方法通常需要原始的token字典,而不是字符串
# 这里的oauth2_scheme返回的是字符串,因此需要重新获取完整token或调整逻辑
# 更常见的做法是在 /auth 回调中直接解析 ID Token
# 暂时保持原样,但要注意这里可能需要调整以匹配实际的token获取方式
# For simplicity, assuming token_str here is directly the ID Token string for demonstration
# In a real scenario, you'd get the full token dict from a session or similar
# This part needs careful handling. The Depends(oauth2_scheme) typically gets the access token string.
# To parse ID token, you usually need the full token response dictionary from authorize_access_token.
# Let's assume for this dependency, we're validating an already parsed ID token or have access to the full token.
# For a more robust solution, the ID token parsing should happen in the /auth endpoint.
# If the token_str is indeed an ID token string, you might parse it directly:
# user_info = await oauth.azure.parse_id_token(token=token_str)
# However, the original problem was in the /auth endpoint, so let's focus there.
# This dependency might be for validating subsequent requests with an access token.
# For the context of ID token parsing, the relevant part is in the /auth endpoint.
pass
except Exception as e:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail=f"Invalid authentication credentials: {str(e)}"
)将上述修正应用于FastAPI应用中,构建完整的登录和认证回调流程。
# main.py
from fastapi import FastAPI, Request, HTTPException, status, Depends
from fastapi.responses import JSONResponse
from starlette.middleware.sessions import SessionMiddleware
from auth_config import oauth, get_current_user, CLIENT_ID, TENANT_ID # 导入必要的配置
app = FastAPI()
# 必须添加 SessionMiddleware 来存储 OAuth 状态
app.add_middleware(SessionMiddleware, secret_key="your_super_secret_key_for_session") # 请替换为强随机密钥
@app.get("/")
async def health():
return JSONResponse(content={"status": "healthy"}, status_code=200)
# 登录重定向到 Azure AD
@app.get("/login")
async def login(request: Request):
redirect_uri = request.url_for('auth')
return await oauth.azure.authorize_redirect(request, redirect_uri)
# 受保护的路由示例
@app.get("/protected")
async def protected_route(user: dict = Depends(get_current_user)):
return {"message": "This is a protected route", "user": user}这是获取并解析ID Token的核心逻辑。
# main.py (认证回调端点)
@app.get("/auth")
async def auth(request: Request):
try:
# 1. 获取访问令牌 (会话中包含 state 和 code)
token = await oauth.azure.authorize_access_token(request)
# 2. 从令牌响应中获取 nonce(如果存在且需要)
# Authlib的authorize_access_token通常会处理nonce,
# 但如果id_token解析失败,可能需要手动提取并传递
# 注意:Authlib的parse_id_token方法通常会从token字典中自动查找nonce。
# 如果您的Azure AD配置要求显式传递,则需要从请求的会话中获取
# 例如:nonce = request.session.get('nonce')
# 3. 解析 ID Token
# token=token 传递的是完整的令牌响应字典
user_info = await oauth.azure.parse_id_token(token=token)
# 认证成功,返回用户信息
return {"user_info": user_info}
except HTTPException as e:
# Authlib内部可能抛出 HTTPException,直接传递
raise e
except Exception as e:
# 捕获其他异常,提供通用错误信息
print(f"Error during authentication: {str(e)}")
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Authentication failed: {str(e)}")
# auth_config.py (更新 get_current_user,使其能从session或token中获取userinfo)
async def get_current_user(request: Request):
# This dependency assumes the user info is stored in the session after successful login
# Or, it could validate an access token for API calls.
# For simplicity, let's assume the user info is retrieved from the session after /auth.
user_info = request.session.get("user_info") # Assuming you store user_info in session after /auth
if not user_info:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Not authenticated"
)
return user_info
# In /auth endpoint, after successful parsing:
# request.session["user_info"] = user_info
# return {"user_info": user_info}完整的main.py示例:
# main.py
from fastapi import FastAPI, Request, HTTPException, status, Depends
from fastapi.responses import JSONResponse
from starlette.middleware.sessions import SessionMiddleware
from authlib.integrations.starlette_client import OAuth
import os
from dotenv import load_dotenv
load_dotenv()
# Load environment variables
CLIENT_ID = os.getenv("ASPEN_APP_AUTH_CLIENT_ID")
TENANT_ID = os.getenv("ASPEN_APP_AUTH_TENANT_ID")
CLIENT_SECRET = os.getenv("ASPEN_APP_AUTH_SECRET")
# Initialize OAuth2
oauth = OAuth()
# Azure AD 认证端点
AZURE_AUTHORIZE_URL = f'https://login.microsoftonline.com/{TENANT_ID}/oauth2/v2.0/authorize'
AZURE_TOKEN_URL = f'https://login.microsoftonline.com/{TENANT_ID}/oauth2/v2.0/token'
JWKS_URI = f"https://login.microsoftonline.com/{TENANT_ID}/discovery/v2.0/keys"
oauth.register(
name='azure',
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
authorize_url=AZURE_AUTHORIZE_URL,
access_token_url=AZURE_TOKEN_URL, # 解决 TypeError 的关键
jwks_uri=JWKS_URI, # 解决 KeyError: 'id_token' 的关键
client_kwargs={'scope': 'openid email profile'}
)
app = FastAPI()
# 必须添加 SessionMiddleware 来存储 OAuth 状态
app.add_middleware(SessionMiddleware, secret_key="q803pJMcx6KNkIlBGi_mPQSYiOP0IPze") # 请替换为强随机密钥
@app.get("/")
async def health():
return JSONResponse(content={"status": "healthy"}, status_code=200)
# 登录重定向到 Azure AD
@app.get("/login")
async def login(request: Request):
redirect_uri = request.url_for('auth')
return await oauth.azure.authorize_redirect(request, redirect_uri)
# 认证回调端点
@app.get("/auth")
async def auth(request: Request):
try:
# 1. 获取访问令牌 (会话中包含 state 和 code)
token = await oauth.azure.authorize_access_token(request)
# 2. 解析 ID Token
# Authlib的parse_id_token方法会从token字典中查找id_token并验证
user_info = await oauth.azure.parse_id_token(token=token)
# 认证成功,将用户信息存储到 session
request.session["user_info"] = user_info
return {"message": "Authentication successful", "user_info": user_info}
except HTTPException as e:
raise e
except Exception as e:
print(f"Error during authentication: {str(e)}")
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Authentication failed: {str(e)}")
# 获取当前用户信息的依赖函数
async def get_current_user(request: Request):
user_info = request.session.get("user_info")
if not user_info:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Not authenticated. Please log in."
)
return user_info
# 受保护的路由示例
@app.get("/protected")
async def protected_route(user: dict = Depends(get_current_user)):
return {"message": "This is a protected route", "current_user": user}
以上就是FastAPI集成Azure AD OAuth2认证配置指南的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号