河南农业大学统一授权认证服务 python SDK
Project description
HenauAuth FastAPI 脚手架
概述
本项目包含一个 FastAPI 项目脚手架,它集成了河南农业大学 OAuth 2.0 服务,使开发者能够轻松地在 FastAPI 应用程序中实现用户身份验证。
使用
ha fastapi init --name myapp
HenauAuth FastAPI 集成示例
本项目将 河南农业大学 OAuth 2.0 服务 与 FastAPI 应用程序集成,以便于开发者使用
概述
HenauAuth 库简化了在 FastAPI 应用程序中针对河南农业大学 OAuth 2.0 服务器验证用户的过程。 此示例展示了该库的基本设置和用法。
准备工作
- Python 3.10+
- FastAPI
henau_auth库 (通过 pip 安装:pip install henau_auth)
配置
-
从河南农业大学 OAuth 2.0 服务器管理面板获取
app_id和app_secret。 -
在您的 FastAPI 应用程序中配置
HenauAuth客户端:from fastapi import FastAPI, Request from henau_auth import HenauAuth app = FastAPI() client = HenauAuth( app_id="YOUR_APP_ID", # 替换为您的应用程序 ID app_secret="YOUR_APP_SECRET", # 替换为您的应用程序密钥 base_url="https://oauth.henau.edu.cn" # 授权服务器地址 (默认为 https://oauth.henau.edu.cn) ) app.add_middleware( client.get_fastapi_middleware(), login_router="/login", # 登录接口的路由,默认为 /login excluded_routes=["/test"], # 排除的路由,被排除的路由不会进行鉴权,默认为 [] redirect_url="https://www.baidu.com", jwt_secret="dasdsafdsfsregtrjukiuok", # jwt的密钥,不传值则每次动态生成一个 建议传值32位字符串即可 expires_delta=3600, # jwt过期时间,默认为3600分钟,单位分钟 enable_permission_controller=True, # 是否启用权限控制,Defaults to True 启用后 request中会携带一个casbin 权限控制对象 # get_user_func = lambda payload: User.get_or_none(User.open_id == payload["henau_openid"]) # 如果传入了get_user_func,则会在获取到用户信息后调用该函数,该函数可以返回一个用户对象,该用户对象会被存储在request.state.user中 ) @app.get("/login") async def login(request: Request, code: str): e : Enforcer = request.state.e return {"user": request.state.user, "token": request.state.token} @app.get("/other") async def user(request: Request): e : Enforcer = request.state.e return {"user": request.state.user} @app.get("/test") async def test(request: Request, code: str = None): e : Enforcer = request.state.e return {"code": code, "user": str(request.state) }
重要提示: 将
"YOUR_APP_ID"和"YOUR_APP_SECRET"替换为您实际的应用程序凭据。 强烈建议设置一个强大的 32 字符jwt_secret以确保安全。
用法
-
运行 FastAPI 应用程序:
uvicorn app:app --reload
-
访问
/login端点以启动 OAuth 2.0 流程。 您需要使用您的app_id构建授权 URL,并将用户重定向到河南农业大学 OAuth 2.0 服务器。 成功验证后,服务器会将用户重定向回您的/login端点,并提供授权code。 -
HenauAuth中间件将自动处理授权码的交换以获取访问令牌,并检索用户的信息。 用户对象将在request.state.user中可用,JWT 令牌将在request.state.token中可用。 -
受保护的路由 (即,不在
excluded_routes中的路由) 将需要在Authorization标头中提供有效的 JWT 令牌。
接口
/login: 登录接口。 处理来自河南农业大学 OAuth 2.0 服务器的 OAuth 2.0 回调。/other: 示例受保护的接口。 需要有效的 JWT 令牌。/test: 示例不受保护的接口。 无需身份验证即可访问。
自定义用户检索 (get_user_func)
app.add_middleware 中的 get_user_func 参数允许您将经过身份验证的用户信息与应用程序的用户模型集成。 该函数接收来自访问令牌的有效负载,并应根据有效负载中的信息返回用户对象。 示例代码展示了如何使用有效负载中的 open_id 从数据库中检索用户。
注意
- 这是一个基本示例,可能需要根据您的特定应用程序要求进行调整。
- 确保在您的生产环境中实施适当的错误处理和安全措施。
- 有关更高级的配置选项和功能,请参阅
henau_auth库文档。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file henau_auth-0.1.6.tar.gz.
File metadata
- Download URL: henau_auth-0.1.6.tar.gz
- Upload date:
- Size: 16.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
33364868036aec31c13d73c4478d9a3aec966d075fcd42f1d92e7d90b9407887
|
|
| MD5 |
c73c37ce623b9229b0793ff749de14d9
|
|
| BLAKE2b-256 |
714cdedf2e3edccecebad7def4bb78edb36fd7b5c16f15f9d8ea7997449645b6
|
File details
Details for the file henau_auth-0.1.6-py3-none-any.whl.
File metadata
- Download URL: henau_auth-0.1.6-py3-none-any.whl
- Upload date:
- Size: 19.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6f0d37ee385c9295542235856394a451815bdea688dac350156ed0cfce7bb21e
|
|
| MD5 |
aab6125bcba864747ed497dc722b3794
|
|
| BLAKE2b-256 |
c1229b915c8a8487bafc2a5dda5d42320e335d221746b5406c9162ab90379261
|