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-1.0.tar.gz
(5.2 kB
view details)
Built Distribution
File details
Details for the file pytokenx-1.0.tar.gz
.
File metadata
- Download URL: pytokenx-1.0.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d932d07dca055f51cbd78c0da16789c41e7d61df393f3d4729a01b7f7f2364bc |
|
MD5 | 7d0cc2b8b0b27fb15cc82eef40f7bc39 |
|
BLAKE2b-256 | 5d26421b55787bcc8b70521053d985987c0a90ec735770c378722e3d5f2d5476 |
File details
Details for the file pytokenx-1.0-py3-none-any.whl
.
File metadata
- Download URL: pytokenx-1.0-py3-none-any.whl
- Upload date:
- Size: 5.9 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 | c2c7b4f0d4ae9c05ccbd37689abd5b1a2067debe81ce7ef6ad051f479173eaf7 |
|
MD5 | 7812a3ca224d72d12fbc214ccc7c49b0 |
|
BLAKE2b-256 | 1ebce212f789c2958a7c7245c81d78bb87bb1fd332761bec6cf93e979f9ed24e |