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.
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.
Made from the documentation in the sources of the library.
#Types
#Algorithm
enum AlgorithmAn algorithm that signs the tokens, the one of a Key.
Hs256: HMAC with SHA-256, with a secretHs384: HMAC with SHA-384, with a secretHs512: HMAC with SHA-512, with a secretRs256: RSA with SHA-256 (PKCS#1 v1.5), the default of many providers of identityRs384: RSA with SHA-384Rs512: RSA with SHA-512Es256: ECDSA on the curve P-256 with SHA-256Es384: ECDSA on the curve P-384 with SHA-384EdDsa: Ed25519
#Algorithm.name
string name() constIts name in the header of a token: "HS256", "EdDSA".
#Key
struct KeyA 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, whichsignwrites in the header of the token and aVerifierlooks for (RFC 7515 §4.1.4): "" unless the program sets it, or it comes fromkeySet.
#Reason
enum ReasonWhy 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 aVerifier, also a token without kid whose algorithm no key has.UnknownKey: no key of theVerifierhas the kid of the tokenBadSignature: the signature is not that of the token with the keyExpired: the time of exp has passedNotYetValid: the time of nbf has not comeWrongIssuer: iss is not the issuer of theVerifierWrongAudience: aud does not name the audience of theVerifier
#Invalid
struct Invalid : ExceptionThe exception of a token that is refused: reason says why, and message() explains it, as "the token has expired".
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
struct VerifierThe 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.
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
T verify<T : json.Data>(string token) constThe 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.
#Functions
#secret
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
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
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
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
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
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.