djwt

repository·master·Indexed 42 days ago

https://github.com/zaubrik/djwt

A library for creating and verifying JSON Web Tokens (JWT) using the native Web Crypto API. It provides functions to generate URL-safe encoded JWT strings via create() and validate signatures and claims via verify().

Tokens
432
Snippets
3
Records
3
Agent score
4%

What's inside djwt

  1. Generate a secure CryptoKey for djwt

    master

    Before using djwt, you must generate a secure CryptoKey using the native Web Crypto API. This key is required for both create and verify operations.

    Example for HMAC SHA-512:

    const key = await crypto.subtle.generateKey(
      { name: "HMAC", hash: "SHA-512" },
      true,
      ["sign", "verify"],
    );
  2. Create a JWT with create()

    master

    The create function takes a Header, a Payload, and a CryptoKey to return a URL-safe encoded JWT string.

    Parameters:

    • header: An object defining the algorithm (alg) and type (typ).
    • payload: The data object to be encoded.
    • key: A CryptoKey generated via the Web Crypto API.
    import { create } from "https://deno.land/x/djwt@$VERSION/mod.ts";
    
    const jwt = await create({ alg: "HS512", typ: "JWT" }, { foo: "bar" }, key);
  3. Verify a JWT with verify()

    master

    The verify function validates a JWT's signature and returns its Payload if valid. If the signature is invalid or claims (like exp or aud) fail validation, it throws an Error.

    Parameters:

    • jwt: The encoded JWT string.
    • key: The CryptoKey used for verification.
    • options (optional): VerifyOptions to enforce specific claims like audience.
    ```typescript
    import { verify } from "https://deno.land/x/djwt@$VERSION/mod.ts";
    
    const payload = await verify(jwt, key); // { foo: