https://github.com/openpeeps/webtokens
Nim Bindings for LibJWT - JSON Web Token Library +JWK +JWKS
https://github.com/openpeeps/webtokens
json-web-token jwe jws jwt jwt-token libjwt nim nim-lang webtoken
Last synced: 1 day ago
JSON representation
Nim Bindings for LibJWT - JSON Web Token Library +JWK +JWKS
- Host: GitHub
- URL: https://github.com/openpeeps/webtokens
- Owner: openpeeps
- Created: 2025-11-26T16:37:53.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2025-11-27T10:46:55.000Z (7 months ago)
- Last Synced: 2025-12-15T02:33:57.022Z (6 months ago)
- Topics: json-web-token, jwe, jws, jwt, jwt-token, libjwt, nim, nim-lang, webtoken
- Language: Nim
- Homepage: https://openpeeps.github.io/webtokens/
- Size: 117 KB
- Stars: 3
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README

👑 Nim lang bindings for LibJWT
JSON Web Token Library
nimble install webtokens
## About
This package provides Nim bindings for [LibJWT](https://github.com/benmcollins/libjwt), a C library that supports the following standards JWS, JWE, JWK, JWA and JWTs. It allows you to create, sign, verify, and decode JSON Web Tokens in your Nim applications.
> [!NOTE]
> 👌 **Low-level** and **high-level APIs** are provided to work with JWTs, JWKs, and JWS/JWE tokens.
### Prerequisites
Check the official [LibJWT installation guide](https://github.com/benmcollins/libjwt?tab=readme-ov-file#construction-build-prerequisites)
## Examples
Here is a simple example demonstrating how to create and sign a JSON Web Token (JWT) using HMAC SHA-256 algorithm with the `webtokens` Nim package.
### Create and Sign a Json Web Token
```nim
{.passL:"-L/opt/local/lib -ljwt", passC:"-I /opt/local/include".}
import std/[times, json]
import webtokens
# Generate a JWK Set with one random HMAC (oct) key
let jwksJson = generateJwkSet(1, 32)
let set = loadJwkSetFromJson($jwksJson)
let key = set.findByKid("hmac-key-0")
let alg = parseAlg("HS256")
# Initialize JWT Builder
var builder = newJwtBuilder()
builder.setKey(alg, key)
builder.setHeader("typ", "JWT")
builder.setHeader("kid", key.itemKid())
builder.setIss("example-app")
builder.setSub("user123")
builder.setExpIn(3600) # token expires in 1 hour
# Generate the token
let token = builder.generate()
echo "Generated JWT: ", token
# Now, you can use this token for authentication/authorization
var verifier = newJwtChecker()
verifier.setKey(alg, key)
verifier.setLeeway(JWT_CLAIM_EXP, 5) # small clock skew
if verifier.verify(token):
echo "Verified OK!"
```
_todo more examples_
### ❤ Contributions & Support
- 🐛 Found a bug? [Create a new Issue](https://github.com/openpeeps/webtokens/issues)
- 👋 Wanna help? [Fork it!](https://github.com/openpeeps/webtokens/fork)
- 😎 [Get €20 in cloud credits from Hetzner](https://hetzner.cloud/?ref=Hm0mYGM9NxZ4)
### 🎩 License
Made by [Humans from OpenPeeps](https://github.com/openpeeps).
Copyright OpenPeeps & Contributors — All rights reserved.