-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(apigatewayv2): Allow to set Lambda authorizer for WebSocket API #13869
Comments
We are unassigning and marking this issue as p2, which means that we are unable to work on this immediately. |
Originally posted by @michaelgmcd in #14085 (comment) I was able to get to to work with a patch: const wsApi = new WebSocketApi(stack, 'wsApi', {
routeSelectionExpression: '$request.body.action',
disconnectRouteOptions: {
integration: new LambdaWebSocketIntegration({
handler: lambdas.wsDisconnect,
}),
},
defaultRouteOptions: {
integration: new LambdaWebSocketIntegration({
handler: lambdas.wsDefault,
}),
},
});
new WebSocketStage(stack, 'wsStage', {
webSocketApi: wsApi,
stageName: 'ws',
autoDeploy: true,
domainMapping: { domainName: wsDomainName },
});
const wsAuthorizer = new CfnAuthorizer(stack, 'WSAuthorizer', {
name: 'wsAuthorizer',
apiId: wsApi.apiId,
authorizerType: 'REQUEST',
authorizerUri: `arn:aws:apigateway:${constants.region}:lambda:path/2015-03-31/functions/${lambdas.wsAuth.functionArn}/invocations`,
identitySource: ['route.request.querystring.token'],
});
wsApi.addRoute('$connect', {
// @ts-ignore
authorizerId: wsAuthorizer.ref,
authorizationType: 'CUSTOM',
integration: new LambdaWebSocketIntegration({
handler: lambdas.wsConnect,
}),
}); node_modules/@aws-cdk/aws-apigatewayv2/lib/websocket/route.js class WebSocketRoute extends core_1.Resource {
/**
* @experimental
*/
constructor(scope, id, props) {
super(scope, id);
this.webSocketApi = props.webSocketApi;
this.routeKey = props.routeKey;
const config = props.integration.bind({
route: this,
scope: this,
});
const integration = props.webSocketApi._addIntegration(this, config);
const route = new apigatewayv2_generated_1.CfnRoute(this, 'Resource', {
apiId: props.webSocketApi.apiId,
routeKey: props.routeKey,
target: `integrations/${integration.integrationId}`,
authorizerId: props.authorizerId, // <========================== Added this
authorizationType: props.authorizationType, // <=================== Added this
});
this.routeId = route.ref;
}
} |
Also there is a sample for adding Lambda authorizer to a WebSocket API written in TypeScript. |
+1 |
closes #13869 By this PR, you will be able to enable WebSocket authorizer as the below code: ```ts const integration = new LambdaWebSocketIntegration({ handler, }); const authorizer = new WebSocketLambdaAuthorizer('Authorizer', authHandler); new WebSocketApi(stack, 'WebSocketApi', { connectRouteOptions: { integration, authorizer, }, }); ``` ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
|
closes aws#13869 By this PR, you will be able to enable WebSocket authorizer as the below code: ```ts const integration = new LambdaWebSocketIntegration({ handler, }); const authorizer = new WebSocketLambdaAuthorizer('Authorizer', authHandler); new WebSocketApi(stack, 'WebSocketApi', { connectRouteOptions: { integration, authorizer, }, }); ``` ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@wakeupmh You have to create a Lambda function to verify JWTs (implementation is very simple thanks to aws-jwt-veryfy lib), and use the function as a Lambda authorizer. |
@tmokmss in my case I translated to use L1 constructs anyway everything works fine, thanks a lot by your attention |
Please allow us to set a Lambda authorizer for a WebSocket API.
Currently we cannot set it because there's no interface for it.
Use Case
Restrict access to a WebSocket API by cognito auth or other auth method.
Proposed Solution
Setting an authorizer for a WebSocket API is simple.
You must only set
authorizationType
andauthorizerId
when creating aCfnRoute
to$connect
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigatewayv2-route.html
Because you can only set authorizer for
$connect
route.And there's only one authorizer type; LambdaAuthorizer.
Other
This is a 🚀 Feature Request
The text was updated successfully, but these errors were encountered: