keymanager_hjy
一句话宣言: 零配置、专业级API安全管理器,为现代Python API提供优雅的认证、限流和审计日志。
🚀 快速开始
安装
# 基础安装
pip install keymanager_hjy
# 安装FastAPI集成
pip install keymanager_hjy[fastapi]
# 安装Flask集成
pip install keymanager_hjy[flask]
# 安装CLI工具
pip install keymanager_hjy[cli]
# 安装所有功能
pip install keymanager_hjy[all]
基本使用
from keymanager_hjy import KeyManager
# 创建密钥管理器
key_manager = KeyManager()
# 生成API密钥
api_key = key_manager.generate_api_key("user123")
# 验证API密钥
is_valid = key_manager.validate_api_key(api_key)
print(f"API密钥: {api_key}")
print(f"验证结果: {is_valid}")
✨ 核心功能
🔐 认证管理
- API密钥生成: 安全的API密钥生成和管理
- 用户认证: 完整的用户认证流程
- 权限验证: 细粒度的权限控制
- 会话管理: 安全的会话管理
🛡️ 安全防护
- 速率限制: 智能的API速率限制
- IP白名单: IP地址白名单管理
- 请求签名: 请求签名验证
- 加密存储: 敏感数据加密存储
📊 审计日志
- 操作记录: 完整的操作审计日志
- 访问追踪: 用户访问行为追踪
- 异常监控: 安全异常监控和告警
- 报告生成: 安全报告自动生成
🔧 集成示例
FastAPI集成
from fastapi import FastAPI, Depends
from keymanager_hjy.integrations import fastapi_guard
app = FastAPI()
# 使用装饰器保护路由
@app.get("/protected")
@fastapi_guard.require_auth
async def protected_endpoint():
return {"message": "This is a protected endpoint"}
# 使用依赖注入
@app.get("/secure")
async def secure_endpoint(auth=Depends(fastapi_guard.auth_dependency)):
return {"message": "Secure endpoint", "user": auth.user_id}
Flask集成
from flask import Flask
from keymanager_hjy.integrations import flask_guard
app = Flask(__name__)
# 使用装饰器保护路由
@app.route("/protected")
@flask_guard.require_auth
def protected_endpoint():
return {"message": "This is a protected endpoint"}
# 使用中间件
app.before_request(flask_guard.auth_middleware)
🎯 高级功能
自定义认证策略
from keymanager_hjy import KeyManager, AuthStrategy
class CustomAuthStrategy(AuthStrategy):
def authenticate(self, request):
# 自定义认证逻辑
custom_token = request.headers.get("X-Custom-Token")
if custom_token == "valid_token":
return {"user_id": "custom_user", "permissions": ["read", "write"]}
return None
key_manager = KeyManager(auth_strategy=CustomAuthStrategy())
速率限制配置
from keymanager_hjy import RateLimiter
# 配置速率限制
rate_limiter = RateLimiter(
requests_per_minute=60,
requests_per_hour=1000,
burst_size=10
)
# 检查速率限制
if rate_limiter.is_allowed("user123"):
# 处理请求
pass
else:
# 返回429错误
pass
📚 文档
🧪 测试
# 运行所有测试
pytest
# 运行集成测试
pytest -m integration
# 运行性能测试
pytest -m performance
# 生成覆盖率报告
pytest --cov=keymanager_hjy --cov-report=html
🤝 贡献
我们欢迎所有形式的贡献!
- Fork 项目
- 创建功能分支 (
git checkout -b feature/amazing-feature) - 提交更改 (
git commit -m 'Add amazing feature') - 推送到分支 (
git push origin feature/amazing-feature) - 创建 Pull Request
📄 许可证
本项目采用 MIT 许可证 - 查看 LICENSE 文件了解详情
🔗 相关项目
- ai_runner_hjy: AI服务运行器
- taskmanager_hjy: 任务管理器
- datamanager_hjy: 数据管理器
- configmanager_hjy: 配置管理器
keymanager_hjy - 让API安全变得简单而强大 🛡️
Metadata
Release files for keymanager-hjy 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| keymanager_hjy-0.3.0.tar.gz | 29.4 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| keymanager_hjy-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 63.7 kB
Release files / keymanager_hjy-0.3.0.tar.gz
| Download URL | keymanager_hjy-0.3.0.tar.gz |
|---|---|
| Size | 29.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6d04acf4c75f98fbc2f65e258c2d773a77d5ab66591b0d52c8b246c31899e5fd
|
|
BLAKE2b-256 checksum How to use checksums |
974bae118363ed980c05dec7188fe1b41ee38ff20fcf13702418201e6ffebd50
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.3
|
Release files / keymanager_hjy-0.3.0-py3-none-any.whl
| Download URL | keymanager_hjy-0.3.0-py3-none-any.whl |
|---|---|
| Size | 34.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d8ca163fc7d35a2efe4679e2c6feb8041395a91c169096206109f7a2b6d135fd
|
|
BLAKE2b-256 checksum How to use checksums |
356f72bf31f3b69faf67a5a676d7ce3af08135c82100be397f67dfc05f4f66fa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.3
|
Security for keymanager-hjy 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.