A token manager for Python
Project description
pytokenx
pytokenx 是一个简单易用的 Python Token 管理器。它提供了生成、验证和管理 token 的功能。
特性
- 生成安全的随机 token, 长度可配置
- token支持过期时间
- 支持quota
- 同时支持多种token类型
- token数据的持久化,目前支持文件、以及SQLAlchemy,也可以用户自定义
- 支持装饰器
- 支持用户扩展数据存储和获取
安装
pip install pytokenx
使用
from pytokenx import TokenManager, FileTokenStorage, token_validator, flask_token_validator, TokenStorage
# 使用文件存储
token_manager = TokenManager(FileTokenStorage("tokens.json"))
# sqlite存储
# token_manager = TokenManager(SQLAlchemyTokenStorage(connection_string="sqlite:///test.db"))
# 自定义存储
# class CustomTokenStorage(TokenStorage):
# xxxx
# 生成token
token1 = token_manager.generate_token(user_id="test_user", expiry= timedelta(days=1))
print("generate token1 with expiry ",token1) # MieZqFUchiasygXW
# 验证token
token_data = token_manager.validate_token(token1)
print("validate token1 pass", token_data)
# 生成带quota的token
token2 = token_manager.generate_token(expiry= timedelta(days=1), quota=10)
print("generate token2 with quota",token2)
# 验证带quota的token, 仅验证,不扣除
token_data = token_manager.validate_token(token2, cost_quota=2 , deduct_quota=False)
print("validate token2 pass", token_data)
# 扣除quota
token_manager.deduct_quota(token2, 2)
# 获取token数据
token_data = token_manager.get_token_data(token2)
print("get token2 data",token_data)
# 使用装饰器
@token_validator(token_manager)
def my_function(token):
print(token)
print(token_manager.get_current_token_data()) # 从当前线程中获取当前token数据
my_function(token=token2)
# # 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(token2)
# 删除token
token_manager.delete_token(token2)
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.6.1.tar.gz
(8.2 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.6.1.tar.gz.
File metadata
- Download URL: pytokenx-1.6.1.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.9.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4afbf9b393e5f795374ea356a365098061daf615ab8fa16e0be99069388e1b9d
|
|
| MD5 |
5b83eebf61c5422f9ab4e244b345dd6c
|
|
| BLAKE2b-256 |
0be2c6a8a7163b42b6798b7ca08503fc28ead555c33d6a6a258f2ec47b5c9ff7
|
File details
Details for the file pytokenx-1.6.1-py3-none-any.whl.
File metadata
- Download URL: pytokenx-1.6.1-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.9.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8c065e97a8a634b136f808604bea26d9090c85e2bd5ea7f8ba87b97c472ecfaf
|
|
| MD5 |
a91892aaeed349f12c097202c08f307e
|
|
| BLAKE2b-256 |
5ec36665383c44e95fce8867c3009a1cf12d25e28d84aaab511918e3d344f545
|