https://github.com/deatil/zig-jwt
A JWT library for zig.
https://github.com/deatil/zig-jwt
jwt zig zig-jwt
Last synced: 11 months ago
JSON representation
A JWT library for zig.
- Host: GitHub
- URL: https://github.com/deatil/zig-jwt
- Owner: deatil
- License: apache-2.0
- Created: 2025-02-20T11:10:53.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-02-20T11:13:56.000Z (11 months ago)
- Last Synced: 2025-02-20T12:24:13.639Z (11 months ago)
- Topics: jwt, zig, zig-jwt
- Language: Zig
- Homepage: https://github.com/deatil/zig-jwt
- Size: 12.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-zig - deatil/zig-jwt - A JWT(JSON Web Token) library for Zig. (Web Framework / Large Language Model)
- awesome-zig - zig-jwt🗒️A JWT library for zig
README
## Zig-jwt
A JWT library for zig.
### Env
- Zig >= 0.14.0-dev.2851+b074fb7dd
### Adding zig-jwt as a dependency
Add the dependency to your project:
```sh
zig fetch --save=zig-jwt git+https://github.com/deatil/zig-jwt#main
```
or use local path to add dependency at `build.zig.zon` file
```zig
.{
.dependencies = .{
.@"zig-jwt" = .{
.path = "./lib/zig-jwt",
},
...
}
}
```
And the following to your `build.zig` file:
```zig
const zig_jwt_dep = b.dependency("zig-jwt", .{});
exe.root_module.addImport("zig-jwt", zig_jwt_dep.module("zig-jwt"));
```
The `zig-jwt` structure can be imported in your application with:
```zig
const zig_jwt = @import("zig-jwt");
```
### Get Starting
~~~zig
const std = @import("std");
const jwt = @import("zig-jwt");
pub fn main() !void {
const alloc = std.heap.page_allocator;
const kp = jwt.eddsa.Ed25519.KeyPair.generate();
const claims = .{
.aud = "example.com",
.iat = "foo",
};
const s = jwt.SigningMethodEdDSA.init(alloc);
const token_string = try s.make(claims, kp.secret_key);
// output:
// make jwt: eyJ0eXAiOiJKV1QiLCJhbGciOiJFZERTQSJ9.eyJhdWQiOiJleGFtcGxlLmNvbSIsImlhdCI6ImZvbyJ9.zXaymzzL0dtQZdK7DS32nqES2qoAvzFGPtcQFRvIC0k4XfRybivp1MpCjwJrI-7SIQ8zMV5wK_zIdEHS9A8tDg
std.debug.print("make jwt: {s} \n", .{token_string});
const p = jwt.SigningMethodEdDSA.init(alloc);
var parsed = try p.parse(token_string, kp.public_key);
// output:
// claims aud: example.com
const claims2 = try parsed.getClaims();
std.debug.print("claims aud: {s} \n", .{claims2.object.get("aud").?.string});
}
~~~
### Signing Methods
The JWT library have signing methods:
- `ES256`: jwt.SigningMethodES256
- `ES384`: jwt.SigningMethodES384
- `EdDSA`: jwt.SigningMethodEdDSA
- `ED25519`: jwt.SigningMethodED25519
- `HS256`: jwt.SigningMethodHS256
- `HS384`: jwt.SigningMethodHS384
- `HS512`: jwt.SigningMethodHS512
- `none`: jwt.SigningMethodNone
### LICENSE
* The library LICENSE is `Apache2`, using the library need keep the LICENSE.
### Copyright
* Copyright deatil(https://github.com/deatil).