
JSON Web Token (JWT) encoding and decoding for Kit
JSON Web Token (JWT) encoding and decoding for Kit
[TOC]
| File | Description |
|---|---|
.editorconfig | Editor formatting configuration |
.gitignore | Git ignore rules for build artifacts and dependencies |
.tool-versions | asdf tool versions (Zig, Kit) |
LICENSE | MIT license file |
README.md | This file |
examples/basic.kit | Basic HS256 usage example |
kit.toml | Package manifest with metadata, capabilities, and dependencies |
src/jwt.kit | JWT encoding, decoding, verification, and helper API |
zig/jwt_rsa.zig | OpenSSL-backed RSA signing and verification bridge |
zig/kit_ffi.zig | Kit Zig FFI value helpers used by the RSA bridge |
tests/hs256.test.kit | Active HS256 behavior tests |
tests/types.test.kit | Error, type, algorithm, and claim-shape tests |
cryptoEncoding.Base64libcrypto for RS256, RS384, and RS512 helpersffiThe package is declared as ffi-zig because RSA signing and verification use zig/jwt_rsa.zig. HS256 uses crypto.hmac-sha256.
kit add gitlab.com/kit-lang/packages/kit-jwt.git
import Kit.Jwt as JWT
main = fn =>
secret = "my-super-secret-key-at-least-32-chars"
claims = "{\"sub\":\"user123\",\"name\":\"John Doe\",\"admin\":true,\"iat\":1700000000}"
match JWT.encode claims secret
| Err e ->
println "Failed to create token:"
println e
| Ok token ->
println "JWT:"
println token
if JWT.verify? token secret then
println "Token is valid"
else
println "Token is invalid"
match JWT.decode token secret
| Ok decoded ->
println "Header:"
println decoded.header
println "Payload:"
println decoded.payload
| Err e ->
println "Decode failed:"
println e
main
Supported signing and verification helpers:
Unsafe inspection helpers are also available for debugging tokens without verifying signatures:
decode-unsafeget-claims-unsafeget-header-unsafeDo not use unsafe helpers for authorization decisions.
Run the basic example with the interpreter:
kit run examples/basic.kit --allow=ffi
Compile the example to a native binary:
kit build examples/basic.kit --allow=ffi && ./basic
Run the active test suite:
kit test --allow=ffi
Run the active test suite with coverage:
kit test --coverage --allow=ffi
Run the optional RSA test files directly:
kit test tests/rs256.kit.disabled --allow=ffi
kit test tests/rs384.kit.disabled --allow=ffi
kit test tests/rs512.kit.disabled --allow=ffi
Run the standard development workflow (format, check, test):
kit dev
This will:
Run interpreter/compiler parity checks for examples:
kit parity --failures-only
Parity checks that examples run through the interpreter, compile successfully, execute successfully, and produce matching output.
Generate API documentation from doc comments:
kit doc src/jwt.kit
Note: Kit sources with doc comments (##) generate HTML documentation.
Remove generated files, caches, and build artifacts:
kit task clean
Note: Defined in kit.toml.
To install this package locally for development:
kit install
This installs the package to ~/.kit/packages/@kit/jwt/, making it available for import as Kit.Jwt in other projects.
exp, nbf, iat, iss, and aud after decoding.This package is released under the MIT License - see LICENSE for details.
tests/rs256.kit.disabled | Optional RS256 behavior tests |
tests/rs384.kit.disabled | Optional RS384 behavior tests |
tests/rs512.kit.disabled | Optional RS512 behavior tests |
| Algorithm | Helpers |
|---|
| HS256 | encode, encode-with-header, decode, verify? |
| RS256 | encode-rs256, decode-rs256, verify-rs256?, get-claims-rs256 |
| RS384 | encode-rs384, decode-rs384, verify-rs384?, get-claims-rs384 |
| RS512 | encode-rs512, decode-rs512, verify-rs512?, get-claims-rs512 |