aws-cdk-lib
Version:
Version 2 of the AWS Cloud Development Kit library
2 lines (1 loc) • 1.79 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0}),exports.WebSocketLambdaAuthorizer=void 0;var aws_apigatewayv2_1=()=>{var tmp=require("../../../aws-apigatewayv2");return aws_apigatewayv2_1=()=>tmp,tmp},aws_iam_1=()=>{var tmp=require("../../../aws-iam");return aws_iam_1=()=>tmp,tmp},core_1=()=>{var tmp=require("../../../core");return core_1=()=>tmp,tmp};class WebSocketLambdaAuthorizer{constructor(id,handler,props={}){this.id=id,this.handler=handler,this.props=props}bind(options){if(this.webSocketApi&&this.webSocketApi.apiId!==options.route.webSocketApi.apiId)throw new Error("Cannot attach the same authorizer to multiple Apis");return this.authorizer||(this.webSocketApi=options.route.webSocketApi,this.authorizer=new(aws_apigatewayv2_1()).WebSocketAuthorizer(options.scope,this.id,{webSocketApi:options.route.webSocketApi,identitySource:this.props.identitySource??["route.request.header.Authorization"],type:aws_apigatewayv2_1().WebSocketAuthorizerType.LAMBDA,authorizerName:this.props.authorizerName??this.id,authorizerUri:lambdaAuthorizerArn(this.handler)}),this.handler.addPermission(`${core_1().Names.nodeUniqueId(this.authorizer.node)}-Permission`,{scope:options.scope,principal:new(aws_iam_1()).ServicePrincipal("apigateway.amazonaws.com"),sourceArn:core_1().Stack.of(options.route).formatArn({service:"execute-api",resource:options.route.webSocketApi.apiId,resourceName:`authorizers/${this.authorizer.authorizerId}`})})),{authorizerId:this.authorizer.authorizerId,authorizationType:"CUSTOM"}}}exports.WebSocketLambdaAuthorizer=WebSocketLambdaAuthorizer;function lambdaAuthorizerArn(handler){return`arn:${core_1().Stack.of(handler).partition}:apigateway:${core_1().Stack.of(handler).region}:lambda:path/2015-03-31/functions/${handler.functionArn}/invocations`}