import { IncomingHttpHeaders } from "http2"; /** Header that carries the webhook signature. */ export declare const SIGNATURE_HEADER = "X-Twilio-Signature"; /** * Header Twilio adds to webhooks signed with a SharedKey: * `key=,algorithm=` */ export declare const SIGNATURE_SETTINGS_HEADER = "X-Twilio-Webhooks-Signature-Settings"; export declare const HMAC_SHA1 = "HMAC_SHA1"; export declare const HMAC_SHA256 = "HMAC_SHA256"; export interface Request { protocol: string; header(name: string): string | undefined; headers: IncomingHttpHeaders; originalUrl: string; rawBody?: any; body: any; } export interface RequestValidatorOptions { /** * The full URL (with query string) you used to configure the webhook with Twilio - overrides host/protocol options */ url?: string; /** * Manually specify the host name used by Twilio in a number's webhook config */ host?: string; /** * Manually specify the protocol used by Twilio in a number's webhook config */ protocol?: string; /** * Signing algorithm: HMAC_SHA1 (default) or HMAC_SHA256. See getSignatureAlgorithm() */ algorithm?: string; } export interface WebhookOptions { /** * Whether or not the middleware should validate the request * came from Twilio. Default true. If the request does not originate from * Twilio, we will return a text body and a 403. If there is no configured * auth token and validate=true, this is an error condition, so we will return * a 500. */ validate?: boolean; /** * Add helpers to the response object to improve support for XML (TwiML) rendering. Default true. */ includeHelpers?: boolean; /** * The full URL (with query string) you used to configure the webhook with Twilio - overrides host/protocol options */ url?: string; /** * Manually specify the host name used by Twilio in a number's webhook config */ host?: string; /** * Manually specify the protocol used by Twilio in a number's webhook config */ protocol?: string; /** * Authentication token */ authToken?: string; /** * SharedKey secret, used instead of the auth token for webhooks that carry the * X-Twilio-Webhooks-Signature-Settings header (signed with a SharedKey). The signing * algorithm is read from that header. */ sharedKeySecret?: string; } /** Utility function to get the signing algorithm named in the X-Twilio-Webhooks-Signature-Settings header. A missing or empty header (or one without `algorithm=`) means the legacy Auth Token signature, HMAC_SHA1. @param signatureSettingsHeader - The header value, e.g. `key=webhooks_sharedkey_...,algorithm=HMAC_SHA256` @returns the algorithm name */ export declare function getSignatureAlgorithm(signatureSettingsHeader?: string | null): string; /** Utility function to get the expected signature for a given request @param authToken - The auth token, as seen in the Twilio portal, or the SharedKey secret @param url - The full URL (with query string) you configured to handle this request @param params - the parameters sent with this request @param algorithm - HMAC_SHA1 (default) or HMAC_SHA256 @returns signature @throws Error if the algorithm is not supported */ export declare function getExpectedTwilioSignature(authToken: string, url: string, params: Record, algorithm?: string): string; /** Utility function to get the expected body hash for a given request's body @param body - The plain-text body of the request */ export declare function getExpectedBodyHash(body: string): string; /** Utility function to validate an incoming request is indeed from Twilio @param authToken - The auth token, as seen in the Twilio portal, or the SharedKey secret @param twilioHeader - The value of the X-Twilio-Signature header from the request @param url - The full URL (with query string) you configured to handle this request @param params - the parameters sent with this request @param algorithm - HMAC_SHA1 (default) or HMAC_SHA256. See getSignatureAlgorithm(). An unsupported algorithm fails validation and logs a warning. @returns valid */ export declare function validateRequest(authToken: string, twilioHeader: string, url: string, params: Record, algorithm?: string): boolean; export declare function validateBody(body: string, bodyHash: any[] | string | Buffer): boolean; /** Utility function to validate an incoming request is indeed from Twilio. This also validates the request body against the bodySHA256 post parameter. @param authToken - The auth token, as seen in the Twilio portal, or the SharedKey secret @param twilioHeader - The value of the X-Twilio-Signature header from the request @param url - The full URL (with query string) you configured to handle this request @param body - The body of the request @param algorithm - HMAC_SHA1 (default) or HMAC_SHA256. See getSignatureAlgorithm(). @returns valid */ export declare function validateRequestWithBody(authToken: string, twilioHeader: string, url: string, body: string, algorithm?: string): boolean; /** Utility function to validate an incoming request is indeed from Twilio. adapted from https://github.com/crabasa/twiliosig @param request - A request object (based on Express implementation http://expressjs.com/api.html#req.params) @param authToken - The auth token, as seen in the Twilio portal @param opts - options for request validation: -> url: The full URL (with query string) you used to configure the webhook with Twilio - overrides host/protocol options -> host: manually specify the host name used by Twilio in a number's webhook config -> protocol: manually specify the protocol used by Twilio in a number's webhook config -> algorithm: HMAC_SHA1 (default) or HMAC_SHA256; pass the result of getSignatureAlgorithm() for SharedKey webhooks */ export declare function validateIncomingRequest(request: Request, authToken: string, opts?: RequestValidatorOptions): boolean; export declare function validateExpressRequest(request: Request, authToken: string, opts?: RequestValidatorOptions): boolean; /** Express middleware to accompany a Twilio webhook. Provides Twilio request validation, and makes the response a little more friendly for our TwiML generator. Request validation requires the express.urlencoded middleware to have been applied (e.g. app.use(express.urlencoded()); in your app config). Options: - validate: {Boolean} whether or not the middleware should validate the request came from Twilio. Default true. If the request does not originate from Twilio, we will return a text body and a 403. If there is no configured auth token and validate=true, this is an error condition, so we will return a 500. - host: manually specify the host name used by Twilio in a number's webhook config - protocol: manually specify the protocol used by Twilio in a number's webhook config - url: The full URL (with query string) you used to configure the webhook with Twilio - overrides host/protocol options - sharedKeySecret: the SharedKey secret, used for webhooks signed with a SharedKey (those carry the X-Twilio-Webhooks-Signature-Settings header). The algorithm, HMAC_SHA1 or HMAC_SHA256, is read from the header. Returns a middleware function. Examples: var webhookMiddleware = twilio.webhook(); var webhookMiddleware = twilio.webhook('asdha9dhjasd'); //init with auth token var webhookMiddleware = twilio.webhook({ validate:false // don't attempt request validation }); var webhookMiddleware = twilio.webhook({ host: 'hook.twilio.com', protocol: 'https' }); var webhookMiddleware = twilio.webhook('asdha9dhjasd', { sharedKeySecret: 'shared key secret' // for webhooks signed with a SharedKey }); */ export declare function webhook(opts?: string | WebhookOptions, authToken?: string | WebhookOptions): (req: any, res: any, next: any) => void;