Checking a Signed License Key Inside a Python Application
You compiled your Python package into a native binary. Your source is no longer readable, your proprietary algorithms are no longer sitting in a site-packages folder waiting to be copied. So why would you still want a license check inside the app?
Because compiled code solves disclosure, not authorization. A compiled binary can still be copied from one machine to another, resold, or shipped inside a competitor’s product. If your business model depends on per-customer keys, seats, or expiring trials, you need a runtime gate that ties the app to a signed key — and that gate has to survive the same tampering pressure your source code does.
This article walks through how to check a signed license key inside a Python application using Py2Native’s Pro plugin: what the license artifact actually is, how to embed verification so it lives inside the compiled binary, how to interpret the result, and how to handle the failure paths without turning your support inbox into a landfill.
Why License Verification Matters for Compiled Python Apps
Shipping proprietary Python without any license enforcement leaves three doors open:
- Unauthorized redistribution. Nothing stops a customer from handing your binary to a third party. Without a signed key, there’s nothing tying the running process to a paying account.
- Tampering. Even compiled code can be attacked. If the check lives in editable Python glue, an attacker patches one
ifstatement and the gate disappears. - Lost revenue. Trials that never end, seats that multiply, and enterprise features that leak into the standard tier are all the same bug: the decision is made in a place the customer controls.
Compiling with Py2Native closes the first half of the problem. It transpiles your plain Python — no .pyx files, no C extensions, no manual cythonize step — into native machine code, so the logic you wrote is no longer readable text. (If you want the mechanics of that, see Compile Python Without Writing C Extensions or .pyx Files.)
The Pro plugin closes the second half. It adds ES256 JWT verification that is baked directly into the compiled binary:
- Zero-config. You don’t write C, you don’t link a third-party crypto library into your project, and you don’t manage a dependency tree for the verifier.
- Compiled in. The verification path is part of the binary you ship, not a Python module an attacker can shadow from
PYTHONPATH. - Offline. Verification is a pure local computation over the token and the embedded public key. No network call, no license server, no phone-home.
That last point is the one that usually decides the architecture. Offline verification is the only option that works for air-gapped environments, factory floors, and customers whose security review will never approve an outbound licensing ping.
What a Signed License Key Looks Like
The license is a JWT signed with an EC P-256 private key — an ES256 token. It carries claims you choose, plus the claims the verifier checks by default: the issuer (iss) and the audience (aud). Because it’s a signed JWT, the claims are tamper-evident: change a byte in the payload and the signature no longer validates.
The Pro plugin gives you the full lifecycle from the same CLI you already use to build:
uv run py2native keygen— generates an EC P-256 keypair:private.pemandpublic.pem.uv run py2native sign— signs a JSON payload with the private key and writeslicense.dat.uv run py2native show— displays the JWT claims, optionally verifying the signature against the public key.
The critical asymmetry: only the public key is embedded in the executable. Your private key never ships, never touches the customer’s machine, and never becomes part of your build artifact. Without it, nobody can mint a license that your binary will accept.
A second detail worth calling out: the elliptic-curve verification is implemented in compiled code inside the Pro plugin itself — no third-party verification libraries are pulled into your project. That keeps the attack surface and the dependency review small, which matters when your customers are the ones auditing your binary.
Step-by-Step: Embedding License Verification with Py2Native
1. Generate a keypair
Do this once per product line, and store the private key somewhere your build system can reach but your customers cannot.
uv run py2native keygen private.pem public.pem
private.pem stays with you. public.pem is what the build embeds.
2. Sign a license payload
Write the claims you want to bind to the license. iss and aud are the two the verifier checks by default, so set them to the values your application expects.
{
"iss": "RSJ Software GmbH",
"aud": "TimestampGIT",
"sub": "acme-corp",
"exp": 1798761600
}
Then sign it:
uv run py2native sign --private private.pem payload.json license.dat
license.dat is the artifact you hand to the customer.
3. Declare the verifier to the build
The Pro plugin exposes a runtime verifier through Py2Native’s generated bootstrap module. You declare it in a .pxd file so the build wires it up during compilation:
# license.pxd
cdef _runtime_verify_es256_jwt(token, expected_iss=*, expected_aud=*)
And the corresponding import in the module that will perform the check:
from _p2n_bootstrap cimport _runtime_verify_es256_jwt
That’s the entire amount of “Cython-looking” surface you touch. You are not rewriting your application in Cython — you’re declaring one symbol so the build system can link the compiled verifier into your binary. Py2Native handles the rest of the pipeline, including the Cython invocation and the platform-specific compile and link steps.
4. Call the verifier with the license and expected claims
In your application code, load the license string, call the verifier with the claims you require, and act on the result:
licenseString = open("license.dat").read().strip()
license = _runtime_verify_es256_jwt(licenseString, expected_iss="RSJ Software GmbH", expected_aud="TimestampGIT")
Passing expected_iss and expected_aud is what turns a generic signature check into an actual product check. A valid JWT signed by your key but issued for a different audience should not unlock this application.
5. Build with the license verification enabled
uv run py2native build main.py *.py --license license.dat --public public.pem
The --license flag turns the Pro license check on for the build, and --public tells it which public key to bake in. The result is a native binary whose verification path is compiled machine code.
If you want a portable output directory instead of a bare executable, add --embed — see How Py2Native’s --embed Builds a Portable Deployment Folder for what lands in that folder. For library-style distribution, --wheel produces the shared library and its import bridge; the details are in How to Ship a PEP 427 Wheel With Compiled Modules.
Interpreting Verification Results and Handling Edge Cases
A successful verification gives you the decoded claims back — that’s why the result is assigned to license in the snippet above. You can read custom claims from it and branch on entitlements, seats, or feature flags.
A failed verification is your code’s decision to make: you can reject by raising an exception or by treating the return value as a failure indicator, depending on how you call it. What matters is that you handle it explicitly.
The failure modes you’ll actually see:
- Expired token. The
expclaim is in the past. - Wrong issuer or audience. The token is real, but it wasn’t minted for this product or this customer relationship.
- Tampered signature. The payload was edited after signing.
- Missing or mismatched public key. The build embedded a key that doesn’t correspond to the private key that signed the license.
- Malformed token. Truncated file, wrong encoding, stray whitespace from an email client.
A typical graceful failure looks like this:
import sys
try:
license = _runtime_verify_es256_jwt(
licenseString,
expected_iss="RSJ Software GmbH",
expected_aud="TimestampGIT",
)
except Exception:
print("License verification failed. Please contact support with your license ID.", file=sys.stderr)
sys.exit(1)
entitlements = license.get("entitlements", [])
if "pro" not in entitlements:
print("This build requires a Pro license.", file=sys.stderr)
sys.exit(1)
Two practical notes on hardening:
- Fail closed. If the license file is unreadable, that is not a reason to continue in degraded mode unless you deliberately designed it that way.
- Keep the message vague. “License verification failed” is enough for the end user. Logging the exact claim mismatch to stderr just tells an attacker which knob to turn next.
And the structural point: because the verification logic is compiled into the binary, an attacker cannot disable the gate by editing a Python source file. They’d have to patch machine code.
Testing and Debugging License Verification
When a customer reports that a license “doesn’t work,” the fastest way to find out why is to inspect the token directly:
uv run py2native show --public public.pem license.dat
This prints the JWT claims and, with --public, verifies the signature against the key you think you shipped. If show passes but your app rejects it, the mismatch is almost always in the expected issuer, the expected audience, or the public key embedded in that particular build.
For development, test both directions:
- Build and run with a valid license and confirm the happy path annotates entitlements correctly.
- Build and run with a deliberately invalid license — wrong audience, expired, or a token signed by a different key — and confirm your error handling produces a clean message and a non-zero exit code.
For CI, generate a throwaway keypair and license inside the pipeline, then build against them:
uv run py2native keygen ci-private.pem ci-public.pem
uv run py2native sign --private ci-private.pem ci-payload.json ci-license.dat
uv run py2native build main.py *.py --license ci-license.dat --public ci-public.pem
Never reuse your production private key in CI.
One reminder: this entire workflow requires the Pro plugin (py2nativepro). The Community edition is MIT-licensed and does full compilation, but it does not include key generation, signing, or embedded JWT verification.
Deploying Your Licensed Application
Because the public key and the verification code are part of the compiled artifact, there is nothing extra to install at runtime. No crypto library to vendor, no service to reach, no configuration file holding a key.
What you do need to ship is the license itself. Either:
- Ship
license.datalongside the executable — simplest for per-customer builds and easy to replace when a license is renewed, or - Embed the license string in the binary — appropriate when the license never changes for that build.
Offline verification makes this work in places where a licensing server simply cannot exist: air-gapped networks, isolated manufacturing cells, and customer environments where an outbound call from a licensed application would itself be a compliance finding.
If you want a single directory containing your compiled application and its license — something you can zip and hand over — add --embed to the build:
uv run py2native build main.py *.py --license license.dat --public public.pem --embed dist/
If you’re shipping third-party libraries alongside your compiled code, remember that those stay unmodified Python in most cases; What Happens to Third-Party Libraries When You Compile Python? covers exactly which ones are left as source and why.
FAQ
Can I use license verification with the Community edition?
No. License verification is a Pro feature. The Community edition (MIT) provides full compilation but does not include the JWT verification plugin. You need the Pro plugin (py2nativepro) to generate keys, sign licenses, and embed verification.
Do I need to write Cython code to verify licenses?
No. You write plain Python and use the provided _runtime_verify_es256_jwt function via a .pxd declaration. Py2Native’s build process handles the Cython compilation and linking automatically.
What happens if the license is invalid or expired? The verification function will raise an exception or return a failure indicator. You should handle this in your code by, for example, printing an error and exiting, or restricting features. The exact behavior depends on how you call the function.
Can I verify licenses without an internet connection? Yes. The verification is fully offline. The public key and verification logic are compiled into your executable, and the license is a self-contained JWT. No network access is required.
Conclusion
Compiling your Python gets your source code out of the customer’s hands. Checking a signed license key inside the application gets authorization out of the customer’s hands too — and with Py2Native’s Pro plugin, that check is an ES256 JWT verification compiled directly into your binary, backed by an EC P-256 keypair where only the public half ever ships.
The flow is short enough to fit on one screen: keygen, sign, declare the verifier, call _runtime_verify_es256_jwt with your expected issuer and audience, then build with --license and --public. No C, no manual cythonize, no third-party crypto dependency, no network dependency.
Ready to gate your build? Start with uv run py2native build and add the Pro plugin when you need enforcement — see py2native.dev for the current Pro offering and the full CLI reference.
Related posts
- Compile Python Without Writing C Extensions or .pyx Files
- What Happens to Third-Party Libraries When You Compile Python?
- How to Ship a PEP 427 Wheel With Compiled Modules