中文 | English
API: wingman.verification
A pure utility function module for TOTP (two-factor authentication).
Module Overview
The verification module provides stateless TOTP utility functions:
- TOTP generation - generate time-based one-time codes
- Steam Guard - generate Steam Guard codes
- Code verification - verify whether a TOTP code is valid
- Remaining time - get the remaining valid seconds of the current code
All functions are stateless pure functions and do not involve key persistence. If you need to store keys, use the
wingman.kvmodule.
Generate a TOTP Code
totp(secret, digits?, period?) / totp(secret, digits?, period?)
Description: Generates a time-based one-time code (TOTP, RFC 6238).
Function signature:
python
totp(secret: str, digits: int = 6, period: int = 30) -> strlua
totp(secret: string, digits: number?, period: number?) -> stringParameters:
secret- Base32 encoded key. Must be ≥16 characters and contain only Base32 characters (A-Z,2-7, spaces allowed); otherwise an empty string is returneddigits- number of code digits, default 6period- time step in seconds, default 30
Returns:
- A numeric code string
python
from wingman import verification
# 默认 6 位,30 秒步长
code = verification.totp("JBSWY3DPEHPK3PXP")
print(f"TOTP: {code}")
# 自定义 8 位,60 秒步长
code = verification.totp("JBSWY3DPEHPK3PXP", digits=8, period=60)Generate a Steam Guard Code
steam_guard(secret) / steamGuard(secret)
Description: Generates a Steam Guard code (5 digits).
Function signature:
python
steam_guard(secret: str) -> strlua
steamGuard(secret: string) -> stringParameters:
secret- Steam Guard key (Base32 of ≥16 characters, or a Base64 encoded key)
Returns:
- A 5-digit numeric code (00000-99999, zero-padded)
python
from wingman import verification
code = verification.steam_guard("STEAM_GUARD_SECRET")
print(f"Steam Guard: {code}")Verify a TOTP Code
verify(secret, code, digits?, period?, window?) / verify(secret, code, digits?, period?, window?)
Description: Verifies whether a TOTP code is valid. Supports a time-window tolerance.
Function signature:
python
verify(secret: str, code: str, digits: int = 6, period: int = 30, window: int = 1) -> boollua
verify(secret: string, code: string, digits: number?, period: number?, window: number?) -> booleanParameters:
secret- Base32 encoded key. Must be ≥16 characters and contain only Base32 characters (A-Z,2-7, spaces allowed); otherwiseFalse/falseis returnedcode- the code to verifydigits- number of code digits, default 6period- time step in seconds, default 30window- time-window tolerance count, default 1 (allows 1 period before/after)
Returns:
- Whether the code is valid
python
from wingman import verification
secret = "JBSWY3DPEHPK3PXP"
code = verification.totp(secret)
# 验证当前验证码
if verification.verify(secret, code):
print("验证通过")
# 带容错窗口(允许前后各 2 个周期)
if verification.verify(secret, code, window=2):
print("验证通过(宽松模式)")Get the Remaining Valid Time
remaining(period?) / remaining(period?)
Description: Gets the remaining valid seconds of the current TOTP code.
Function signature:
python
remaining(period: int = 30) -> intlua
remaining(period: number?) -> numberParameters:
period- time step in seconds, default 30
Returns:
- Remaining valid seconds
python
from wingman import verification
# 默认 30 秒步长
secs = verification.remaining()
print(f"验证码还有 {secs} 秒有效")
# 自定义步长
secs = verification.remaining(60)Available Interfaces
| Python function | Lua function | Description | Parameters |
|---|---|---|---|
totp(secret, digits?, period?) | totp(secret, digits?, period?) | Generate a TOTP code | secret: Base32 key (≥16 chars) digits: digit count (default 6) period: step seconds (default 30) Return: code string |
steam_guard(secret) | steamGuard(secret) | Generate a Steam Guard code | secret: key Return: 5-digit numeric code |
verify(secret, code, digits?, period?, window?) | verify(secret, code, digits?, period?, window?) | Verify a TOTP code | secret: key (≥16 chars Base32) code: code window: tolerance window (default 1) Return: whether valid |
remaining(period?) | remaining(period?) | Remaining valid seconds | period: step seconds (default 30) Return: remaining seconds |