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.2.tar.gz
(5.5 kB
view details)
Built Distribution
File details
Details for the file pytokenx-1.2.tar.gz
.
File metadata
- Download URL: pytokenx-1.2.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 70234574d9fc9bdb1393cf2c0b48133346c1557c568af8f16258eaf439ea4cbb |
|
MD5 | ceabe469aca0bfa201a4c441e5c31979 |
|
BLAKE2b-256 | da7f27bd4cb15ce301535f1df519499caca0746c34dff2aba889d5d4460c6134 |
File details
Details for the file pytokenx-1.2-py3-none-any.whl
.
File metadata
- Download URL: pytokenx-1.2-py3-none-any.whl
- Upload date:
- Size: 6.2 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 | 456b0657216d3398055004bd13e4004b811ed89bb9d4ab4aa087fca44d73ffdf |
|
MD5 | c7f85482308be126ceac7f66810485e4 |
|
BLAKE2b-256 | 877f991bfe31c574384ed4932726b54c521d36d35b6db7b9f9475e566c37dd2d |