File

src/crypto/key/kms-adapter.ts

Description

Abstract base class for KMS adapters.

Each KMS adapter (DB, Vault, …) implements only the cryptographic and storage operations specific to that backend. DB queries for routing, config‐import and metadata updates live in the concrete KeyService facade — not here.

Index

Properties
Methods
Accessors

Properties

Protected Readonly logger
Type : unknown
Default value : new Logger(this.constructor.name)

Methods

Abstract create
create(tenantId: string)

Generate a new key pair and persist it in this KMS.

Parameters :
Name Type Optional
tenantId string No
Returns : Promise<string>

the key ID

Abstract deleteKey
deleteKey(tenantId: string, keyId: string)

Delete a key from this KMS.

Parameters :
Name Type Optional
tenantId string No
keyId string No
Returns : Promise<void>
Abstract getKid
getKid(tenantId: string, usage?: KeyUsage)

Get the first available key ID for this tenant.

Parameters :
Name Type Optional
tenantId string No
usage KeyUsage Yes
Returns : Promise<string>
Abstract getPublicKey
getPublicKey(type: unknown, tenantId: string, keyId?: string)

Get the public key in JWK format.

Parameters :
Name Type Optional
type unknown No
tenantId string No
keyId string Yes
Returns : Promise<JWK>
Abstract getPublicKey
getPublicKey(type: unknown, tenantId: string, keyId?: string)

Get the public key in PEM format.

Parameters :
Name Type Optional
type unknown No
tenantId string No
keyId string Yes
Returns : Promise<string>
Abstract getPublicKey
getPublicKey(type: "pem" | "jwk", tenantId: string, keyId?: string)
Parameters :
Name Type Optional
type "pem" | "jwk" No
tenantId string No
keyId string Yes
Returns : Promise<JWK | string>
Abstract import
import(tenantId: string, body: KeyImportDto)

Import existing key material into this KMS.

Parameters :
Name Type Optional
tenantId string No
body KeyImportDto No
Returns : Promise<string>

the key ID

Abstract init
init(tenantId: string)

Initialise the KMS for a tenant (e.g. create a Vault transit mount).

Parameters :
Name Type Optional
tenantId string No
Returns : Promise<string>
Abstract signer
signer(tenantId: string, keyId?: string)

Get a signer callback for the given key.

Parameters :
Name Type Optional
tenantId string No
keyId string Yes
Returns : Promise<Signer>
Abstract signJWT
signJWT(payload: JWTPayload, header: JWSHeaderParameters, tenantId: string, keyId?: string)

Sign a JWT.

Parameters :
Name Type Optional
payload JWTPayload No
header JWSHeaderParameters No
tenantId string No
keyId string Yes
Returns : Promise<string>

Accessors

capabilities
getcapabilities()

Capabilities of this KMS provider. Override in subclasses to restrict operations (e.g. Vault cannot import).

import { Logger } from "@nestjs/common";
import { Signer } from "@sd-jwt/types";
import { JWK, JWSHeaderParameters, JWTPayload } from "jose";
import { KeyImportDto } from "./dto/key-import.dto";

/**
 * Describes what operations a KMS provider supports.
 * Returned by the providers endpoint so the UI can adapt accordingly.
 */
export interface KmsProviderCapabilities {
    canImport: boolean;
    canCreate: boolean;
    canDelete: boolean;
}

/**
 * Abstract base class for KMS adapters.
 *
 * Each KMS adapter (DB, Vault, …) implements **only** the cryptographic and
 * storage operations specific to that backend.  DB queries for routing,
 * config‐import and metadata updates live in the concrete {@link KeyService}
 * facade — not here.
 */
export abstract class KmsAdapter {
    protected readonly logger = new Logger(this.constructor.name);

    /**
     * Capabilities of this KMS provider.
     * Override in subclasses to restrict operations (e.g. Vault cannot import).
     */
    get capabilities(): KmsProviderCapabilities {
        return { canImport: true, canCreate: true, canDelete: true };
    }

    /**
     * Initialise the KMS for a tenant (e.g. create a Vault transit mount).
     */
    abstract init(tenantId: string): Promise<string>;

    /**
     * Generate a new key pair and persist it in this KMS.
     * @returns the key ID
     */
    abstract create(tenantId: string): Promise<string>;

    /**
     * Import existing key material into this KMS.
     * @returns the key ID
     */
    abstract import(tenantId: string, body: KeyImportDto): Promise<string>;

    /** Get a signer callback for the given key. */
    abstract signer(tenantId: string, keyId?: string): Promise<Signer>;

    /** Get the first available key ID for this tenant. */
    abstract getKid(tenantId: string, usage?: KeyUsage): Promise<string>;

    /** Get the public key in JWK format. */
    abstract getPublicKey(
        type: "jwk",
        tenantId: string,
        keyId?: string,
    ): Promise<JWK>;
    /** Get the public key in PEM format. */
    abstract getPublicKey(
        type: "pem",
        tenantId: string,
        keyId?: string,
    ): Promise<string>;
    abstract getPublicKey(
        type: "pem" | "jwk",
        tenantId: string,
        keyId?: string,
    ): Promise<JWK | string>;

    /** Sign a JWT. */
    abstract signJWT(
        payload: JWTPayload,
        header: JWSHeaderParameters,
        tenantId: string,
        keyId?: string,
    ): Promise<string>;

    /** Delete a key from this KMS. */
    abstract deleteKey(tenantId: string, keyId: string): Promise<void>;
}

results matching ""

    No results matching ""