Skip to main content

keystone-core

Signed capability tokens and short-code primitives for Keystone — the Python port of the v1 wire-format spec.

A capability token is a short, bearer, cryptographically signed envelope authorizing a specific short link under a specific scope, with optional expiry and use-count. The Keystone origin (Go) mints them; any verifier in any language can validate them against the public key — this package is the Python verifier/signer.

Install

pip install keystone-core

Requires Python 3.9+ and cryptography>=42 (for Ed25519).

Usage

from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
from keystone_core import Capability, verify_capability, random_code

priv = Ed25519PrivateKey.generate()
pub = priv.public_key()

# Mint
cap = Capability(code="aB3xZ9k", scope="read-only", signer="my-service",
                 exp=0, uses=5)   # exp=0 → no expiry; uses=5 → bearer may redeem 5 times
token = cap.sign(priv)
print(token)
# → "v1.<base64url body>.<base64url signature>"

# Verify (in another process, another language, however you got the token)
back = verify_capability(token, pub)
print(back.code, back.scope, back.uses)
# → "aB3xZ9k" "read-only" 5

# Generate a plain short code (no signing)
print(random_code())    # "aB3xZ9k"
print(random_code(10))  # "xY2fK9mNbA"

Verification raises specific exceptions on failure: MalformedError, BadVersionError, BadSignatureError, ExpiredError, EmptyCodeError, EmptyScopeError.

Why this exists

Every short-link redirect in Keystone can carry an optional ?k=<token> that gates the redirect behind a signed capability — "the holder of this token may follow code X, until Y, at most N times." The Go origin signs them with its private Ed25519 key. Any client, in any language, can verify them without round-tripping to the origin, given only the public key.

The wire format is small, deterministic, and language-independent:

token    = "v1." + body_b64 + "." + sig_b64
body     = canonical JSON of {code, exp, nonce, scope, signer, uses}
          (keys sorted, no whitespace)
body_b64 = base64url-no-pad(body)
sig_b64  = base64url-no-pad(Ed25519-sign("v1." + body_b64))

The authoritative spec is capability-spec.md.

Cross-language guarantee

This package is tested against vectors produced by the Go reference implementation. Every test run re-signs each vector with Python and asserts the result is byte-identical to the Go-signed token. If canonical JSON, base64 encoding, or the signing input construction ever drifts, the test fails loudly. See tests/test_cross_language.py.

Status

Alpha, v0.1.0. API may change until v1.0.0. If you build on this and hit a wart, open an issue.

License

Apache-2.0.

Metadata

Release files for keystone-core 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for keystone-core 0.1.0
File Size Uploaded
keystone_core-0.1.0.tar.gz 7.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for keystone-core 0.1.0
File Interpreter ABI Platform
keystone_core-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 14.8 kB

Release files / keystone_core-0.1.0.tar.gz

Download URL keystone_core-0.1.0.tar.gz
Size 7.8 kB
Tags Source
SHA-256 checksum
How to use checksums
80eebce941285b4f66347f1814e6c67767aa725579e1a0f5fb12c474a999e3ca
BLAKE2b-256 checksum
How to use checksums
c6d5fd192d1a1074853ad6a34d144cad2cac629400ab184e901913b932ae956a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Release files / keystone_core-0.1.0-py3-none-any.whl

Download URL keystone_core-0.1.0-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
77fd35462707dcc8090d0a146f2d8f7e5695927db136be435eafeeab960e8a6b
BLAKE2b-256 checksum
How to use checksums
6b64240cd57da5007cfe92c195b86c92808e452ce32ff623d9ca30ec2ea8399f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page