
kit-jwt v2026.7.12
JSON Web Token (JWT) encoding and decoding for Kit
kit-jwt
JSON Web Token (JWT) encoding and decoding for Kit
[TOC]
Files
| 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 |
tests/rs256.kit.disabled | Optional RS256 behavior tests |
tests/rs384.kit.disabled | Optional RS384 behavior tests |
tests/rs512.kit.disabled | Optional RS512 behavior tests |
Dependencies
- Kit package dependency:
crypto - Kit standard module:
Encoding.Base64 - Native library: OpenSSL
libcryptofor RS256, RS384, and RS512 helpers - Capability required:
ffi
The package is declared as ffi-zig because RSA signing and verification use zig/jwt_rsa.zig. HS256 uses crypto.hmac-sha256.
Installation
kit add gitlab.com/kit-lang/packages/kit-jwt.git
Usage
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:
| 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 |
Unsafe inspection helpers are also available for debugging tokens without verifying signatures:
decode-unsafeget-claims-unsafeget-header-unsafe
Do not use unsafe helpers for authorization decisions.
Development
Running Examples
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
Running Tests
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
Running kit dev
Run the standard development workflow (format, check, test):
kit dev
This will:
- Check formatting for Kit source and example files
- Type check source and examples
- Run active tests with coverage
Running Parity
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.
Generating Documentation
Generate API documentation from doc comments:
kit doc src/jwt.kit
Note: Kit sources with doc comments (##) generate HTML documentation.
Cleaning Build Artifacts
Remove generated files, caches, and build artifacts:
kit task clean
Note: Defined in kit.toml.
Local Installation
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.
Security Notes
- Use strong, random HS256 secrets. A minimum of 256 bits is recommended.
- Never commit private keys or production JWT secrets.
- Validate application claims such as
exp,nbf,iat,iss, andaudafter decoding. - Prefer short-lived tokens and rotate keys according to your application's threat model.
- Use HTTPS whenever transmitting JWTs over a network.
License
This package is released under the MIT License - see LICENSE for details.