Skip to content

Other languages

The stamp is a standard JWT, so any language with a JWT library that supports EdDSA (Ed25519) can verify it.

Check the token as you would any signed JWT, with these values:

Check Value
Algorithm EdDSA (Ed25519). Reject anything else.
Keys https://api.terrastamp.dev/.well-known/jwks.json, matched by the header’s kid. Cache it, and refetch when a kid is unknown.
aud Your host, e.g. www.brand.com.
exp In the future.
iss https://api.terrastamp.dev (optional).

The ip_binding claim holds a keyed hash of each visitor address the stamp was issued to:

ip_binding.v4 or .v6 == base64url(HMAC-SHA256(key, subject))[0:22]
  • key: your binding key without its tsb_ prefix, base64url-decoded to bytes.
  • subject, for IPv4: the address in dotted form without leading zeros, e.g. 203.0.113.7.
  • subject, for IPv6: the /64 prefix as four lower-case hextets without leading zeros, followed by ::/64, e.g. 2001:db8:0:1::/64. An IPv4-mapped address (::ffff:203.0.113.7) counts as IPv4.

Compare against the entry for the visitor’s address family. If the stamp has no entry for that family, the binding can’t be checked; decide whether to accept it (the Node SDK does by default).

Compare in constant time.

import base64, hashlib, hmac, ipaddress
import jwt # PyJWT with cryptography
jwks = jwt.PyJWKClient("https://api.terrastamp.dev/.well-known/jwks.json")
def verify(token: str, host: str, binding_key: str, ip: str):
key = jwks.get_signing_key_from_jwt(token)
stamp = jwt.decode(token, key.key, algorithms=["EdDSA"], audience=host)
addr = ipaddress.ip_address(ip)
if addr.version == 6 and addr.ipv4_mapped:
addr = addr.ipv4_mapped
if addr.version == 4:
family, subject = "v4", str(addr)
else:
groups = addr.exploded.split(":")[:4]
family, subject = "v6", ":".join(format(int(g, 16), "x") for g in groups) + "::/64"
raw = base64.urlsafe_b64decode(binding_key.removeprefix("tsb_") + "==")
mac = hmac.new(raw, subject.encode(), hashlib.sha256).digest()
expected = base64.urlsafe_b64encode(mac).rstrip(b"=").decode()[:22]
bound = (stamp.get("ip_binding") or {}).get(family)
if bound is not None and not hmac.compare_digest(expected, bound):
raise ValueError("ip_mismatch")
return stamp