AuthForge Python SDK
Official Python SDK for AuthForge: credit-based license key authentication with Ed25519-verified responses.
Uses cryptography for Ed25519 verification. Works on Python 3.9+.
How licensing works
- Activate online.
login()callsPOST /auth/validate. The server checks revocation, expiry, HWID binding, and credits, then returns an Ed25519-signed session with a TTL. - Run through the grace period. By default the app keeps running on the signed session with no further network calls. A background check re-verifies the signature locally and fails when the session TTL expires. The grace period equals the session TTL: default 24h, and the server clamps requested values to 1h to 7d.
- Optionally enable online check-ins. With
online_heartbeat=True, the SDK also callsPOST /auth/heartbeateveryheartbeat_intervalseconds for fast revocation and concurrent-use detection.
Separately, for machines that can never reach the internet, an operator can mint a signed offline license file (.authforge) in the AuthForge dashboard or Developer API. The SDK verifies it locally with your app public key: see Offline license files.
Features
Everything in this list ships in authforge.py today:
- License validation via
POST /auth/validate, returning a signed session payload. - Ed25519 signature verification on every
/auth/validateand/auth/heartbeatresponse; tampered or unsigned responses are rejected. - Key rotation:
public_keyaccepts a single key, a list of keys, or a comma-separated string. The SDK trusts a signature that matches any key in the list, so you can roll the server-side signing key without breaking deployed clients. - Grace period by default: after one successful activation, the app runs on the signed session (no network) until the TTL expires.
- Online check-ins (opt-in): periodic
/auth/heartbeatcalls for fast revocation and concurrent-use detection. - Offline license files (
.authforge):login_from_file()/verify_license_file()verify a cloud-minted, Ed25519-signed file with zero network access for air-gapped machines. - Nonce anti-replay: a fresh 128-bit nonce is sent on every request and the echoed nonce in the signed payload is checked before the response is accepted.
- HWID fingerprinting: deterministic device hash from MAC + CPU + disk serial, with graceful per-component fallback.
hwid_override: bind to any identity instead of the machine (for exampletg:<id>,discord:<id>).- Seat enforcement: the server binds each HWID into a license's free slots up to
maxHwidSlots;hwid_count/max_hwid_slotsare surfaced on the result. A shared (unlimited-seat) key skips per-device binding. - Self-ban (
self_ban()) for anti-tamper response, both pre-session and post-session. - Grace period duration control via
ttl_seconds, with server-side clamping to[3600, 604800]. - App variables / license variables for feature flags and tiered licensing.
- Automatic retries for rate-limited and transient network failures, with a fresh nonce per retry.
Installation
Install from PyPI as authforge-sdk. In code, import the authforge module:
pip install authforge-sdk
Alternative: copy authforge.py into your project if you need a single-file vendored layout (you must still satisfy the cryptography dependency yourself).
Quick Start
After pip install authforge-sdk (or vendoring authforge.py), use:
from authforge import AuthForgeClient
client = AuthForgeClient(
app_id="YOUR_APP_ID", # from your AuthForge dashboard
app_secret="YOUR_APP_SECRET", # from your AuthForge dashboard
public_key="YOUR_PUBLIC_KEY", # from your AuthForge dashboard
)
license_key = input("Enter license key: ")
if client.login(license_key):
print("Activated!")
# Your app logic here. The app runs through the grace period by default:
# no further network calls until the session TTL expires.
else:
print("Invalid license key.")
exit(1)
To detect revocations quickly (or concurrent use), enable online check-ins:
client = AuthForgeClient(
app_id="YOUR_APP_ID",
app_secret="YOUR_APP_SECRET",
public_key="YOUR_PUBLIC_KEY",
heartbeat_interval=900, # check in every 15 minutes
online_heartbeat=True, # periodic POST /auth/heartbeat
)
Configuration
| Parameter | Type | Default | Description |
|---|---|---|---|
app_id |
str | required | Your application ID from the AuthForge dashboard |
app_secret |
str | required for online APIs; None / "" for login_from_file only |
Your application secret from the AuthForge dashboard. Do not ship it in air-gapped binaries. |
public_key |
str | Sequence[str] |
required | App Ed25519 public key(s) (base64) from dashboard. Pass one key, a list of keys, or a comma-separated string to trust multiple keys during rotation (see Key rotation). |
heartbeat_mode |
str | None |
None |
Deprecated shim (see Migrating from heartbeat_mode). Use online_heartbeat instead. |
heartbeat_interval |
int | 900 |
Seconds between background checks (minimum 10; default 15 min). Applies to both grace period checks and online check-ins. |
api_base_url |
str | https://auth.authforge.cc |
API endpoint |
on_failure |
callable | None |
Callback `(reason: str, exc: Exception |
request_timeout |
int | 15 |
HTTP request timeout in seconds |
ttl_seconds |
int | None |
None (server default: 86400) |
The grace period duration: how long the app keeps running on the signed session without contacting AuthForge. Server clamps to [3600, 604800] (1h to 7d); preserved across check-in refreshes. |
hwid_override |
str | None |
None |
Optional custom hardware/subject identifier. When set to a non-empty value, the SDK uses it instead of machine fingerprinting. |
online_heartbeat |
bool (keyword-only) | False |
Enable online check-ins: periodic POST /auth/heartbeat for fast revocation and concurrent-use detection. |
Identity-based binding example (Telegram/Discord)
client = AuthForgeClient(
app_id="YOUR_APP_ID",
app_secret="YOUR_APP_SECRET",
public_key="YOUR_PUBLIC_KEY",
hwid_override=f"tg:{telegram_user_id}", # or f"discord:{discord_user_id}"
)
Key rotation
public_key is a trust list. To rotate the server-side signing key without a
flag-day, ship the new key alongside the previous one; the SDK accepts a
signature that matches any entry:
client = AuthForgeClient(
app_id="YOUR_APP_ID",
app_secret="YOUR_APP_SECRET",
public_key=["NEW_PUBLIC_KEY", "PREVIOUS_PUBLIC_KEY"], # or "NEW,PREVIOUS"
)
client.public_keys exposes the full trust list; client.public_key is the
first (primary) entry.
Grace period and online check-ins
Grace period (default). After a successful activation, the SDK re-verifies the cached signed session payload and checks the expiry timestamp locally at each heartbeat_interval: no network calls. When the session TTL expires, it triggers failure with session_expired. The grace period is session continuation within the session TTL (default 24h, server clamps 1h to 7d) after one successful online activation. It is not persistent offline licensing, and a mid-session revocation is not picked up until the next online validate or check-in.
Online check-ins (opt-in, online_heartbeat=True). The SDK calls /auth/heartbeat every heartbeat_interval seconds with a fresh nonce, verifies signature + nonce, and triggers failure on invalid session state. Use this when you need fast revocation propagation or concurrent-use detection. A definitive rejection (revoked, hwid_mismatch, blocked, ...) clears the stored session immediately; every other failure (network, rate_limited, system_error, no_credits, unrecognized responses, ...) is transient and keeps it until the session TTL runs out. See Background check failures.
Tune ttl_seconds to set the grace period duration (server default 24h, clamped to 1h to 7d).
Offline license files (.authforge)
For machines that never connect to the internet, the operator mints a signed offline license file in the AuthForge dashboard (License page -> Mint .authforge file) or via POST /v1/licenses/{licenseKey}/offline-files. The file is a standalone Ed25519-signed document; the SDK verifies it with only your app public key and the machine HWID. It never contacts AuthForge and never starts online check-ins. Pass app_secret=None (or "") so the air-gapped binary does not contain the App Secret.
| Grace period (default) | Offline license file | |
|---|---|---|
| Needs network | Once, at login() |
Never on the end machine |
| What is verified | Signed session from /auth/validate |
Signed document minted in the cloud |
| Lifetime | Session TTL: 1h to 7d | Operator-chosen expiry or lifetime (perpetual licenses only) |
| Revocation | Picked up at the next online validate / check-in | Not reachable: the file stays valid until its own expiry |
| Cost | 1 credit per login() |
1 credit per mint; verifying is free |
from authforge import AuthForgeClient
client = AuthForgeClient(
app_id="YOUR_APP_ID",
app_secret=None, # login_from_file does not use the App Secret; do not ship it in air-gapped builds
public_key="YOUR_PUBLIC_KEY",
on_failure=lambda reason, exc: print(reason, exc),
)
# 1. Write an activation request the operator drops into the mint dialog:
client.write_activation_request("machine.authforge-request")
# 2. Later, authorize from the minted file (path or armored text). No network.
if client.login_from_file("license.authforge"):
info = client.get_offline_license()
print("Offline license OK until", info["expires_at"] or "forever")
print(client.get_license_variables())
Collect the HWID from the same SDK build that will load the file: fingerprints are not portable across SDKs or languages. After login_from_file(), get_session_kind() returns "offline" ("online" after login(), None when logged out).
verify_license_file() (module function and client method) performs the same checks without touching client state. Failure codes, in check order: bad_armor, bad_signature, unsupported_version, malformed_payload, wrong_app, expired, hwid_mismatch. login_from_file() reports them through on_failure("offline_login_failed", exc) and returns False; it never calls os._exit.
File format (version 1): PEM-style armor with informational headers, a base64 JSON payload (v, appId, licenseKey, jti, kid, issuedAt, expiresAt, hwid policy, optional label/variable snapshots) and a detached Ed25519 signature over the UTF-8 bytes of the base64 payload string - the same contract as /auth/validate. See offline_license_vectors.json for conformance vectors.
Migrating from heartbeat_mode
Earlier releases required heartbeat_mode="LOCAL" or "SERVER". The argument is now optional and deprecated; it still works but emits a DeprecationWarning.
heartbeat_mode="LOCAL"maps to the default behavior (the grace period): just remove the argument.heartbeat_mode="SERVER"maps toonline_heartbeat=True.- If both arguments are set, either one enables online check-ins:
heartbeat_mode="SERVER"is not overridden byonline_heartbeat=False.
# Before
client = AuthForgeClient(app_id, app_secret, public_key, heartbeat_mode="SERVER")
# After
client = AuthForgeClient(app_id, app_secret, public_key, online_heartbeat=True)
For code that still reads it, client.heartbeat_mode remains available and reflects the effective policy ("SERVER" when online check-ins are enabled, "LOCAL" otherwise).
Billing
- 1
login()orvalidate_license()call = 1 credit (one/auth/validatedebit each). - 10 online check-ins on the same license = 1 credit (billed every 10th successful
/auth/heartbeat). Grace period checks are local and free.
A desktop app running 6h/day with online check-ins at a 15-minute interval burns ~3-4 credits/day. The server enforces /auth/heartbeat at 6 requests/minute per license key, so keep intervals at 10 seconds or higher and pick the interval based on how fast you need revocations to propagate (they always land on the next check-in).
Methods
| Method | Returns | Description |
|---|---|---|
login(license_key) |
bool |
Activates: validates the key online and stores the signed session (sessionToken, expiresIn, appVariables, licenseVariables) |
validate_license(license_key) |
ValidateLicenseResult |
Same /auth/validate + signatures as login; does not store session or start background checks; returns a dict with valid / code and never calls on_failure or os._exit |
self_ban(...) |
dict |
Requests /auth/selfban to blacklist HWID/IP and optionally revoke (session-authenticated only) |
login_from_file(path_or_text) |
bool |
Authorizes from an offline .authforge file with no network; never starts background checks; failures go to on_failure("offline_login_failed", …) |
verify_license_file(path_or_text, *, now=None) |
VerifyLicenseFileResult |
Verifies a .authforge file with this client's app id / keys / HWID without changing state |
get_offline_license() |
dict | None |
Metadata of the offline file in use (jti, expires_at, hwid_policy, …) |
get_session_kind() |
"online" | "offline" | None |
Which kind of session the client holds (None when logged out) |
get_hwid() |
str |
The HWID this client sends (or hwid_override); customers share it to receive a bound file |
create_activation_request(**kwargs) |
str |
Unsigned .authforge-request for this machine. No network, no secret. Hostname omitted unless include_machine_name=True |
write_activation_request(path, **kwargs) |
None |
Writes that file as UTF-8 |
logout() |
None |
Stops background checks and clears all session/auth state |
is_authenticated() |
bool |
True when an active authenticated session exists |
get_session_data() |
dict | None |
Full decoded payload map |
get_app_variables() |
dict | None |
App-scoped variables map |
get_license_variables() |
dict | None |
License-scoped variables map |
Failure Handling
If authentication fails (activation rejected, check-in fails, signature mismatch, grace period expired, etc.), the SDK calls your on_failure callback if one is provided. If no callback is set, the SDK calls os._exit(1) to terminate the process. This is intentional: it prevents your app from running without a valid license.
validate_license() does not trigger on_failure or os._exit: check result["valid"] and result["code"].
Recognized server errors (KNOWN_SERVER_ERRORS):
invalid_app, invalid_key, expired, revoked, hwid_mismatch, no_credits, app_burn_cap_reached, blocked, rate_limited, replay_detected, app_disabled, session_expired, revoke_requires_session, bad_request, malformed_request, demo_quota_exceeded, system_error. Codes added to the server later are passed through unchanged.
Request retries are automatic inside the internal HTTP layer:
rate_limited, or HTTP 429 with no error code: retry after 2s, then 5s (max 3 attempts total)no_credits,demo_quota_exceededandapp_burn_cap_reachedalso arrive as HTTP 429 but are never retried immediately; online check-ins try again at the next interval- network failure: retry once after 2s
- every retry regenerates a fresh nonce
Background check failures
Background check failures reach on_failure("heartbeat_failed", exc), where exc is an AuthForgeError (a ValueError subclass):
exc.code: the server's error code from the response body, whatever the HTTP status (revoked,expired,hwid_mismatch,blocked,session_expired,rate_limited,no_credits,system_error,malformed_request, ...), or an SDK code:network_error,timeout,http_error_<status>(non-JSON error body),invalid_json_response,unexpected_response,signature_mismatch,nonce_mismatch.exc.transient/exc.fatal: the classification.is_transient_error(code_or_exc)is the same check as a function;DEFINITIVE_ERROR_CODESis the full list of fatal codes.
A failed check-in only counts as an AuthForge verdict when the body is a JSON object with "status": "failed" and a non-empty error string. Any other failure body (for example a proxy's JSON, or {"status": "revoked"}) is reported as unexpected_response, whose message includes the raw status and error.
| Kind | Codes | What the SDK does |
|---|---|---|
| Fatal | revoked, expired, hwid_mismatch (the HWID is no longer bound to the license, for example after an HWID reset), blocked (HWID/IP blacklisted or not whitelisted), session_expired (also raised locally when the grace period or session TTL runs out), malformed_request, app_disabled, invalid_app, signature_mismatch |
Clears the stored session (as logout() does) and stops background checks before calling on_failure, so the grace period cannot keep the app running on it. on_failure may call login() again. |
| Transient | Everything else: network_error, timeout, rate_limited, system_error, no_credits, demo_quota_exceeded, app_burn_cap_reached, bad_request, invalid_key, every http_error_<status>, invalid_json_response, unexpected_response, nonce_mismatch, any unrecognized code |
Keeps the session and checks in again on the next heartbeat_interval. Once the signed session's TTL has passed, the next transient failure is reported as a fatal session_expired instead. |
Transient failures only keep checking in if on_failure returns normally. Without a callback, any failure still ends the process with os._exit(1). Heartbeat network failures are reported once, as heartbeat_failed with code network_error or timeout, not as a separate network_error reason.
on_failure runs on the background heartbeat thread with no SDK lock held, so calling logout(), is_authenticated() or login() from it is safe. A check-in that is still in flight when you call logout() or login() is discarded: it cannot restore the old session or report a failure for it.
To tolerate short outages but exit on a definitive answer:
import os
import sys
from authforge import AuthForgeClient, AuthForgeError
def handle_auth_failure(reason, exc):
if reason == "heartbeat_failed" and isinstance(exc, AuthForgeError) and exc.transient:
# Connectivity problem or AuthForge overloaded: keep running. The SDK
# retries every heartbeat_interval and reports session_expired (fatal)
# once the ttl_seconds grace period is used up.
print(f"AuthForge check-in failed ({exc.code}), retrying", file=sys.stderr)
return
code = exc.code if isinstance(exc, AuthForgeError) else exc
print(f"License check failed: {reason} ({code})", file=sys.stderr)
# on_failure can run on the heartbeat thread, where sys.exit() only ends that thread.
os._exit(1)
client = AuthForgeClient(
app_id="YOUR_APP_ID",
app_secret="YOUR_APP_SECRET",
public_key="YOUR_PUBLIC_KEY",
online_heartbeat=True,
ttl_seconds=3600, # retry window for transient check-in failures
on_failure=handle_auth_failure,
)
Self-ban (tamper response)
Use self_ban() when anti-tamper checks trigger:
# Post-session (authenticated): defaults to revoke + HWID/IP blacklist.
client.self_ban()
# Pre-session: pass license_key, SDK automatically disables revoke_license.
client.self_ban(license_key="AF-XXXX-XXXX-XXXX")
# Custom flags:
client.self_ban(
blacklist_hwid=True,
blacklist_ip=True,
revoke_license=False,
)
self_ban() automatically chooses mode:
- Uses post-session mode when a session token is available (
session_tokenarg or current SDK session). - Falls back to pre-session mode using
license_key+ nonce + app secret. - In pre-session mode, revoke is forced off client-side to avoid unsafe key revocations.
- Not available after
login_from_file(): offline sessions have no server session, soself_ban()with no explicitlicense_key/session_tokenraisesValueError("offline_session")without contacting the server.
How It Works
-
Activate:
login()useshwid_overrideif provided; otherwise it collects a hardware fingerprint (MAC, CPU, disk serial). It then generates a random nonce and sends everything to the AuthForge API. The server validates the license key, binds the HWID, deducts a credit, and returns a signed payload with a TTL. The SDK verifies the Ed25519 signature and nonce to prevent replay attacks. -
Background checks: a daemon thread wakes at the configured interval. By default it enforces the grace period: it re-verifies the stored signature and checks expiry without network calls. With
online_heartbeat=True, it instead sends/auth/heartbeatwith a fresh nonce and verifies the response. -
Crypto: both
/validateand/heartbeatresponses are signed by AuthForge with your app's Ed25519 private key. The SDK verifies every signedpayloadusing your configuredpublic_keyand rejects tampered responses.
Hardware ID
The SDK generates a deterministic hardware fingerprint by hashing:
- MAC address
- CPU identifier
- Disk serial number
Each component falls back gracefully if it can't be read (e.g. permissions issues). The HWID is sent with every auth request so the server can enforce per-device license limits.
For non-device identities (for example Telegram users), pass hwid_override such as tg:<user_id>.
Test Vectors
The shared test_vectors.json file validates cross-language Ed25519 verification behavior. offline_license_vectors.json (generated from a fixed test seed in the Node SDK repo) is the cross-SDK conformance suite for .authforge offline license files: good files plus the bad_signature, wrong key, wrong_app, expired, hwid_mismatch, unsupported_version and bad_armor rejects.
Requirements
- Python 3.9+
- Dependency:
cryptography
License
MIT
Release files for authforge-sdk 1.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| authforge_sdk-1.4.0.tar.gz | 32.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| authforge_sdk-1.4.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 57.9 kB
Release files / authforge_sdk-1.4.0.tar.gz
| Download URL | authforge_sdk-1.4.0.tar.gz |
|---|---|
| Size | 32.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
30232c0cc1ca37a9de3bcbce9676b8398901fe02364f9d0185e6d431986283bc
|
|
BLAKE2b-256 checksum How to use checksums |
b5d33b651527e0bde526c1e1623fd5144c9add8fd6507e8871fd7b3b1879e281
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 23, 2026.
Transparency logRelease files / authforge_sdk-1.4.0-py3-none-any.whl
| Download URL | authforge_sdk-1.4.0-py3-none-any.whl |
|---|---|
| Size | 25.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c9f4a2092f38ab36ced817a62c30b6631ebff894e375ef703e57c83d73b05346
|
|
BLAKE2b-256 checksum How to use checksums |
cca5acd33069be8cd2b5f86183edd27de49195681598cdabb1a1c2715f2c2874
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 23, 2026.
Transparency log