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)
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-0.4.tar.gz
(5.0 kB
view details)
Built Distribution
File details
Details for the file pytokenx-0.4.tar.gz
.
File metadata
- Download URL: pytokenx-0.4.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4dd60ce765c8b74a8a5c5920c8946f572db7a17077d07e686cfee4a96e10fae6 |
|
MD5 | b84eca85d69a8140888c5f21f8b7d718 |
|
BLAKE2b-256 | 5b4ab755de4e7976c3e26ce3e21f943c7750cae568efadc88250e3d67a8c64ab |
File details
Details for the file pytokenx-0.4-py3-none-any.whl
.
File metadata
- Download URL: pytokenx-0.4-py3-none-any.whl
- Upload date:
- Size: 5.6 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 | 287c355e29c22ffcb39f4092a8175b8c8e270e3a78ac4cd5260f81c876f54afa |
|
MD5 | a7ec719df7087375ffccc66645b34210 |
|
BLAKE2b-256 | 84da7179544fb3ebc0a6ccbcd8ea3d4ab3e03b93abfde89525163b0d9d71508e |