Class Session

Represents a stateless, cryptographically verifiable session token.

Instead of storing session state on the server, the Session object encapsulates all necessary authorization data (handle ID, scopes, expiration, derivation path) into a self-contained structure that can be verified using the Handle's or SubHandle's public key.

Sessions can be created from either a Handle or a SubHandle. When created from a SubHandle, the token carries the derivation path (hPath) for hierarchical auditing.

Properties

handleId: string
handleName: string
audience: string
scopes: string[]
expiresAt: number
token: string
path?: string[]

Derivation path, present only for SubHandle sessions.

sessionId: string

Unique session identifier, used for revocation.

Methods

  • Checks if the session has expired based on the current time.

    Returns boolean

    true if the current time is past the expiresAt timestamp.

  • Creates a new signed session token.

    If the handle is a SubHandle, its constraints are validated before signing (audience, scopes, TTL, expiration). The derivation path is included in the payload as hPath.

    Parameters

    • handle: SubHandle | Handle

      The Handle or SubHandle that will sign this session.

    • options: SessionOptions

      Session configuration including audience, scopes, TTL, and optional sessionId.

    Returns Promise<Session>

    A Promise resolving to a new Session instance.

    const session = await Session.create(handle, {
    audience: 'app.example.com',
    scopes: ['read', 'write'],
    ttl: 3600,
    sessionId: 'unique-session-id' // optional
    });
  • Verifies a session token statelessly without server-side storage.

    Validates:

    • Token format (two Base64URL parts)
    • Payload size limit
    • Required fields (hId, hNm, aud, scp, exp, iat, jti)
    • Audience match
    • Expiration (with clock skew tolerance)
    • Signature (against the reconstructed Handle or SubHandle public key)
    • Revocation status (if a RevocationChecker is provided)

    For SubHandle sessions (those with hPath), the Handle/SubHandle is reconstructed atomically via Identity.deriveSubHandle.

    Parameters

    • token: string

      The Base64URL-encoded session token string.

    • companyIdentity: Identity

      The Identity used to reconstruct Handle/SubHandle public keys.

    • expectedAudience: string

      The audience that this token must be intended for.

    • OptionalrevocationChecker: RevocationChecker

      Optional checker for revocation list.

    Returns Promise<Session>

    A Promise resolving to a verified Session instance.

    If the token is invalid, expired, tampered, audience mismatch, or revoked.

    const session = await Session.verifyStateless(
    token,
    companyIdentity,
    'app.example.com',
    redisRevocationChecker // optional
    );
  • Verifies a session token together with its attestation chain (Mode 2 - using an attestation chain) — for verifiers that hold only the root PUBLIC key.

    The chain contains serialized attestations, root first:

    • handle sessions: [attestHandle_token] (length 1)
    • subhandle sessions: [attestHandle_token, attestSubHandle_token] (length 2)

    Enforced, in order: token structure and required fields (including iat); audience; session time (±30 s skew); chain shape; parent signature against rootPublicKey; parent validity and revocation; for subhandle sessions — child signature against the parent's subjectId, child validity/revocation, name/path agreement, wildcard permitting, hId == child.subjectId, and grant nesting (child scopes ⊆ parent scopes, child maxTtl ≤ parent maxTtl); session vs the effective grant (scopes, audiences, TTL); session expiry against the earliest attestation expiry; session signature against hId; session revocation — last.

    Parameters

    • token: string
    • rootPublicKey: Uint8Array
    • attestationChain: string[]

      Attestation tokens, root first; length must match the session kind (1 for Handle, 2 for SubHandle).

    • expectedAudience: string
    • OptionalrevocationChecker: RevocationChecker

      Optional; consulted for attestation jti values (each chain level) and the session jti.

    Returns Promise<Session>

    The verified session.

    With code and level identifying the failed layer — integrate on these fields, not on message text.

    const A = await identity.attestHandle('station-001', grantA);
    const sub = await station.deriveSubHandle('connector-ccs');
    const B = await station.attestSubHandle('connector-ccs', grantB);
    const session = await Session.create(sub, {
    audience: 'ev-app.com', scopes: ['charge:start'], ttl: 1800,
    });
    const verified = await Session.verifyAttested(
    session.token, identity.getPublicKey(), [A.token, B.token],
    'ev-app.com',
    );