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.tar.gz
(8.0 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.tar.gz.
File metadata
- Download URL: pytokenx-1.6.tar.gz
- Upload date:
- Size: 8.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 |
a6f9a6024b8acfa6579d84b680679cad30a7f98b55fd225b458acd2f5f88e88d
|
|
| MD5 |
5c5a47f601751f52f81f0a3fce899fbb
|
|
| BLAKE2b-256 |
cc7ec02291c770c020d6bec7ac8391c2d7de48841303a06a2a69d338a0fa4129
|
File details
Details for the file pytokenx-1.6-py3-none-any.whl.
File metadata
- Download URL: pytokenx-1.6-py3-none-any.whl
- Upload date:
- Size: 7.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 |
d3e5bc60ef364efa319edfb8eefc4d8b9fd0ee219470f464a463fddd7ff2077a
|
|
| MD5 |
2c13ac8f6d035722b825b1318299af6c
|
|
| BLAKE2b-256 |
2919a16598d7f1891e4871653bf705425dc91b8f9bacc40e4bdc2a05e40bce6e
|