import { inject, injectable } from "@codemation/core"; import type { CallerContext, CredentialMaterialProvider, CredentialMaterialRef, MaterialBundle, } from "@codemation/core"; import { IllegalMaterialSourceError, ManagedCredentialMaterialWriteError, ManagedMaterialFetchError, } from "@codemation/core"; import { PairedFetch } from "../pairing/PairedFetch"; import { PairingConfigToken } from "../pairing/PairingConfigToken"; import type { PairingConfig } from "../pairing/pairing.types"; @injectable() export class ControlPlaneCredentialMaterialProvider implements CredentialMaterialProvider { constructor( @inject(PairedFetch) private readonly pairedFetch: PairedFetch, @inject(PairingConfigToken) private readonly pairingConfig: PairingConfig, ) {} async getMaterial(ref: CredentialMaterialRef, context: CallerContext): Promise { if (ref.source !== "control-plane") { throw new IllegalMaterialSourceError(ref.source, "ControlPlaneCredentialMaterialProvider"); } const url = `${this.pairingConfig.controlPlaneUrl}/internal/credentials/material/${encodeURIComponent(ref.id)}`; const response = await this.pairedFetch.post(url, { callerContext: context }); if (!response.ok) { const body = await response.text().catch(() => ""); throw new ManagedMaterialFetchError(response.status, body.slice(0, 500)); } const json = (await response.json()) as { accessToken?: unknown; expiresAt?: unknown; scopes?: unknown; providerAccountId?: unknown; typeId?: unknown; }; if (typeof json.accessToken !== "string" || json.accessToken.length === 0) { throw new ManagedMaterialFetchError( response.status, "missing accessToken in CP response", "Control-plane material response missing accessToken", ); } return { accessToken: json.accessToken, refreshToken: undefined, expiresAt: typeof json.expiresAt === "string" ? json.expiresAt : undefined, grantedScopes: Array.isArray(json.scopes) ? json.scopes.filter((s): s is string => typeof s === "string") : [], }; } async setMaterial(_ref: CredentialMaterialRef, _material: MaterialBundle): Promise { throw new ManagedCredentialMaterialWriteError(); } }