cromo La guida di Cromo
Scarica PDF EN

std.jwt

JSON Web Tokens (RFC 7519) in the compact form of JWS (RFC 7515), signed with HMAC, RSA, ECDSA or Ed25519 (RFC 7518, RFC 8037): a service gives one to a client that logs in, or a provider of identity gives it, and the client shows it at each request. sign makes a token of the claims, a struct of the program written as JSON, and verify checks one and gives the claims back, or throws Invalid with the reason. The algorithm is always the one of the Key, never the one written in the token, so a token with "alg": "none" is refused.

cromo
struct Session {
    string sub
    i64 exp
}

jwt.Key key = jwt.secret(jwt.Algorithm.Hs256, "a secret of at least 32 bytes!!!".bytes())
Session session
session.sub = "anna"
session.exp = time.unix() + 3600  // in an hour
string token = jwt.sign(session, key)
Session back = jwt.verify<Session>(token, key)

A Verifier checks the tokens with several keys, as those that a provider of identity publishes, read with keySet, and checks the issuer and the audience too.

The guide: Networking and HTTP.

Generata dalla documentazione nei sorgenti della libreria, che è in inglese.

#Tipi

#Algorithm

cromo
enum Algorithm

An algorithm that signs the tokens, the one of a Key.

  • Hs256: HMAC with SHA-256, with a secret
  • Hs384: HMAC with SHA-384, with a secret
  • Hs512: HMAC with SHA-512, with a secret
  • Rs256: RSA with SHA-256 (PKCS#1 v1.5), the default of many providers of identity
  • Rs384: RSA with SHA-384
  • Rs512: RSA with SHA-512
  • Es256: ECDSA on the curve P-256 with SHA-256
  • Es384: ECDSA on the curve P-384 with SHA-384
  • EdDsa: Ed25519

#Algorithm.name

cromo
string name() const

Its name in the header of a token: "HS256", "EdDSA".

#Key

cromo
struct Key

A key to sign or to verify the tokens of one algorithm: a secret of HMAC from secret, a key in PEM from publicKey or privateKey, or a key of a JWK Set from keySet. A secret or a private key signs and verifies, a public key only verifies. The text of a key is read once, when the key is made, and its secret is wiped from memory when it ends. It can be moved, not copied.

  • string id: Its kid, which sign writes in the header of the token and a Verifier looks for (RFC 7515 §4.1.4): "" unless the program sets it, or it comes from keySet.

#Reason

cromo
enum Reason

Why a token is refused: the reason of Invalid.

  • Malformed: Not three parts of base64url, a header or claims that are not objects of JSON, a header with crit, or claims that do not fit the struct asked for.
  • WrongAlgorithm: Signed with another algorithm than the one of its key, as none; for a Verifier, also a token without kid whose algorithm no key has.
  • UnknownKey: no key of the Verifier has the kid of the token
  • BadSignature: the signature is not that of the token with the key
  • Expired: the time of exp has passed
  • NotYetValid: the time of nbf has not come
  • WrongIssuer: iss is not the issuer of the Verifier
  • WrongAudience: aud does not name the audience of the Verifier

#Invalid

cromo
struct Invalid : Exception

The exception of a token that is refused: reason says why, and message() explains it, as "the token has expired".

cromo
try {
    Claims claims = jwt.verify<Claims>(token, key)
} catch (jwt.Invalid e) {
    if (e.reason == jwt.Reason.Expired) {
        println("log in again")
    }
}
  • Reason reason: why the token is refused

#Verifier

cromo
struct Verifier

The checks of the tokens of a service, with one or more keys: the issuer and the audience that the tokens must name, when they are set, and a tolerance on their times. Its verify is const, so the tasks of a server can share one verifier.

cromo
jwt.Verifier verifier
verifier.keys = jwt.keySet(http.get("https://id.example.com/jwks.json").text())
verifier.issuer = "https://id.example.com"
verifier.audience = "my-api"
verifier.leeway = 30
Claims claims = verifier.verify<Claims>(token)
  • list<Key> keys: The keys: a token takes the one with its kid, or, without a kid, the first of its algorithm.
  • string issuer: The iss that the tokens must have; "" does not check it.
  • string audience: The audience that the aud of the tokens must name, alone or in an array; "" does not check it.
  • i64 leeway: The seconds of tolerance on exp and nbf, for the clocks that differ; 0 by default.

#Verifier.verify

cromo
T verify<T : json.Data>(string token) const

The claims of the token as the struct T, when it passes the checks: its key, its algorithm, its signature, exp and nbf when it has them, with the tolerance of leeway, and the issuer and the audience when they are set. Otherwise jwt.Invalid, with its reason.

#Funzioni

#secret

cromo
Key secret(Algorithm algorithm, list<u8> bytes)

A key of HMAC for HS256, HS384 or HS512, which signs and verifies, with a copy of a secret at least as long as the hash: 32 bytes for HS256, 48 for HS384, 64 for HS512 (RFC 7518 §3.2); crypto.random makes a good one. An exception for another algorithm, or a shorter secret.

#publicKey

cromo
Key publicKey(Algorithm algorithm, string pem)

A key of the algorithm that verifies, from a public key in PEM: of RSA or of a curve as SubjectPublicKeyInfo ("BEGIN PUBLIC KEY") or PKCS#1, of Ed25519 as SubjectPublicKeyInfo (RFC 8410). An exception for an algorithm of HMAC, a text that is not such a key, a key of another algorithm, as one of RSA for ES256, and a key of RSA of fewer than 2048 bits.

#privateKey

cromo
Key privateKey(Algorithm algorithm, string pem)

A key of the algorithm that signs and verifies, from a private key in PEM: PKCS#8 ("BEGIN PRIVATE KEY"), or PKCS#1 for RSA and SEC 1 for a curve; of Ed25519 as PKCS#8 (RFC 8410). An exception as for publicKey.

#keySet

cromo
list<Key> keySet(string text)

The keys of a JWK Set (RFC 7517), the JSON that a provider of identity publishes, which verify: RSA from n and e, P-256 and P-384 from x and y, Ed25519 from x, each with its kid and its algorithm. The keys for encryption, of another kind, weak or that cannot be read are left out, so the list can be empty. An exception if the text is not JSON, or has no member "keys". Downloading the set, as with http.Client, and reading it again when the provider changes its keys is up to the program.

#sign

cromo
string sign<T : json.Data>(T claims, Key key)

The token of the claims, written as JSON as json.encode does and signed with a secret or a private key; its header has the algorithm of the key, and its kid when it has one. The claims are written as they are: the times, as exp, are fields of the struct that the program sets, in seconds from 1970, as time.unix() + 3600. An exception with a public key, which cannot sign.

#verify

cromo
T verify<T : json.Data>(string token, Key key)

The claims of a token signed with the key, as the struct T: it checks the algorithm, the signature, and exp and nbf when the token has them, without tolerance; not the issuer nor the audience, which a Verifier checks. Otherwise jwt.Invalid, with its reason.