Native HS256 JWT Utility (gateway/jwt.utils.ts)

nitrostack · sample-apps/seizure-monitor/vitalis/src/gateway/jwt.utils.ts · typescript · Apache-2.0
auth, security, tag:crypto, tag:backend

Provides lightweight signing and verification of JWT tokens using Node.js native crypto. Use this for implementing secure authentication without external dependencies.

// Source: https://github.com/nitrocloudofficial/nitrostack | License: Apache-2.0 | Copyright You must retain, in the Source form of any Derivative Works
// Provided via justclaw.it.com; keep this notice with the code.
/**
 * Native HS256 JWT Utility — Signs and verifies JWT tokens using node:crypto.
 */
import crypto from 'node:crypto';
import { env } from '../config/env.js';

export interface JwtPayload {
  sub: string;
  scopes: string[];
  iat: number;
  exp: number;
  iss?: string;
  aud?: string | string[];
}

function base64UrlEncode(str: string | Buffer): string {
  return Buffer.from(str)
    .toString('base64')
    .replace(/=/g, '')
    .replace(/\+/g, '-')
    .replace(/\//g, '_');
}

function base64UrlDecode(str: string): string {
  let base64 = str.replace(/-/g, '+').replace(

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key