UNPKG

apigw-vtl-emulator

Version:

JavaScript VTL engine for AWS API Gateway mapping templates - works in both Node.js and browsers

351 lines 10.8 kB
"use strict"; /** * Context Functions - Implementation of $context variables for AWS API Gateway VTL */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ResponseOverrideContext = exports.QuerystringContext = exports.PathContext = exports.HeaderContext = exports.RequestOverrideContext = exports.ErrorContext = exports.AuthorizerContext = exports.ValidityContext = exports.ClientCertContext = exports.IdentityContext = exports.ContextFunctions = void 0; class ContextFunctions { constructor(context) { this.context = context; // Expose properties directly for velocityjs access this.accountId = this.context.accountId || '123456789012'; this.apiId = this.context.apiId || 'abc123def4'; this.requestId = this.context.requestId || `test-request-id-${Date.now()}`; this.extendedRequestId = this.context.extendedRequestId || `test-extended-request-id-${Date.now()}`; this.httpMethod = this.context.httpMethod || 'GET'; this.stage = this.context.stage || 'test'; this.deploymentId = this.context.deploymentId || 'deployment-123'; this.domainName = this.context.domainName || 'abc123def4.execute-api.us-east-1.amazonaws.com'; this.domainPrefix = this.context.domainPrefix || 'abc123def4'; this.path = this.context.path || '/test/resource'; this.protocol = this.context.protocol || 'HTTP/1.1'; this.resourceId = this.context.resourceId || 'resource-123'; this.resourcePath = this.context.resourcePath || '/resource'; const now = new Date(); this.requestTime = now.toUTCString().replace('GMT', '+0000'); this.requestTimeEpoch = Date.now(); this.isCanaryRequest = false; this.wafResponseCode = this.context.wafResponseCode || 'WAF_ALLOW'; this.webaclArn = this.context.webaclArn || 'arn:aws:wafv2:us-east-1:123456789012:regional/webacl/test-webacl/12345678-1234-1234-1234-123456789012'; // Initialize nested objects this.identity = new IdentityContext(this.context); this.authorizer = new AuthorizerContext(this.context); this.error = new ErrorContext(); this.requestOverride = new RequestOverrideContext(); this.responseOverride = new ResponseOverrideContext(); } // Keep getter methods for backward compatibility getAccountId() { return this.accountId; } getApiId() { return this.apiId; } getRequestId() { return this.requestId; } getExtendedRequestId() { return this.extendedRequestId; } getHttpMethod() { return this.httpMethod; } getStage() { return this.stage; } getDeploymentId() { return this.deploymentId; } getDomainName() { return this.domainName; } getDomainPrefix() { return this.domainPrefix; } getPath() { return this.path; } getProtocol() { return this.protocol; } getResourceId() { return this.resourceId; } getResourcePath() { return this.resourcePath; } getRequestTime() { return this.requestTime; } getRequestTimeEpoch() { return this.requestTimeEpoch; } getIsCanaryRequest() { return this.isCanaryRequest; } getWafResponseCode() { return this.wafResponseCode; } getWebaclArn() { return this.webaclArn; } // Nested context objects getIdentity() { return new IdentityContext(this.context); } getAuthorizer() { return new AuthorizerContext(this.context); } getError() { return new ErrorContext(); } getRequestOverride() { return new RequestOverrideContext(); } getResponseOverride() { return new ResponseOverrideContext(); } } exports.ContextFunctions = ContextFunctions; /** * Identity Context */ class IdentityContext { constructor(context) { this.context = context; // Expose properties directly const identity = this.getIdentityMap(); this.accountId = identity.accountId || '123456789012'; this.apiKey = identity.apiKey || null; this.apiKeyId = identity.apiKeyId || null; this.caller = identity.caller || 'AIDACKCEVSQ6C2EXAMPLE'; this.cognitoAuthenticationProvider = 'cognito-idp.us-east-1.amazonaws.com/us-east-1_example,cognito-idp.us-east-1.amazonaws.com/us-east-1_example:CognitoSignIn:user123'; this.cognitoAuthenticationType = 'authenticated'; this.cognitoIdentityId = 'us-east-1:12345678-1234-1234-1234-123456789012'; this.cognitoIdentityPoolId = 'us-east-1:12345678-1234-1234-1234-123456789012'; this.principalOrgId = 'o-1234567890'; this.sourceIp = identity.sourceIp || '192.0.2.1'; this.user = identity.user || 'AIDACKCEVSQ6C2EXAMPLE'; this.userAgent = identity.userAgent || 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'; this.userArn = identity.userArn || 'arn:aws:iam::123456789012:user/example-user'; this.vpcId = 'vpc-12345678'; this.vpceId = 'vpce-12345678'; this.clientCert = new ClientCertContext(); } getIdentityMap() { return this.context.identity || {}; } getAccountId() { const identity = this.getIdentityMap(); return identity.accountId || '123456789012'; } getApiKey() { const identity = this.getIdentityMap(); return identity.apiKey || null; } getApiKeyId() { const identity = this.getIdentityMap(); return identity.apiKeyId || null; } getCaller() { const identity = this.getIdentityMap(); return identity.caller || 'AIDACKCEVSQ6C2EXAMPLE'; } getCognitoAuthenticationProvider() { return 'cognito-idp.us-east-1.amazonaws.com/us-east-1_example,cognito-idp.us-east-1.amazonaws.com/us-east-1_example:CognitoSignIn:user123'; } getCognitoAuthenticationType() { return 'authenticated'; } getCognitoIdentityId() { return 'us-east-1:12345678-1234-1234-1234-123456789012'; } getCognitoIdentityPoolId() { return 'us-east-1:12345678-1234-1234-1234-123456789012'; } getPrincipalOrgId() { return 'o-1234567890'; } getSourceIp() { const identity = this.getIdentityMap(); return identity.sourceIp || '192.0.2.1'; } getUser() { const identity = this.getIdentityMap(); return identity.user || 'AIDACKCEVSQ6C2EXAMPLE'; } getUserAgent() { const identity = this.getIdentityMap(); return identity.userAgent || 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'; } getUserArn() { const identity = this.getIdentityMap(); return identity.userArn || 'arn:aws:iam::123456789012:user/example-user'; } getVpcId() { return 'vpc-12345678'; } getVpceId() { return 'vpce-12345678'; } getClientCert() { return new ClientCertContext(); } } exports.IdentityContext = IdentityContext; /** * Client Certificate Context */ class ClientCertContext { getClientCertPem() { return '-----BEGIN CERTIFICATE-----\nMIIDXTCCAkWgAwIBAgIJAKoK/OvK5tYzMA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNV\n-----END CERTIFICATE-----'; } getSubjectDN() { return 'CN=example.com, O=Example Corp, C=US'; } getIssuerDN() { return 'CN=Example CA, O=Example Corp, C=US'; } getSerialNumber() { return '1234567890123456789012345678901234567890'; } getValidity() { return new ValidityContext(); } } exports.ClientCertContext = ClientCertContext; /** * Certificate Validity Context */ class ValidityContext { getNotBefore() { return 'Jan 01 00:00:00 2023 GMT'; } getNotAfter() { return 'Jan 01 00:00:00 2024 GMT'; } } exports.ValidityContext = ValidityContext; /** * Authorizer Context */ class AuthorizerContext { constructor(context) { this.context = context; // Expose properties directly this.principalId = this.context.principalId || 'user123'; this.claims = null; // Simulate authorizer context map this.key = 'value'; this.numKey = 1; this.boolKey = true; this.user_id = '12345'; this.scope = 'read write'; } getPrincipalId() { return this.context.principalId || 'user123'; } getClaims() { return null; } get(property) { if (this.context[property] !== undefined) { return this.context[property]; } // Simulate authorizer context map const authorizerContext = { key: 'value', numKey: 1, boolKey: true, user_id: '12345', scope: 'read write' }; return authorizerContext[property] || null; } } exports.AuthorizerContext = AuthorizerContext; /** * Error Context */ class ErrorContext { getMessage() { return 'Internal server error'; } getMessageString() { return '"Internal server error"'; } getResponseType() { return 'DEFAULT_5XX'; } getValidationErrorString() { return 'Validation error: Invalid parameter value'; } } exports.ErrorContext = ErrorContext; /** * Request Override Context */ class RequestOverrideContext { getHeader() { return new HeaderContext(); } getPath() { return new PathContext(); } getQuerystring() { return new QuerystringContext(); } } exports.RequestOverrideContext = RequestOverrideContext; /** * Header Context */ class HeaderContext { get(headerName) { const headers = { 'Content-Type': 'application/json', 'Authorization': 'Bearer override-token' }; return headers[headerName] || null; } } exports.HeaderContext = HeaderContext; /** * Path Context */ class PathContext { get(pathName) { const paths = { id: 'override-id', version: 'v2' }; return paths[pathName] || null; } } exports.PathContext = PathContext; /** * Query String Context */ class QuerystringContext { get(querystringName) { const querystrings = { filter: 'override-filter', sort: 'desc' }; return querystrings[querystringName] || null; } } exports.QuerystringContext = QuerystringContext; /** * Response Override Context */ class ResponseOverrideContext { getStatus() { return '200'; } getHeader() { return new HeaderContext(); } } exports.ResponseOverrideContext = ResponseOverrideContext; //# sourceMappingURL=context-functions.js.map