jwt.zig
jwt.zig

zigster64/jwt.zig

MIT

A JWT library for zig

0000
3
build.zig.zon build.zig fork 
View on Github  
Updated: 8:56:46 AM Sun Sep 01 2024Size: 37KBCreated: 3:14:17 AM Tue Aug 13 2024
Dependencies:
benchmark
zig fetch --save git+https://github.com/zigster64/jwt.zig

zig jwt

A JWT library for zig

This is a hard fork of github.com/softprops/zig-jwt

Differences are :

  • Allows you to decode a JWT that you dont have the secret for
  • Fixes breaking changes from the latest 0.14 type name changes

📼 installing

Create a new exec project with zig init. Copy an example from the examples directory into your into src/main.zig

Create a build.zig.zon file to declare a dependency

.zon short for "zig object notation" files are essentially zig structs. build.zig.zon is zigs native package manager convention for where to declare dependencies

Starting in zig 0.12.0, you can use and should prefer

zig fetch --save https://github.com/zigster64/jwt.zig/archive/refs/tags/v1.0.0.tar.gz

otherwise, to manually add it, do so as follows

.{
    .name = "my-app",
    .version = "0.1.0",
    .dependencies = .{
+       // 👇 declare dep properties
+        .jwt = .{
+            // 👇 uri to download
+            .url = "https://github.com/zigster64/jwt.zig/archive/refs/tags/v1.0.0.tar.gz",
+            // 👇 hash verification
+            .hash = "...",
+        },
    },
}

the hash below may vary. you can also depend any tag with https://github.com/zigster64/jwt.zig/archive/refs/tags/v{version}.tar.gz or current main with https://github.com/zigster64/jwt.zig/archive/refs/heads/main/main.tar.gz. to resolve a hash omit it and let zig tell you the expected value.

Add the following in your build.zig file

const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});

    const optimize = b.standardOptimizeOption(.{});
    // 👇 de-reference dep from build.zig.zon
+    const jwt = b.dependency("jwt", .{
+        .target = target,
+        .optimize = optimize,
+    }).module("jwt");
    var exe = b.addExecutable(.{
        .name = "your-exe",
        .root_source_file = .{ .path = "src/main.zig" },
        .target = target,
        .optimize = optimize,
    });
    // 👇 add the module to executable
+    exe.root_mode.addImport("jwt", jwt);

    b.installArtifact(exe);
}

examples

See examples directory

🥹 for budding ziglings

Does this look interesting but you're new to zig and feel left out? No problem, zig is young so most us of our new are as well. Here are some resources to help get you up to speed on zig

- softprops 2024