A token manager for Python
Project description
pytokenx
pytokenx 是一个简单易用的 Python Token 管理器。它提供了生成、验证和管理 token 的功能。
特性
- 生成安全的随机 token, 长度可配置
- 支持 token 过期时间设置
- 同时支持多种token类型
- token数据的持久化,目前支持文件、以及SQLAlchemy,也可以用户自定义
- 支持装饰器
- 支持用户扩展数据存储和获取
安装
pip install pytokenx
使用
from pytokenx import TokenManager, FileTokenStorage, SQLAlchemyTokenStorage
# 使用文件存储
token_manager = TokenManager(FileTokenStorage("tokens.json"))
# sqlite存储
# token_manager = TokenManager(SQLAlchemyTokenStorage(connection_string="sqlite:///test.db"))
token = token_manager.generate_token(user_id="test_user", extra_data = {"name": "test_name"}) # 生成token
print(token) # MieZqFUchiasygXW
token_data = token_manager.validate_token(token) # 验证token
if token_data:
print(token_data) # {'token': 'MieZqFUchiasygXW', 'token_type': 'default', 'user_id': 'test_user', 'extra_data': {'name': 'test_name'}, 'created_at': '2024-11-07T14:12:17.389325', 'expires_at': None, 'deleted_at': None, 'is_active': True}
else:
print("token 无效")
# 使用装饰器
@token_validator(token_manager)
def my_function(token):
print(token)
print(token_manager.get_token_data()) # {'token': 'MieZqFUchiasygXW', 'token_type': 'default', 'user_id': 'test_user', 'extra_data': {'name': 'test_name'}, 'created_at': '2024-11-07T14:12:17.389325', 'expires_at': None, 'deleted_at': None, 'is_active': True}
my_function(token=token)
# flask装饰器 使用示例
@app.route("/get_code", methods=["POST"])
@flask_token_validator(token_manager)
def get_code():
pass
# 通用装饰器
# 1.定义获取token的方法
def extract_token_func(*args, **kwargs) -> str:
return args[0]
# 使用装饰器
@token_validator(token_manager, extract_token_func=extract_token_func)
def my_function_custom(token):
print(token)
my_function_custom(token)
token_manager.delete_token(token) # 删除token
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pytokenx-1.5.tar.gz
(7.8 kB
view details)
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 pytokenx-1.5.tar.gz.
File metadata
- Download URL: pytokenx-1.5.tar.gz
- Upload date:
- Size: 7.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d1fdfda6a332323cf3a99d76871183d0dadc67b2f7c9bdaa3735f05fe8708dc1
|
|
| MD5 |
08978fed702af50efe19e5cd688b9f16
|
|
| BLAKE2b-256 |
81a02192f72e0f0b40617e4cb59c4263368937f6a52faf8c82e30ff75052470c
|
File details
Details for the file pytokenx-1.5-py3-none-any.whl.
File metadata
- Download URL: pytokenx-1.5-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1a7a01b115f727fa941220f422e79f6a8e492edcbf94c2621b7274d194b5ee43
|
|
| MD5 |
83626526de211735fce67c78eac07089
|
|
| BLAKE2b-256 |
b4fcfb4b22ca69e8a50a5638479f287f24d51ce4601a25c03e6bd7c4d8140f14
|