API Reference
Complete reference for the @quantumshield/qauth TypeScript SDK.
Classes
Interfaces
QAuthServer
classServer-side class for creating and validating QAuth tokens. Generates Ed25519 signing keys on construction.
Constructor
new QAuthServer(config: QAuthConfig)| Parameter | Type | Description |
|---|---|---|
| config | QAuthConfig | Server configuration with issuer and audience URLs. |
.getPublicKeys()
returns IssuerKeysgetPublicKeys(): IssuerKeysReturns the public keys for this server instance. Share these with QAuthValidator instances on API servers.
.createToken()
returns stringcreateToken(options: TokenOptions): stringCreates a new QAuth token signed with Ed25519. The token contains the payload claims encrypted and signed.
| Parameter | Type | Description |
|---|---|---|
| options.subject | string | Uint8Array | The token subject (usually user ID). |
| options.policyRef | string | Policy document URN reference. |
| options.audience? | string | string[] | Token audience(s). Defaults to server config audience. |
| options.validitySeconds? | number | Token validity in seconds. Defaults to 3600 (1 hour). |
| options.clientKey? | Uint8Array | Client's public key for proof-of-possession binding. |
| options.deviceKey? | Uint8Array | Device public key for device binding. |
| options.claims? | Record<string, unknown> | Custom claims to embed in the token. |
.validateToken()
returns TokenPayloadvalidateToken(token: string): TokenPayloadValidates a QAuth token by verifying the signature, checking expiration, not-before, issuer, and audience.
| Parameter | Type | Description |
|---|---|---|
| token | string | The QAuth token string to validate. |
Throws: Error if the token is invalid, expired, or has wrong issuer/audience.
QAuthClient
classClient-side class for proof-of-possession. Generates an Ed25519 keypair for signing request proofs.
Constructor
new QAuthClient().getPublicKey()
returns Uint8ArraygetPublicKey(): Uint8ArrayReturns the client's public key. Send this to the server during authentication to enable proof validation.
.createProof()
returns stringcreateProof(method: string, uri: string, token: string, body?: Uint8Array | string): stringCreates a proof-of-possession for an API request. The proof binds the token to the specific HTTP request (method, URI, body).
| Parameter | Type | Description |
|---|---|---|
| method | string | HTTP method (GET, POST, PUT, DELETE, etc.). |
| uri | string | Request URI path (e.g., '/api/resource'). |
| token | string | The QAuth access token. |
| body? | Uint8Array | string | Request body (for POST/PUT requests). |
QAuthValidator
classStandalone token validator using pre-shared issuer public keys. Deploy on API servers that need to validate tokens without access to the signing private key.
Constructor
new QAuthValidator(keys: IssuerKeys, config: QAuthConfig)| Parameter | Type | Description |
|---|---|---|
| keys | IssuerKeys | Issuer's public keys obtained from QAuthServer.getPublicKeys(). |
| config | QAuthConfig | Expected issuer and audience for validation. |
.validate()
returns TokenPayloadvalidate(token: string): TokenPayloadValidates a token against the pre-shared public keys. Checks signature, expiration, not-before, issuer, and audience.
| Parameter | Type | Description |
|---|---|---|
| token | string | The QAuth token string to validate. |
Throws: Error if validation fails.
ProofValidator
classValidates proof-of-possession proofs. Verifies that the request was made by the holder of the client's private key.
Constructor
new ProofValidator(clientPublicKey: Uint8Array)| Parameter | Type | Description |
|---|---|---|
| clientPublicKey | Uint8Array | The client's Ed25519 public key (32 bytes). |
.validate()
returns booleanvalidate(proof: string, method: string, uri: string, token: string, body?: Uint8Array | string): booleanValidates a proof of possession. Checks timestamp (60-second window), method/URI binding, body hash, token hash, and signature.
| Parameter | Type | Description |
|---|---|---|
| proof | string | The base64URL-encoded proof string from X-QAuth-Proof header. |
| method | string | The HTTP method of the request. |
| uri | string | The URI of the request. |
| token | string | The QAuth token from Authorization header. |
| body? | Uint8Array | string | The request body, if present. |
PolicyEngine
classEvaluates authorization policies. Load policy documents and evaluate them against request contexts. Supports glob patterns, conditions, and priority ordering.
Constructor
new PolicyEngine().loadPolicy()
returns voidloadPolicy(policy: Policy): voidLoads a policy document into the engine. Policies are stored by ID and can be updated by loading a new policy with the same ID.
| Parameter | Type | Description |
|---|---|---|
| policy | Policy | The policy document to load. |
.evaluate()
returns EvaluationResultevaluate(policyId: string, context: EvaluationContext): EvaluationResultEvaluates a loaded policy against the given context. Rules are sorted by priority (highest first). Returns the first matching rule's effect, or 'deny' if no rule matches (default deny).
| Parameter | Type | Description |
|---|---|---|
| policyId | string | The policy ID to evaluate. |
| context | EvaluationContext | The request context (subject, resource, request). |
Interfaces
QAuthConfig
TokenOptions
TokenPayload
IssuerKeys
Policy
PolicyRule
PolicyConditions
EvaluationContext
EvaluationResult
Utility Functions
getVersion()Returns the SDK version (e.g., '0.1.0').
getProtocolVersion()Returns the QAuth protocol version (e.g., '1.0').
initQAuth()No-op for the pure JS implementation. Included for API compatibility.
isInitialized()Always returns true for the pure JS implementation.
toBytes(data: string | Uint8Array)Converts a string to UTF-8 bytes.
bytesToHex(bytes: Uint8Array)Converts bytes to a hex string.
hexToBytes(hex: string)Converts a hex string to bytes.