keymanager-hjy


Namekeymanager-hjy JSON
Version 0.3.0 PyPI version JSON
download
home_pageNone
SummaryZero-config, professional-grade API security manager. Elegant authentication, rate limiting, and audit logging for modern Python APIs.
upload_time2025-08-18 01:32:05
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseMIT
keywords security authentication api rate-limiting audit-logging
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # keymanager_hjy

[![PyPI version](https://img.shields.io/pypi/v/keymanager_hjy.svg?style=flat-square&color=1E3A8A)](https://pypi.org/project/keymanager_hjy/)
[![Python versions](https://img.shields.io/pypi/pyversions/keymanager_hjy.svg?style=flat-square&color=1E3A8A)](https://pypi.org/project/keymanager_hjy/)
[![License](https://img.shields.io/pypi/l/keymanager_hjy.svg?style=flat-square&color=059669)](https://github.com/hjy/keymanager_hjy/blob/main/LICENSE)
[![Downloads](https://img.shields.io/pypi/dm/keymanager_hjy.svg?style=flat-square&color=059669)](https://pypi.org/project/keymanager_hjy/)

> **一句话宣言**: 零配置、专业级API安全管理器,为现代Python API提供优雅的认证、限流和审计日志。

## 🚀 快速开始

### 安装

```bash
# 基础安装
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]
```

### 基本使用

```python
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集成

```python
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集成

```python
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)
```

## 🎯 高级功能

### 自定义认证策略

```python
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())
```

### 速率限制配置

```python
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
```

## 📚 文档

- **[API文档](https://keymanager-hjy.readthedocs.io/)**: 完整的API参考
- **[集成指南](docs/integrations.md)**: 详细的集成说明
- **[安全最佳实践](docs/security.md)**: 安全配置建议
- **[部署指南](docs/deployment.md)**: 生产环境部署

## 🧪 测试

```bash
# 运行所有测试
pytest

# 运行集成测试
pytest -m integration

# 运行性能测试
pytest -m performance

# 生成覆盖率报告
pytest --cov=keymanager_hjy --cov-report=html
```

## 🤝 贡献

我们欢迎所有形式的贡献!

1. Fork 项目
2. 创建功能分支 (`git checkout -b feature/amazing-feature`)
3. 提交更改 (`git commit -m 'Add amazing feature'`)
4. 推送到分支 (`git push origin feature/amazing-feature`)
5. 创建 Pull Request

## 📄 许可证

本项目采用 MIT 许可证 - 查看 [LICENSE](LICENSE) 文件了解详情

## 🔗 相关项目

- [ai_runner_hjy](https://pypi.org/project/ai-runner-hjy/): AI服务运行器
- [taskmanager_hjy](https://pypi.org/project/taskmanager-hjy/): 任务管理器
- [datamanager_hjy](https://pypi.org/project/datamanager-hjy/): 数据管理器
- [configmanager_hjy](https://pypi.org/project/configmanager-hjy/): 配置管理器

---

**keymanager_hjy** - 让API安全变得简单而强大 🛡️

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "keymanager-hjy",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "hjy <hjy@example.com>",
    "keywords": "security, authentication, api, rate-limiting, audit-logging",
    "author": null,
    "author_email": "hjy <hjy@example.com>",
    "download_url": "https://files.pythonhosted.org/packages/97/4b/ae118363ed980c05dec7188fe1b41ee38ff20fcf13702418201e6ffebd50/keymanager_hjy-0.3.0.tar.gz",
    "platform": null,
    "description": "# keymanager_hjy\n\n[![PyPI version](https://img.shields.io/pypi/v/keymanager_hjy.svg?style=flat-square&color=1E3A8A)](https://pypi.org/project/keymanager_hjy/)\n[![Python versions](https://img.shields.io/pypi/pyversions/keymanager_hjy.svg?style=flat-square&color=1E3A8A)](https://pypi.org/project/keymanager_hjy/)\n[![License](https://img.shields.io/pypi/l/keymanager_hjy.svg?style=flat-square&color=059669)](https://github.com/hjy/keymanager_hjy/blob/main/LICENSE)\n[![Downloads](https://img.shields.io/pypi/dm/keymanager_hjy.svg?style=flat-square&color=059669)](https://pypi.org/project/keymanager_hjy/)\n\n> **\u4e00\u53e5\u8bdd\u5ba3\u8a00**: \u96f6\u914d\u7f6e\u3001\u4e13\u4e1a\u7ea7API\u5b89\u5168\u7ba1\u7406\u5668\uff0c\u4e3a\u73b0\u4ee3Python API\u63d0\u4f9b\u4f18\u96c5\u7684\u8ba4\u8bc1\u3001\u9650\u6d41\u548c\u5ba1\u8ba1\u65e5\u5fd7\u3002\n\n## \ud83d\ude80 \u5feb\u901f\u5f00\u59cb\n\n### \u5b89\u88c5\n\n```bash\n# \u57fa\u7840\u5b89\u88c5\npip install keymanager_hjy\n\n# \u5b89\u88c5FastAPI\u96c6\u6210\npip install keymanager_hjy[fastapi]\n\n# \u5b89\u88c5Flask\u96c6\u6210\npip install keymanager_hjy[flask]\n\n# \u5b89\u88c5CLI\u5de5\u5177\npip install keymanager_hjy[cli]\n\n# \u5b89\u88c5\u6240\u6709\u529f\u80fd\npip install keymanager_hjy[all]\n```\n\n### \u57fa\u672c\u4f7f\u7528\n\n```python\nfrom keymanager_hjy import KeyManager\n\n# \u521b\u5efa\u5bc6\u94a5\u7ba1\u7406\u5668\nkey_manager = KeyManager()\n\n# \u751f\u6210API\u5bc6\u94a5\napi_key = key_manager.generate_api_key(\"user123\")\n\n# \u9a8c\u8bc1API\u5bc6\u94a5\nis_valid = key_manager.validate_api_key(api_key)\n\nprint(f\"API\u5bc6\u94a5: {api_key}\")\nprint(f\"\u9a8c\u8bc1\u7ed3\u679c: {is_valid}\")\n```\n\n## \u2728 \u6838\u5fc3\u529f\u80fd\n\n### \ud83d\udd10 \u8ba4\u8bc1\u7ba1\u7406\n- **API\u5bc6\u94a5\u751f\u6210**: \u5b89\u5168\u7684API\u5bc6\u94a5\u751f\u6210\u548c\u7ba1\u7406\n- **\u7528\u6237\u8ba4\u8bc1**: \u5b8c\u6574\u7684\u7528\u6237\u8ba4\u8bc1\u6d41\u7a0b\n- **\u6743\u9650\u9a8c\u8bc1**: \u7ec6\u7c92\u5ea6\u7684\u6743\u9650\u63a7\u5236\n- **\u4f1a\u8bdd\u7ba1\u7406**: \u5b89\u5168\u7684\u4f1a\u8bdd\u7ba1\u7406\n\n### \ud83d\udee1\ufe0f \u5b89\u5168\u9632\u62a4\n- **\u901f\u7387\u9650\u5236**: \u667a\u80fd\u7684API\u901f\u7387\u9650\u5236\n- **IP\u767d\u540d\u5355**: IP\u5730\u5740\u767d\u540d\u5355\u7ba1\u7406\n- **\u8bf7\u6c42\u7b7e\u540d**: \u8bf7\u6c42\u7b7e\u540d\u9a8c\u8bc1\n- **\u52a0\u5bc6\u5b58\u50a8**: \u654f\u611f\u6570\u636e\u52a0\u5bc6\u5b58\u50a8\n\n### \ud83d\udcca \u5ba1\u8ba1\u65e5\u5fd7\n- **\u64cd\u4f5c\u8bb0\u5f55**: \u5b8c\u6574\u7684\u64cd\u4f5c\u5ba1\u8ba1\u65e5\u5fd7\n- **\u8bbf\u95ee\u8ffd\u8e2a**: \u7528\u6237\u8bbf\u95ee\u884c\u4e3a\u8ffd\u8e2a\n- **\u5f02\u5e38\u76d1\u63a7**: \u5b89\u5168\u5f02\u5e38\u76d1\u63a7\u548c\u544a\u8b66\n- **\u62a5\u544a\u751f\u6210**: \u5b89\u5168\u62a5\u544a\u81ea\u52a8\u751f\u6210\n\n## \ud83d\udd27 \u96c6\u6210\u793a\u4f8b\n\n### FastAPI\u96c6\u6210\n\n```python\nfrom fastapi import FastAPI, Depends\nfrom keymanager_hjy.integrations import fastapi_guard\n\napp = FastAPI()\n\n# \u4f7f\u7528\u88c5\u9970\u5668\u4fdd\u62a4\u8def\u7531\n@app.get(\"/protected\")\n@fastapi_guard.require_auth\nasync def protected_endpoint():\n    return {\"message\": \"This is a protected endpoint\"}\n\n# \u4f7f\u7528\u4f9d\u8d56\u6ce8\u5165\n@app.get(\"/secure\")\nasync def secure_endpoint(auth=Depends(fastapi_guard.auth_dependency)):\n    return {\"message\": \"Secure endpoint\", \"user\": auth.user_id}\n```\n\n### Flask\u96c6\u6210\n\n```python\nfrom flask import Flask\nfrom keymanager_hjy.integrations import flask_guard\n\napp = Flask(__name__)\n\n# \u4f7f\u7528\u88c5\u9970\u5668\u4fdd\u62a4\u8def\u7531\n@app.route(\"/protected\")\n@flask_guard.require_auth\ndef protected_endpoint():\n    return {\"message\": \"This is a protected endpoint\"}\n\n# \u4f7f\u7528\u4e2d\u95f4\u4ef6\napp.before_request(flask_guard.auth_middleware)\n```\n\n## \ud83c\udfaf \u9ad8\u7ea7\u529f\u80fd\n\n### \u81ea\u5b9a\u4e49\u8ba4\u8bc1\u7b56\u7565\n\n```python\nfrom keymanager_hjy import KeyManager, AuthStrategy\n\nclass CustomAuthStrategy(AuthStrategy):\n    def authenticate(self, request):\n        # \u81ea\u5b9a\u4e49\u8ba4\u8bc1\u903b\u8f91\n        custom_token = request.headers.get(\"X-Custom-Token\")\n        if custom_token == \"valid_token\":\n            return {\"user_id\": \"custom_user\", \"permissions\": [\"read\", \"write\"]}\n        return None\n\nkey_manager = KeyManager(auth_strategy=CustomAuthStrategy())\n```\n\n### \u901f\u7387\u9650\u5236\u914d\u7f6e\n\n```python\nfrom keymanager_hjy import RateLimiter\n\n# \u914d\u7f6e\u901f\u7387\u9650\u5236\nrate_limiter = RateLimiter(\n    requests_per_minute=60,\n    requests_per_hour=1000,\n    burst_size=10\n)\n\n# \u68c0\u67e5\u901f\u7387\u9650\u5236\nif rate_limiter.is_allowed(\"user123\"):\n    # \u5904\u7406\u8bf7\u6c42\n    pass\nelse:\n    # \u8fd4\u56de429\u9519\u8bef\n    pass\n```\n\n## \ud83d\udcda \u6587\u6863\n\n- **[API\u6587\u6863](https://keymanager-hjy.readthedocs.io/)**: \u5b8c\u6574\u7684API\u53c2\u8003\n- **[\u96c6\u6210\u6307\u5357](docs/integrations.md)**: \u8be6\u7ec6\u7684\u96c6\u6210\u8bf4\u660e\n- **[\u5b89\u5168\u6700\u4f73\u5b9e\u8df5](docs/security.md)**: \u5b89\u5168\u914d\u7f6e\u5efa\u8bae\n- **[\u90e8\u7f72\u6307\u5357](docs/deployment.md)**: \u751f\u4ea7\u73af\u5883\u90e8\u7f72\n\n## \ud83e\uddea \u6d4b\u8bd5\n\n```bash\n# \u8fd0\u884c\u6240\u6709\u6d4b\u8bd5\npytest\n\n# \u8fd0\u884c\u96c6\u6210\u6d4b\u8bd5\npytest -m integration\n\n# \u8fd0\u884c\u6027\u80fd\u6d4b\u8bd5\npytest -m performance\n\n# \u751f\u6210\u8986\u76d6\u7387\u62a5\u544a\npytest --cov=keymanager_hjy --cov-report=html\n```\n\n## \ud83e\udd1d \u8d21\u732e\n\n\u6211\u4eec\u6b22\u8fce\u6240\u6709\u5f62\u5f0f\u7684\u8d21\u732e\uff01\n\n1. Fork \u9879\u76ee\n2. \u521b\u5efa\u529f\u80fd\u5206\u652f (`git checkout -b feature/amazing-feature`)\n3. \u63d0\u4ea4\u66f4\u6539 (`git commit -m 'Add amazing feature'`)\n4. \u63a8\u9001\u5230\u5206\u652f (`git push origin feature/amazing-feature`)\n5. \u521b\u5efa Pull Request\n\n## \ud83d\udcc4 \u8bb8\u53ef\u8bc1\n\n\u672c\u9879\u76ee\u91c7\u7528 MIT \u8bb8\u53ef\u8bc1 - \u67e5\u770b [LICENSE](LICENSE) \u6587\u4ef6\u4e86\u89e3\u8be6\u60c5\n\n## \ud83d\udd17 \u76f8\u5173\u9879\u76ee\n\n- [ai_runner_hjy](https://pypi.org/project/ai-runner-hjy/): AI\u670d\u52a1\u8fd0\u884c\u5668\n- [taskmanager_hjy](https://pypi.org/project/taskmanager-hjy/): \u4efb\u52a1\u7ba1\u7406\u5668\n- [datamanager_hjy](https://pypi.org/project/datamanager-hjy/): \u6570\u636e\u7ba1\u7406\u5668\n- [configmanager_hjy](https://pypi.org/project/configmanager-hjy/): \u914d\u7f6e\u7ba1\u7406\u5668\n\n---\n\n**keymanager_hjy** - \u8ba9API\u5b89\u5168\u53d8\u5f97\u7b80\u5355\u800c\u5f3a\u5927 \ud83d\udee1\ufe0f\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Zero-config, professional-grade API security manager. Elegant authentication, rate limiting, and audit logging for modern Python APIs.",
    "version": "0.3.0",
    "project_urls": {
        "Changelog": "https://github.com/hjy/keymanager_hjy/releases",
        "Documentation": "https://github.com/hjy/keymanager_hjy#readme",
        "Homepage": "https://github.com/hjy/keymanager_hjy",
        "Issues": "https://github.com/hjy/keymanager_hjy/issues",
        "Repository": "https://github.com/hjy/keymanager_hjy.git"
    },
    "split_keywords": [
        "security",
        " authentication",
        " api",
        " rate-limiting",
        " audit-logging"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "356f72bf31f3b69faf67a5a676d7ce3af08135c82100be397f67dfc05f4f66fa",
                "md5": "72e032e439573f8d71fa7524118a753a",
                "sha256": "d8ca163fc7d35a2efe4679e2c6feb8041395a91c169096206109f7a2b6d135fd"
            },
            "downloads": -1,
            "filename": "keymanager_hjy-0.3.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "72e032e439573f8d71fa7524118a753a",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 34328,
            "upload_time": "2025-08-18T01:32:03",
            "upload_time_iso_8601": "2025-08-18T01:32:03.777962Z",
            "url": "https://files.pythonhosted.org/packages/35/6f/72bf31f3b69faf67a5a676d7ce3af08135c82100be397f67dfc05f4f66fa/keymanager_hjy-0.3.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "974bae118363ed980c05dec7188fe1b41ee38ff20fcf13702418201e6ffebd50",
                "md5": "828ca1fd7ded9328db5173cfb67a20b8",
                "sha256": "6d04acf4c75f98fbc2f65e258c2d773a77d5ab66591b0d52c8b246c31899e5fd"
            },
            "downloads": -1,
            "filename": "keymanager_hjy-0.3.0.tar.gz",
            "has_sig": false,
            "md5_digest": "828ca1fd7ded9328db5173cfb67a20b8",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 29393,
            "upload_time": "2025-08-18T01:32:05",
            "upload_time_iso_8601": "2025-08-18T01:32:05.009590Z",
            "url": "https://files.pythonhosted.org/packages/97/4b/ae118363ed980c05dec7188fe1b41ee38ff20fcf13702418201e6ffebd50/keymanager_hjy-0.3.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-08-18 01:32:05",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "hjy",
    "github_project": "keymanager_hjy",
    "github_not_found": true,
    "lcname": "keymanager-hjy"
}
        
Elapsed time: 0.76516s