Skip to content

中文 | 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.kv module.


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) -> str
lua
totp(secret: string, digits: number?, period: number?) -> string

Parameters:

  • secret - Base32 encoded key. Must be ≥16 characters and contain only Base32 characters (A-Z, 2-7, spaces allowed); otherwise an empty string is returned
  • digits - number of code digits, default 6
  • period - 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) -> str
lua
steamGuard(secret: string) -> string

Parameters:

  • 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) -> bool
lua
verify(secret: string, code: string, digits: number?, period: number?, window: number?) -> boolean

Parameters:

  • secret - Base32 encoded key. Must be ≥16 characters and contain only Base32 characters (A-Z, 2-7, spaces allowed); otherwise False/false is returned
  • code - the code to verify
  • digits - number of code digits, default 6
  • period - time step in seconds, default 30
  • window - 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) -> int
lua
remaining(period: number?) -> number

Parameters:

  • 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 functionLua functionDescriptionParameters
totp(secret, digits?, period?)totp(secret, digits?, period?)Generate a TOTP codesecret: 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 codesecret: key
Return: 5-digit numeric code
verify(secret, code, digits?, period?, window?)verify(secret, code, digits?, period?, window?)Verify a TOTP codesecret: key (≥16 chars Base32)
code: code
window: tolerance window (default 1)
Return: whether valid
remaining(period?)remaining(period?)Remaining valid secondsperiod: step seconds (default 30)
Return: remaining seconds