Sample code for 30+ languages & platforms
Zig

Validate the at_hash Claim of an ID Token

See more JSON Web Token (JWT) Examples

Demonstrates how to hash an access token to compare it with the at_hash claim of an ID token.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // This example uses a Google access_token + id_token that looks like this:

    //  {
    //   "access_token": "ya29.a0...0f",
    //   "expires_in": 3599,
    //   "scope": "openid https://www.googleapis.com/auth/userinfo.email",
    //   "token_type": "Bearer",
    //   "id_token": "eyJhb...o5nQ"
    // }

    const json_token = try chilkat.JsonObject.init();
    defer json_token.deinit();
    json_token.loadFile("qa_data/tokens/google_sample_id_token.json") catch {
        std.debug.print("Failed to load the JSON file...\n", .{});
        return;
    };

    // Use Chilkat's JWT API to examine the id_token..
    const jwt = try chilkat.Jwt.init();
    defer jwt.deinit();
    const id_token = try json_token.stringOf(alloc, "id_token");

    // Extract the JOSE header..
    const jose = try jwt.getHeader(alloc, id_token);

    const json_header = try chilkat.JsonObject.init();
    defer json_header.deinit();
    json_header.load(jose) catch {};
    json_header.setEmitCompact(false);
    std.debug.print("{s}\n", .{try json_header.emit(alloc)});

    // The JOSE header looks like this:

    // {
    //   "alg": "RS256",
    //   "kid": "e8799db06287515556213c80acbcfd022fb302a9",
    //   "typ": "JWT"
    // }

    const claims = try jwt.getPayload(alloc, id_token);

    const json_claims = try chilkat.JsonObject.init();
    defer json_claims.deinit();
    json_claims.load(claims) catch {};
    json_claims.setEmitCompact(false);
    std.debug.print("{s}\n", .{try json_claims.emit(alloc)});

    // The claims look like this:

    // {
    //   "iss": "https://accounts.google.com",
    //   "azp": "258999997753-5ni8lu5f15r7mno97d82f5lir9i9f6i1.apps.googleusercontent.com",
    //   "aud": "258999997753-5ni8lu5f15r7mno97d82f5lir9i9f6i1.apps.googleusercontent.com",
    //   "sub": "111787341816486547572",
    //   "email": "somebody@gmail.com",
    //   "email_verified": true,
    //   "at_hash": "HYJZImlW3mUK-UfjRfXjKw",
    //   "iat": 1615315968,
    //   "exp": 1615319568
    // }

    // The at_hash is the Access Token hash value. Its value is the base64url encoding of the
    // left-most half of the hash of the octets of the ASCII representation of the access_token value,
    // where the hash algorithm used is the hash algorithm used in the alg Header Parameter of the
    // ID Token's JOSE Header. For instance, if the alg is RS256, hash the access_token value with SHA-256,
    // then take the left-most 128 bits and base64url encode them. The at_hash value is a case sensitive string.

    const token_to_hash = try json_token.stringOf(alloc, "access_token");
    const token_hash_expected = try json_claims.stringOf(alloc, "at_hash");

    // Step 1. hashes the access token using SHA-256 (Google uses `RS256` as the ID Token `alg`).
    const crypt = try chilkat.Crypt2.init();
    defer crypt.deinit();
    const bd_hash = try chilkat.BinData.init();
    defer bd_hash.deinit();

    crypt.setHashAlgorithm("sha256");
    // This encoding mode must match the encoding mode passed in the 2nd arg to AppendEncoded.
    // The encoding mode can be anything, as long as they are the same in both places.
    crypt.setEncodingMode("hex");

    try bd_hash.appendEncoded(try crypt.hashStringENC(alloc, token_to_hash), "hex");
    const sz = bd_hash.getNumBytes();

    const token_hash_computed = try bd_hash.getEncodedChunk(alloc, 0, @divTrunc(sz, 2), "base64url");

    // If the hashes are identical, then the access_token as issued for the given id_token.
    std.debug.print("token_hash_expected: {s}\n", .{token_hash_expected});
    std.debug.print("token_hash_computed: {s}\n", .{token_hash_computed});
}