mooncred — credential formats for MoonBit: JSON Web Tokens, JWS, JWK and the JOSE algorithm set, built on mooncrypt's algorithms and moonbase's encodings.
Dependencies
// The key is whatever signs. Here, HMAC over SHA-256.
let key = @jwt.mac(fn() {
@hmac.Hmac::new(secret[:], fn() { @sha2.Hasher::new() })
})
let token = @jwt.sign(claims, HS256, key)
// The algorithm is the caller's, not the token's.
let got = @jwt.verify(token[:], HS256, key, now=now_seconds,
issuer=Some("moonbitstack"), audience=Some("moonapi"))| Package | What | Specification |
|---|---|---|
| jwt | JWS compact serialisation, all thirteen JOSE algorithms | RFC 7515, 7518, 7519, 8037 |
| jwk | JSON Web Key and key sets: reading, writing, choosing | RFC 7517, RFC 7518 §6 |
| x509 | X.509 v3 certificates: build a self-signed one, read one down to its extensions, check its dates, its names and its signature | RFC 5280, RFC 6125 |
@jwt.policy < the policy you pass < the arguments you pass// A policy for the deployment, assembled once.
let ours = @jwt.Policy::new(issuer=Some("moonbitstack"), audience=["moonapi"])
// The same thing written as a record update.
let ours : @jwt.Policy = { ..@jwt.policy, issuer: Some("moonbitstack") }
// Used as it is, and used with one thing changed for this call.
@jwt.verify(token[:], HS256, key, now=At(seconds), policy=ours)
@jwt.verify(token[:], HS256, key, now=At(seconds), policy=ours, leeway=60L)
// Reading a token without asking whether it has expired.
@jwt.verify(token[:], HS256, key, now=Ignored)match key.material() {
Okp(x~, ..) => @ed25519.PublicKey::new(x[:])
_ => …
}@jwt.Policy::new(issuer="https://issuer.example", leeway=30)
@jwt.verify(token[:], HS256, key, now=At(seconds), issuer="https://issuer.example")@jwt.sign(claims, HS256, key) // typ: "JWT"
@jwt.sign(claims, HS256, key, typ=Some("at+jwt")) // typ: "at+jwt"
@jwt.sign(claims, HS256, key, typ=None) // no typ header// Build one a TLS server can present. Self-signed, P-256, ES256.
let der = @x509.self_signed(key, serial, "example.test", "260101000000Z", "270101000000Z")
// Read one a peer sent.
let cert = @x509.parse(der[:])
cert.subject.cn() // Some("example.test")
cert.valid_at(now) // both ends inclusive, as RFC 5280 gives them
cert.matches("a.example.test") // RFC 6125 §6.4.3, wildcards included
cert.signed_by(issuer_key) // raises rather than answering false for an
// algorithm it did not checkmoon add moonbitstack/mooncredInstall
Download zipmooncred — credential formats for MoonBit: JSON Web Tokens, JWS, JWK and the JOSE algorithm set, built on mooncrypt's algorithms and moonbase's encodings.
Dependencies