Skip to content

Kitto.Web.JWT

JWT runtime infrastructure for KittoX authentication. Wraps the delphi-jose-jwt library (paolo-rossi) with KittoX-specific configuration parsing, key resolution, claims construction, validation and cookie issuance helpers.

EKJWTError class

Exception type raised for JWT configuration and key-resolution errors.

TKJWTAclEntry record

Single ACL row carried by the kx_acl claim. Mirrors the three columns of the KITTO_PERMISSIONS table the DB access controller reads: Pattern — RESOURCE_URI_PATTERN (wildcards * ?, REGEX:, ~ negation) Modes — comma-separated list of ACM_* mode codes (V/M/E/A/D/R) GrantValue — '0' / '1' for boolean grants, any string for non-standard modes Serialised in the JWT as a 3-element JSON array [pattern, modes, grant].

TKJWTContext record

Strongly-typed view of validated JWT claims used by the KittoX runtime. Populated by TKJWTValidator on a successful Validate call.

pascal
procedure Clear;

Resets all fields to empty/invalid.

pascal
function HasRole(const ARole: string): Boolean;

True if the Roles list contains ARole (case-insensitive).

TKJWTSigningKey record

Resolved signing material. For HS* algorithms only PrivateKey is used. For RS*/ES* PrivateKey is the PEM private key, PublicKey is the PEM public key (verifier-only deploys can configure PublicKey alone).

pascal
function HasPrivateKey: Boolean;

True if a private (or symmetric) key is present.

pascal
function HasPublicKey: Boolean;

True if a public key is present (asymmetric verifier).

pascal
function IsSymmetric: Boolean;

True for symmetric (HS*) algorithms.

pascal
procedure Clear;

Zeroes and clears the key material.

TKJWTSigningKeyRegistry class

Process-wide registry of signing key providers, scoped by app name.

pascal
procedure RegisterProvider(const AAppName: string;

Registers a signing-key provider for the given app name (replaces any existing).

pascal
procedure UnregisterProvider(const AAppName: string);

Removes the signing-key provider registered for the given app name.

pascal
function FindProvider(const AAppName: string): TKJWTSigningKeyProvider;

Returns the provider registered for the app name, or nil.

TKJWTConfig class

Parsed configuration extracted from the Auth/JWT YAML node.

pascal
constructor Create(const AAppName: string;

Parses the Auth/JWT config node for the given app.

pascal
property AppName: string read FAppName;

The application name this config belongs to.

pascal
property Algorithm: TJOSEAlgorithmId read FAlgorithm;

The JOSE signing algorithm (HS256, RS256, …).

pascal
property Issuer: string read FIssuer;

Expected/emitted 'iss' (issuer) claim.

pascal
property Audience: string read FAudience;

Expected/emitted 'aud' (audience) claim.

pascal
property TokenLifetimeSeconds: Integer read FTokenLifetimeSeconds;

Token lifetime in seconds (exp = iat + this).

pascal
property SlidingThresholdSeconds: Integer read FSlidingThresholdSeconds;

Remaining-lifetime threshold (seconds) below which the cookie is slid/refreshed.

pascal
property MaxSessionLifetimeSeconds: Integer read FMaxSessionLifetimeSeconds;

Absolute cap (seconds) on the total session length: past this, sliding stops renewing the token and a fresh login is required. 0 = no cap. Default DEFAULT_MAX_SESSION_LIFETIME (12h).

pascal
property ClockSkewSeconds: Integer read FClockSkewSeconds;

Allowed clock skew (seconds) when validating exp/nbf.

pascal
property CookieName: string read FCookieName;

Name of the cookie carrying the token (default 'kx_token').

pascal
property CookiePath: string read FCookiePath write FCookiePath;

Cookie Path attribute.

pascal
property CookieSecure: Boolean read FCookieSecure;

Cookie Secure attribute.

pascal
property CookieHttpOnly: Boolean read FCookieHttpOnly;

Cookie HttpOnly attribute.

pascal
property CookieSameSite: string read FCookieSameSite;

Cookie SameSite attribute ('Strict'/'Lax'/'None'/'').

pascal
property IncludeRoles: Boolean read FIncludeRoles;

Include the user's roles as a claim.

pascal
property IncludeDB: Boolean read FIncludeDB;

Include the active database name as a claim ('db').

pascal
property IncludeDisplayName: Boolean read FIncludeDisplayName;

Include the user's display name as a claim.

pascal
property IncludeLanguage: Boolean read FIncludeLanguage;

Include the language as a claim.

pascal
property IncludeACL: Boolean read FIncludeACL;

Include the ACL grant rows as a claim (when AccessControl: JWT).

pascal
function GetSigningKey: TKJWTSigningKey;

Returns the resolved signing key (from config spec or a registered provider).

TKJWTBuilder class

Builds a compact JWT from a TKJWTContext + signing config. Stateless.

pascal
class function Build(const AContext: TKJWTContext;

Builds and signs a compact JWT from the context + config (plus any extra claims).

TKJWTValidator class

Validates a compact JWT against config + signing key. Stateless.

pascal
class function Validate(const ACompactToken: string;

Verifies the token's signature and claims against AConfig; on success fills AContext and returns True, else returns False with AErrorMessage set.

TKJWTCookieHelper class

Cookie helpers.

pascal
class procedure Issue(const ACompactToken: string;

Writes the token to the response as the configured secure cookie.

pascal
class procedure Clear(const AConfig: TKJWTConfig);

Clears (expires) the token cookie on the response.

pascal
class function ReadFromRequest(const AConfig: TKJWTConfig): string;

Reads the token from the request's configured cookie ('' if absent).

pascal
class function ShouldSlide(const AContext: TKJWTContext;

True if the token is close enough to expiry that the cookie should be re-issued (slid).

pascal
class function IsSessionCapReached(const AContext: TKJWTContext;

True when the absolute session cap is configured (>0) and the time since SessionStart has reached it: the token must no longer be slid, and is refused, so a fresh login is required.

TKJWTRevocation class

Process-wide denylist of revoked token ids (jti). Logout and password change add the current token's jti; the validator refuses a token whose jti is listed. Each entry carries the token's own expiry and is purged lazily, so the list never grows past the set of still-valid revoked tokens. v1 is single-instance: the list lives in this process only, so behind a load balancer a revocation does not propagate to other nodes.

pascal
class function Instance: TKJWTRevocation;

The process-wide singleton.

pascal
procedure Revoke(const AJti: string;

Revokes AJti until AExpiry (the token's own exp), after which the entry is dropped because a token that old would be rejected anyway.

pascal
function IsRevoked(const AJti: string): Boolean;

True if AJti is currently revoked (and not yet past its expiry).

Routines

pascal
function StringToAlgorithmId(const S: string): TJOSEAlgorithmId;

Maps an algorithm name (e.g. 'HS256') to its TJOSEAlgorithmId.

pascal
function ResolveKeySpec(const ASpec: string): TBytes;

Resolves a key spec (inline value, file: or env: reference) to raw key bytes.

pascal
function TryDecodeSidFromJWT(const ACompactToken: string;

Decodes the payload portion of a compact JWT without verifying the signature, and extracts the 'sid' custom claim if present. Used by the engine to correlate the request to a server-side TKWebSession before the JWT signature is verified by the application's auth gate. Safety: signature is still verified later before any authenticated operation, so the unsafe decode here only affects which session object is bound to the current thread — it cannot grant any privilege.

Released under Apache License, Version 2.0.