Xentitlement (Xentitlement v0.1.0)

Copy Markdown View Source

Xentitlement — Shared RSA-signed entitlement token library for distributed services.

This library provides a reusable entitlement system for validating cryptographically signed tokens across multiple applications. An entitlement is a JSON document signed with RSA-SHA256 that carries authorization metadata (user ID, operation type, resource constraints, expiration, etc.).

Usage

Validate a token received via HTTP header:

case Xentitlement.validate(token_json, public_key_pem, entitlement_id) do
  {:ok, claims} -> handle_authorized_request(claims)
  {:error, reason} -> reject_request(reason)
end

Extract the token from an HTTP header (e.g., X-Entitlement):

case Xentitlement.extract_from_header(conn) do
  {:ok, token} -> validate_token(token)
  {:error, _} -> reject_no_header()
end

Token Format

An entitlement token is a JSON object with these fields:

{
  "entitlement_id": "967376a7-6a33-4bc8-846c-ca443c5c56eb",
  "user_id": "user-123",
  "operation": "file_upload",
  "expires_at": "2026-12-31",
  "signature": "base64-encoded-rsa-sha256-signature",
  ...additional business fields...
}

The signature is computed over the canonical JSON representation (all fields except "signature", sorted by key). This ensures that the token payload cannot be tampered with without detection.

Key Functions

Summary

Functions

Delegates to Xentitlement.Entitlements.extract_from_header/2. See that module for full documentation.

Delegates to Xentitlement.Entitlements.sign_canonical_json/2. See that module for full documentation.

Delegates to Xentitlement.Entitlements.sign_token/2. See that module for full documentation.

Delegates to Xentitlement.Entitlements.validate/3. See that module for full documentation.

Functions

extract_from_header(conn, header_name \\ "x-entitlement")

Delegates to Xentitlement.Entitlements.extract_from_header/2. See that module for full documentation.

sign_canonical_json(canonical_json, private_key_pem)

Delegates to Xentitlement.Entitlements.sign_canonical_json/2. See that module for full documentation.

sign_token(claims, private_key_pem)

Delegates to Xentitlement.Entitlements.sign_token/2. See that module for full documentation.

validate(token_json, public_key_pem, entitlement_id)

Delegates to Xentitlement.Entitlements.validate/3. See that module for full documentation.