import { Logger } from '../logging'; import { SessionStorage } from '../storage/session-storage'; interface OAuthService { getLocationAccessToken(requestBody: { companyId: string; locationId: string; }): Promise; } /** * Minimal request shape the webhook middleware needs. Structurally compatible * with an Express `Request`, so no dependency on express is required. */ export interface WebhookRequest { method?: string; url?: string; headers: Record; /** Parsed JSON body (e.g. from `express.json()`), or the raw Buffer when using `express.raw()` */ body?: any; /** * Raw request body as received on the wire. When present it is used for * signature verification instead of re-serializing `body`. Capture it with * `express.json({ verify: (req, _res, buf) => { req.rawBody = buf; } })`. */ rawBody?: Buffer | string; /** Set by the middleware: whether the x-ghl-signature header verified against the configured public key */ isSignatureValid?: boolean; /** Set by the middleware: true when no signature header or public key was available so verification was skipped */ skippedSignatureVerification?: boolean; /** Set by the middleware when an INSTALL webhook was verified but the location token could not be generated */ installTokenError?: unknown; } export type WebhookResponse = unknown; export type WebhookNextFunction = (error?: any) => void; export type WebhookMiddleware = (req: WebhookRequest, res: WebhookResponse, next: WebhookNextFunction) => Promise; export interface WebhookManagerOptions { /** * OAuth client id. Its first segment is the app id that incoming webhooks * must match. Falls back to the CLIENT_ID environment variable. */ clientId?: string; /** * Ed25519 public key used to verify the x-ghl-signature header (PEM, or the * raw 32-byte key as base64/hex). Falls back to the * WEBHOOK_SIGNATURE_PUBLIC_KEY environment variable. */ publicKey?: string; /** * Log webhook headers and bodies without redacting Authorization headers, * tokens and secrets. Off by default. */ logSensitiveData?: boolean; } export declare const WEBHOOK_SIGNATURE_HEADER = "x-ghl-signature"; /** * WebhookManager handles incoming webhooks from GoHighLevel * Provides Express-compatible middleware for processing webhook events */ export declare class WebhookManager { private logger; private sessionStorage; private oauthService; private options; constructor(logger: Logger, sessionStorage: SessionStorage, oauthService: OAuthService, options?: WebhookManagerOptions); /** * Returns Express-compatible middleware for handling GoHighLevel webhooks. * Every webhook is verified against the x-ghl-signature header (Ed25519). * INSTALL webhooks generate and store a location token, UNINSTALL webhooks * remove the stored token. The outcome is exposed on the request as * `isSignatureValid`, `skippedSignatureVerification` and `installTokenError`. */ subscribe(): WebhookMiddleware; /** * Verify a legacy RSA-SHA256 webhook signature (x-wh-signature header). * @deprecated The x-wh-signature header is no longer sent. Use `verifyEd25519Signature` with the x-ghl-signature header. * @param payload - The JSON stringified request body * @param signature - The base64 signature * @param publicKey - The RSA public key (PEM) * @returns True if signature is valid, false otherwise */ verifySignature(payload: string, signature: string, publicKey: string): boolean; /** * Verify webhook signature using an Ed25519 public key * @param payload - The raw request body (Buffer) or its JSON string * @param signature - The base64 signature from the x-ghl-signature header * @param publicKey - The Ed25519 public key (PEM, or raw 32-byte key as base64/hex) * @returns True if signature is valid, false otherwise */ verifyEd25519Signature(payload: string | Buffer, signature: string, publicKey: string): boolean; /** * Accept the public key as PEM or as the raw 32-byte key encoded in base64 or hex */ private toEd25519PublicKey; /** * App id derived from the configured client id (option first, then CLIENT_ID env) */ private getAppId; /** * Ed25519 public key (option first, then WEBHOOK_SIGNATURE_PUBLIC_KEY env) */ private getPublicKey; private getHeader; /** * Bytes that were signed: the raw body when the host captured it, otherwise the * Buffer body from `express.raw()`, otherwise the re-serialized parsed body. */ private getSignedPayload; /** * Parsed JSON body regardless of whether the host used a JSON or raw body parser */ private parseBody; private safeJsonParse; /** * Generate location access token and store it using company token * @param companyId - The company ID * @param locationId - The location ID * @throws when the token exchange or storage write fails */ private generateLocationAccessToken; } export {}; //# sourceMappingURL=webhook-manager.d.ts.map