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.1.tar.gz
(5.4 kB
view details)
Built Distribution
File details
Details for the file pytokenx-1.1.tar.gz
.
File metadata
- Download URL: pytokenx-1.1.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 28df36951ba0889fd0817878a3b632cf92ac3dc5826fca086568cc5694382c0b |
|
MD5 | 3dd6ad31968e051a3cdc8e49dc6ec567 |
|
BLAKE2b-256 | 1f4848075e908e1f0a28092d215d18a325ad87fcbe30fe70faed674aa57f1c05 |
File details
Details for the file pytokenx-1.1-py3-none-any.whl
.
File metadata
- Download URL: pytokenx-1.1-py3-none-any.whl
- Upload date:
- Size: 6.1 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 | 0bc3175bbaf3b3d9204443d75d893be0149bedb8e56f9a0b4312a56acd21efef |
|
MD5 | 62401f5cc744af976d875cb5af192868 |
|
BLAKE2b-256 | e89b4bb9e77a2190b581cc440a26ee60e1a3235ab4a67b9ec285b5e1b929faf7 |