Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@envoy/envoy-integrations-sdk",
"version": "2.3.6",
"version": "2.4.0-beta.5",
"description": "SDK for building Envoy integrations.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
25 changes: 22 additions & 3 deletions src/sdk/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ import EnvoyPluginJobAttachment from './EnvoyPluginJobAttachment';
import EnvoyPluginSDK from './EnvoyPluginSDK';
import EnvoyPluginAPI from './EnvoyPluginAPI';

/**
* Options for configuring the Envoy middleware.
*/
export interface EnvoyMiddlewareOptions extends Partial<EnvoySignatureVerifierOptions> {
/** Optional custom client ID to use for API authentication instead of environment variable */
customClientId?: string;
/** Optional custom client secret to use for API authentication instead of environment variable */
customClientSecret?: string;
}

/**
* Sets up an {@link EnvoyPluginSDK} object in the path `req.envoy`.
* Modifies the `res` object to include Envoy's helpers, per {@link EnvoyResponse}.
Expand All @@ -18,8 +28,8 @@ import EnvoyPluginAPI from './EnvoyPluginAPI';
*
* @category Middleware
*/
export function envoyMiddleware(options?: EnvoySignatureVerifierOptions): RequestHandler {
const signatureVerifier = new EnvoySignatureVerifier(options);
export function envoyMiddleware(options?: EnvoyMiddlewareOptions): RequestHandler {
const signatureVerifier = new EnvoySignatureVerifier(options as EnvoySignatureVerifierOptions);
const verify = (req: VerifiedRequest, res: Response, rawBody: Buffer) => {
req[VERIFIED] = signatureVerifier.verify(req, rawBody);
};
Expand All @@ -35,7 +45,10 @@ export function envoyMiddleware(options?: EnvoySignatureVerifierOptions): Reques
try {
const now = Date.now();
if (now > threshold) {
const { access_token: rawAccessToken, expires_in: expiresIn } = await EnvoyPluginAPI.loginAsPlugin();
const hasCustomCredentials = options?.customClientId && options?.customClientSecret;
const { access_token: rawAccessToken, expires_in: expiresIn } = hasCustomCredentials
? await EnvoyPluginAPI.loginAsPlugin(options?.customClientId, options?.customClientSecret)
: await EnvoyPluginAPI.loginAsPlugin();
accessToken = rawAccessToken;
threshold = now + expiresIn * 1000 - 1000 * 60 * 10;
}
Expand Down Expand Up @@ -101,3 +114,9 @@ export function errorMiddleware(onError: (err: Error) => void = () => {}): Error
res.end(JSON.stringify({ message: err.message }));
};
}

/**
* Backward compatibility alias for envoyMiddleware
* @deprecated Use envoyMiddleware instead
*/
export const middleware = envoyMiddleware;