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)
Release files for pytokenx 1.6.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pytokenx-1.6.1.tar.gz | 8.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pytokenx-1.6.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.2 kB
Release files / pytokenx-1.6.1.tar.gz
| Download URL | pytokenx-1.6.1.tar.gz |
|---|---|
| Size | 8.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4afbf9b393e5f795374ea356a365098061daf615ab8fa16e0be99069388e1b9d
|
|
BLAKE2b-256 checksum How to use checksums |
0be2c6a8a7163b42b6798b7ca08503fc28ead555c33d6a6a258f2ec47b5c9ff7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.9.20
|
Release files / pytokenx-1.6.1-py3-none-any.whl
| Download URL | pytokenx-1.6.1-py3-none-any.whl |
|---|---|
| Size | 8.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8c065e97a8a634b136f808604bea26d9090c85e2bd5ea7f8ba87b97c472ecfaf
|
|
BLAKE2b-256 checksum How to use checksums |
5ec36665383c44e95fce8867c3009a1cf12d25e28d84aaab511918e3d344f545
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.9.20
|