Julia JWT generator quickstart
Create a token with your public API key in the sub claim, a near-term Unix timestamp in exp, and an HS256 signature made with your private API key.
sub claim containing your public API key and an exp claim containing a Unix timestamp. The examples use a one-hour lifetime; shorten it further when your application can refresh tokens easily.
This example uses Julia's Base64 and SHA standard libraries plus JSON3.jl.
using Base64
using JSON3
using SHA
function base64url(data)
replace(
replace(rstrip(base64encode(data), '='), '+' => '-'),
'/' => '_',
)
end
public_key = get(ENV, "SAPLING_PUBLIC_KEY", "")
private_key = get(ENV, "SAPLING_PRIVATE_KEY", "")
if isempty(public_key) || isempty(private_key)
error("Set SAPLING_PUBLIC_KEY and SAPLING_PRIVATE_KEY first.")
end
expires_at = floor(Int, time()) + 3600
header = base64url(JSON3.write(Dict("alg" => "HS256", "typ" => "JWT")))
payload = base64url(JSON3.write(Dict("sub" => public_key, "exp" => expires_at)))
signing_input = "$header.$payload"
signature = base64url(hmac_sha256(
codeunits(private_key),
codeunits(signing_input),
))
println("$signing_input.$signature")
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.<base64url-claims>.<signature>
Return the compact token to the browser and pass it to Sapling.init as the key. Issue a fresh token after it expires; never send the private key.
How a JWT is structured
A JSON Web Token (JWT) is a compact, URL-safe way to pass signed claims between two parties, most often for authentication. It has three base64url-encoded parts separated by dots: header.payload.signature.
- Header
- The token type (
JWT) and the signing algorithm, such as HS256 (HMAC with SHA-256) or RS256 (RSA). - Payload
- The claims: statements about the subject plus metadata such as an expiry. Claims are registered (standard names below), public, or private names agreed between the two parties.
- Signature
- The encoded header and payload signed with a secret (or private key) using the header's algorithm. It proves who issued the token and that it hasn't been changed in transit.
Standard JWT claims
Sapling checks only sub and exp, but these are the registered claims you will see in Julia JWT libraries:
iss(issuer): who issued the token.sub(subject): who the token is about. For Sapling, your public API key.aud(audience): the recipients the token is intended for.exp(expiration time): seconds since the Unix epoch after which the token must be rejected.nbf(not before): seconds since the epoch before which the token must be rejected.iat(issued at): when the token was issued, in seconds since the epoch.jti(JWT ID): a unique identifier that lets a server reject replayed tokens.
Use a vetted JWT library
Sign tokens with an established, maintained cryptography library rather than hand-rolling HMAC and base64url encoding. Those libraries are peer-reviewed, handle edge cases such as algorithm confusion and clock skew, and get patched when new vulnerabilities appear.
About Julia
Julia is a popular programming language for data science applications in industry and academia like numerical analysis and computational science. It is designed to be dynamic, concurrent/parallel and includes efficient libraries for linear algebra and floating-point calculations.