std::crypto::jwtJSON Web Token encoding and validation.
Encode, decode, and validate JWTs using HMAC algorithms.
import std.crypto.jwt;
fn main() {
match jwt.encode("{\"sub\":\"1234\"}", "secret", "HS256") {
.Ok(token) => println(token),
.Err(err) => println(err),
}
}
last_errorReturn the last JWT error observed on this thread.
encodeEncode a JSON payload into a signed JWT string.
Returns Err(JwtError) when the payload, key, algorithm, or allocation
path fails.
decodeDecode and verify a JWT, returning its payload as a JSON string.
Returns Err(JwtError) when the token is malformed, expired, signed with a
different key, or cannot be allocated.
decode_insecureDecode a JWT without verifying its signature.
Warning: This does not validate the token. Only use for inspecting untrusted tokens when you don't need authenticity.
import std.crypto.jwt;
fn main() {
let token = jwt.encode("{\"sub\":\"1234\"}", "secret", "HS256") handle error {
println(f"operation failed: {error}");
return;
};
let payload = jwt.decode_insecure(token);
println(payload);
}
validateValidate a JWT signature and expiration.
Returns true if the token is valid, false otherwise.
import std.crypto.jwt;
fn main() {
let token = jwt.encode("{\"sub\":\"1234\"}", "my_secret", "HS256") handle error {
println(f"operation failed: {error}");
return;
};
if jwt.validate(token, "my_secret", "HS256") {
println("valid");
}
}
JwtErrorStructured JWT errors surfaced by the native C ABI.
NoneAlgorithmUnsupportedInvalidKeyTokenMalformedSignatureInvalidClaimsExpiredAllocationFailure