jwt

JSON Web Token HS256 encoder, decoder and verifier with pure-HolyPHP SHA-256.

v1.0.0Security & encodingbundledimport "jwt";
Overview Installation Documentation Examples

Overview

Zero-dependency JSON Web Token (JWT) HS256 library for HolyPHP, featuring pure-HolyPHP Base64URL, SHA-256, and HMAC-SHA256 implementations, constant-time signature comparison, and exp / nbf claim validation.

bundled — works offline zero dependencies auth token

Quick start

import "jwt";

$secret = "my-super-secret-key";

// Encode a token with an expiration claim
$token = jwt_encode([
    "sub" => "user-123",
    "role" => "admin",
    "exp" => time() + 3600
], $secret);

println("Token: " . $token);

// Verify and decode
$payload = jwt_decode($token, $secret);
println("Subject: " . $payload["sub"]);

// Boolean verification
if (jwt_verify($token, $secret)) {
    println("Token is valid!");
}

Installation

In a project

Inside a project (a folder with hphp.yaml, see Projects & packages):

hphp install jwt

This adds the library to hphp.yaml, installs it into .hphp/packages/jwt/ and pins the exact version in hphp.lock:

require:
  jwt: "^1.0"

Use it

import "jwt";

jwt is bundled with the compiler, so import "jwt"; also works in a single file without installing anything, and hphp install works offline.

Outside a project

hphp install jwt          # for your user, shared by all scripts
hphp create myapp                 # or start a project, then: hphp install jwt
Packagejwt
Version1.0.0 (follows the compiler)
Entry filejwt.hphp
Sourcepackages/lib/jwt ↗

Documentation

API Reference

jwt_encode($payload, string $secret, $header = []): string

Encodes a payload map into a signed HS256 JWT string (header.payload.signature).

jwt_decode(string $token, string $secret, int $leeway = 0, int $now = -1)

Verifies a JWT's format, header algorithm (HS256), HMAC-SHA256 signature (in constant time), and nbf / exp claims, then returns the decoded payload map.

jwt_verify(string $token, string $secret, int $leeway = 0, int $now = -1): bool

Returns true if jwt_decode(...) succeeds, or false if any validation or signature check fails.

jwt_decode_unverified(string $token)

Decodes the header and payload of a JWT without verifying its signature or time claims.

Base64URL & Crypto Helpers

Error Handling

All validation and verification failures throw Exception with clear jwt: ... messages:

Examples

01Quick start

Bundled with the compiler — import "jwt"; works offline in any file, or add it to a project with hphp install jwt.

import "jwt";

$secret = "my-super-secret-key";

// Encode a token with an expiration claim
$token = jwt_encode([
    "sub" => "user-123",
    "role" => "admin",
    "exp" => time() + 3600
], $secret);

println("Token: " . $token);

// Verify and decode
$payload = jwt_decode($token, $secret);
println("Subject: " . $payload["sub"]);

// Boolean verification
if (jwt_verify($token, $secret)) {
    println("Token is valid!");
}
Try it
hphp install jwt
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_jwt.hphp.