Verifying Signed JWT Licenses Inside a Compiled Python Executable
You can write the most carefully designed license check in the world, and it is still worthless if it runs as readable Python on the user’s machine. A branch that says if licensed: is one keystroke away from if True:. The interesting engineering problem isn’t whether to check a license — it’s making the check something an attacker has to work for.
This article is about the verification half of that problem: how to get a signed JWT license checked inside a compiled Python executable, with the public key and the signature-verification code baked into the binary. We’ll use Py2Native’s Pro plugin, because it’s the zero-config way to compile plain Python into a native binary that can carry that logic — no Cython syntax, no .pyx build scripts, no C extensions to write by hand.
Why License Verification Belongs Inside the Binary
The default advice for license checks is “call home.” Post the token to your server, get a yes/no back, gate the app. It’s simple, and it fails in five predictable ways:
- Offline users. Air-gapped and firewalled installs never reach your endpoint, so your app either breaks or defaults to “allow.”
- Blocked endpoints. One hosts-file entry or DNS rule and your server is unreachable.
- Spoofed responses. A local proxy that answers
{"valid": true}is a ten-line script. - Patched-out calls. The HTTP request is just Python. Delete it.
- Network latency on every launch. Users notice.
Moving the decision into the binary fixes the first four at once. The verification becomes local, offline, and part of the compiled artifact rather than a separate step a user can skip.
The key property that makes this work is asymmetry. An ES256 license is signed with a private key that never leaves your build machine. Verifying it requires only the public key, and a public key is useless for forging new licenses. So you can embed the public key and the verification routine into every copy of your product without handing out the ability to mint licenses.
That’s the theory. The practical question is how you get the verification code into a native binary without a week of C-extension work. With Py2Native, you write plain Python — third-party libraries stay unmodified and keep working, so any existing JWT or claims-handling code you already have still runs — and uv run py2native build takes care of the Cython invocation, C compilation, and linking. If you’re still deciding how to package the app itself, the sibling post on shipping a Python app without exposing your source code covers that side.
What a Signed JWT License Proof Looks Like
A JWT license is three base64url segments joined by dots:
header.payload.signature
- Header — declares the algorithm (
ES256: ECDSA over the P-256 curve with SHA-256). - Payload — the claims: who the license is for, what it grants, when it expires.
- Signature — the ES256 signature over
header.payload, produced with your private key.
A typical payload looks like this:
{
"iss": "RSJ Software GmbH",
"aud": "TimestampGIT",
"sub": "acme-corp",
"exp": 1798761600,
"entitlements": ["pro", "exports"]
}
iss (issuer) and aud (audience) are the two claims you should always pin, because they turn a generic “is this signature valid?” check into “is this signature valid and was this token minted for this product by us?” A token signed by your key for a different product should not unlock this one. exp gives you an expiry you can act on without recompiling. Anything else — seat counts, feature flags, customer IDs — rides along as custom claims.
The split of responsibility is:
- Private key — stays with you, on your signing machine.
- Public key — baked into the compiled executable at build time.
- Verification code — compiled into the executable alongside it.
Py2Native’s Pro plugin handles both halves. uv run py2native keygen generates an EC P-256 keypair, uv run py2native sign produces a license, and uv run py2native show lets you inspect and optionally verify a token against the public key.
Step-by-Step: Baking Verification into Your Compiled Executable
Step 1 — Install Py2Native and the Pro plugin
Install both packages with uv:
uv add py2native
uv add py2nativepro
py2native is the MIT-licensed open-source compiler (full compilation, uv embedding, cross-platform builds). py2nativepro is the proprietary plugin that adds JWT license verification, string compression, and the keygen / sign / show commands. Everything below runs through uv run py2native.
Step 2 — Generate a keypair
uv run py2native keygen private.pem public.pem
Keep private.pem off your build servers and out of version control. public.pem is the file you’ll hand to the compiler.
Step 3 — Create a JWT license
Write the claims you want to grant:
{
"iss": "RSJ Software GmbH",
"aud": "TimestampGIT",
"sub": "acme-corp",
"exp": 1798761600,
"entitlements": ["pro", "exports"]
}
Then sign it:
uv run py2native sign --private private.pem payload.json license.dat
Before shipping it to a customer, confirm what you actually signed:
uv run py2native show --public public.pem license.dat
show prints the JWT claims and, when you pass --public, verifies the signature against the public key.
Step 4 — Declare the runtime check and call it from your code
The Pro plugin exposes the ES256 verification routine as a compiled symbol. You get to it through a small .pxd declaration file — this is the only non-.py file you need, and it’s a declaration, not an implementation:
# _p2n_bootstrap.pxd
from _p2n_bootstrap cimport _runtime_verify_es256_jwt
cdef _runtime_verify_es256_jwt(token, expected_iss=*, expected_aud=*)
Now call it from your own module. The license string is whatever you read from the customer’s license file; the two keyword arguments pin the issuer and audience:
from pathlib import Path
def load_license(path: str = "license.dat") -> dict:
licenseString = Path(path).read_text().strip()
license = _runtime_verify_es256_jwt(licenseString, expected_iss="RSJ Software GmbH", expected_aud="TimestampGIT")
return license
claims = load_license()
if "exports" not in claims.get("entitlements", []):
raise SystemExit("This build requires the 'exports' entitlement.")
The function signature you’re calling is:
_runtime_verify_es256_jwt(token, expected_iss=*, expected_aud=*)
On success it hands you back the decoded claims; on failure it raises, so a bad or missing license stops the process rather than silently continuing.
Step 5 — Build with the license and public key
uv run py2native build main.py *.py --license license.dat --public public.pem
The Pro plugin bakes the signature-verification code and the public key directly into the executable. There’s no runtime dependency on a third-party crypto library — the elliptic-curve key verification is handled by compiled code inside the product itself. Add --library, --embed <dir>, or --wheel <dir> when you need a shared library (.pyd / .so / .dylib) or a uv-managed deployment directory instead of a bare executable.
Under the hood the pipeline is the same one that compiles everything else: glob your sources, dispatch plugins, generate the bootstrap and transpile to C, compile the C with platform flags, link. That’s what makes the embedded check work on Windows (PE, .pyd), Linux (ELF, auditwheel repair on wheels), and macOS (Mach-O, .dylib) from the same source tree.
Interpreting Verification Results and Handling Edge Cases
A successful verification returns the claims dictionary. That’s your gate for everything else — features, seat limits, export permissions — and it’s data you can check at any point in the app’s life, not just at startup.
Common failures and what they mean:
| Symptom | Likely cause | What to do |
|---|---|---|
| Raises on signature verification | Token signed by a different key, or tampered payload | Regenerate the license with your private key |
| Issuer/audience mismatch | Token minted for another product or tenant | Pin iss and aud to values you control; issue a correct token |
| Expired token | exp claim in the past |
Reissue, or move to short-lived tokens with automatic renewal |
| Missing or unreadable license file | Customer deleted or moved license.dat |
Prompt for a license path; fail closed |
| Verification works locally, fails in the shipped build | Built against a different public.pem than the one used to sign |
Rebuild with the matching public key |
Two of these deserve more thought than the others.
Revocation and renewal. Because the verification code is generic — it doesn’t hard-code which customer or which expiry — you can reissue licenses without recompiling. Short-lived tokens are the cleanest pattern: sign a license valid for, say, 30 days, have the app refresh it while it has network access, and let the token expire naturally if the customer stops paying. You get revocation granularity without shipping a new binary to anyone.
Monkey-patching. Compiled Python doesn’t make your program immune to tampering. Third-party libraries remain Python source, and a determined attacker can still patch around a check at runtime. What embedding changes is the cost: the verification routine and the public key are compiled, so there’s no readable if licensed branch to flip and no key file sitting next to the executable to swap. That’s a meaningful step up, and more than most commercial Python software currently bothers with.
The Hard Way vs. Py2Native’s Way
Everything above can be done by hand. You can write a C extension that pulls in an OpenSSL EVP context, link it into a CPython shared library, hand-write the argument parsing, and call it from a .pyx module you compile yourself. It works — until it doesn’t: build flags drift between MSVC, GCC, and Clang; CPython 3.14t’s free-threaded build changes what your extension can assume; and every release becomes a portability project.
Py2Native collapses that into one command. You write plain Python, use the Pro plugin, and get a native binary with verification baked in — Cython invocation, C compilation, and linking are handled for you. If you want the longer comparison of hand-rolled approaches, the Cython alternatives post walks through it.
FAQ
Can I use my own JWT library instead of the built-in runtime check?
Yes, but the Pro plugin’s _runtime_verify_es256_jwt is designed to be compiled and embedded, providing stronger protection. Using an external library may leave verification logic exposed.
What happens if someone extracts the public key from the binary? The public key alone cannot be used to forge licenses; it only verifies signatures. The private key remains secret, so attackers cannot generate valid tokens.
Do I need the Pro plugin for license verification?
Yes, the Pro plugin (py2nativepro) adds JWT license verification, keygen, sign, and show commands. The open-source core does not include these features.
How do I update the public key without recompiling? The public key is baked into the executable at build time. To change it, you must rebuild with the new key. For flexibility, consider using short-lived tokens and a revocation list.
Ship the Check, Not the Source
External license checks are a network dependency dressed up as security. Embedding an ES256 verification routine and a public key into a compiled binary makes the check local, offline, and far more expensive to defeat — and with Py2Native’s Pro plugin, the whole flow is keygen, sign, one .pxd declaration, one call, and a uv run py2native build.
Ready to try it? Add py2native and py2nativepro, generate your first keypair, and build a license-gated binary — start at py2native.dev.
Related posts
- How to Ship a Python App Without Exposing Your Source Code
- Automate Python Builds for Windows, Linux, and macOS
- Is Compiled Python Really Harder to Reverse Engineer?