UNPKG

@faceteer/cdk

Version:

CDK 2.0 constructs and helpers that make composing a Lambda powered service easier.

128 lines (127 loc) 4.5 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.JwtAuthorizer = exports.LambdaAuthorizer = exports.ApiStage = exports.ApiGateway = void 0; exports.isLambdaAuthorizerConfig = isLambdaAuthorizerConfig; exports.isJwtAuthorizerConfig = isJwtAuthorizerConfig; const aws_apigatewayv2_1 = require("aws-cdk-lib/aws-apigatewayv2"); const cdk = __importStar(require("aws-cdk-lib")); class ApiGateway extends aws_apigatewayv2_1.CfnApi { constructor(scope, id, props) { super(scope, id, { protocolType: 'HTTP', corsConfiguration: { allowHeaders: [ 'Authorization', 'Content-Type', 'Accept', 'Accept-Language', 'Content-Language', 'Cf-Access-Jwt-Assertion', ], allowMethods: [ 'GET', 'HEAD', 'OPTIONS', 'PATCH', 'POST', 'PUT', 'DELETE', ], allowOrigins: ['*'], maxAge: 3600, }, ...props, }); } } exports.ApiGateway = ApiGateway; class ApiStage extends aws_apigatewayv2_1.CfnStage { constructor(scope, id, props) { super(scope, id, { apiId: props.api.ref, stageName: '$default', autoDeploy: true, ...props, }); } } exports.ApiStage = ApiStage; function isLambdaAuthorizerConfig(config) { return (!!config && 'fn' in config && 'identitySource' in config && 'enableSimpleResponses' in config); } function isJwtAuthorizerConfig(config) { return !!config && !isLambdaAuthorizerConfig(config); } class LambdaAuthorizer extends aws_apigatewayv2_1.CfnAuthorizer { constructor(scope, id, props) { super(scope, id, { apiId: props.api.ref, authorizerType: 'REQUEST', authorizerUri: cdk.Fn.join('', [ 'arn:', cdk.Fn.ref('AWS::Partition'), ':apigateway:', cdk.Fn.ref('AWS::Region'), ':lambda:path/2015-03-31/functions/', props.config.fn.functionArn, '/invocations', ]), identitySource: [...props.config.identitySource], authorizerPayloadFormatVersion: '2.0', enableSimpleResponses: props.config.enableSimpleResponses, ...props, }); } } exports.LambdaAuthorizer = LambdaAuthorizer; class JwtAuthorizer extends aws_apigatewayv2_1.CfnAuthorizer { constructor(scope, id, props) { super(scope, id, { apiId: props.api.ref, authorizerType: 'JWT', identitySource: [...props.config.identitySource], jwtConfiguration: { audience: [...props.config.audience], issuer: props.config.issuer, }, ...props, }); } } exports.JwtAuthorizer = JwtAuthorizer;