|
| 1 | +import { Resource } from '@aws-cdk/core'; |
| 2 | +import { Construct } from 'constructs'; |
| 3 | +import { CfnAuthorizer } from '../apigatewayv2.generated'; |
| 4 | + |
| 5 | +import { IAuthorizer } from '../common'; |
| 6 | +import { IWebSocketApi } from './api'; |
| 7 | +import { IWebSocketRoute } from './route'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Supported Authorizer types |
| 11 | + */ |
| 12 | +export enum WebSocketAuthorizerType { |
| 13 | + /** Lambda Authorizer */ |
| 14 | + LAMBDA = 'REQUEST', |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Properties to initialize an instance of `WebSocketAuthorizer`. |
| 19 | + */ |
| 20 | +export interface WebSocketAuthorizerProps { |
| 21 | + /** |
| 22 | + * Name of the authorizer |
| 23 | + * @default - id of the WebSocketAuthorizer construct. |
| 24 | + */ |
| 25 | + readonly authorizerName?: string |
| 26 | + |
| 27 | + /** |
| 28 | + * WebSocket Api to attach the authorizer to |
| 29 | + */ |
| 30 | + readonly webSocketApi: IWebSocketApi |
| 31 | + |
| 32 | + /** |
| 33 | + * The type of authorizer |
| 34 | + */ |
| 35 | + readonly type: WebSocketAuthorizerType; |
| 36 | + |
| 37 | + /** |
| 38 | + * The identity source for which authorization is requested. |
| 39 | + * @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigatewayv2-authorizer.html#cfn-apigatewayv2-authorizer-identitysource |
| 40 | + */ |
| 41 | + readonly identitySource: string[]; |
| 42 | + |
| 43 | + /** |
| 44 | + * The authorizer's Uniform Resource Identifier (URI). |
| 45 | + * |
| 46 | + * For REQUEST authorizers, this must be a well-formed Lambda function URI. |
| 47 | + * |
| 48 | + * @default - required for Request authorizer types |
| 49 | + */ |
| 50 | + readonly authorizerUri?: string; |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * An authorizer for WebSocket APIs |
| 55 | + */ |
| 56 | +export interface IWebSocketAuthorizer extends IAuthorizer { |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Reference to an WebSocket authorizer |
| 61 | + */ |
| 62 | +export interface WebSocketAuthorizerAttributes { |
| 63 | + /** |
| 64 | + * Id of the Authorizer |
| 65 | + */ |
| 66 | + readonly authorizerId: string |
| 67 | + |
| 68 | + /** |
| 69 | + * Type of authorizer |
| 70 | + * |
| 71 | + * Possible values are: |
| 72 | + * - CUSTOM - Lambda Authorizer |
| 73 | + * - NONE - No Authorization |
| 74 | + */ |
| 75 | + readonly authorizerType: string |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * An authorizer for WebSocket Apis |
| 80 | + * @resource AWS::ApiGatewayV2::Authorizer |
| 81 | + */ |
| 82 | +export class WebSocketAuthorizer extends Resource implements IWebSocketAuthorizer { |
| 83 | + /** |
| 84 | + * Import an existing WebSocket Authorizer into this CDK app. |
| 85 | + */ |
| 86 | + public static fromWebSocketAuthorizerAttributes(scope: Construct, id: string, attrs: WebSocketAuthorizerAttributes): IWebSocketRouteAuthorizer { |
| 87 | + class Import extends Resource implements IWebSocketRouteAuthorizer { |
| 88 | + public readonly authorizerId = attrs.authorizerId; |
| 89 | + public readonly authorizerType = attrs.authorizerType; |
| 90 | + |
| 91 | + public bind(): WebSocketRouteAuthorizerConfig { |
| 92 | + return { |
| 93 | + authorizerId: attrs.authorizerId, |
| 94 | + authorizationType: attrs.authorizerType, |
| 95 | + }; |
| 96 | + } |
| 97 | + } |
| 98 | + return new Import(scope, id); |
| 99 | + } |
| 100 | + |
| 101 | + public readonly authorizerId: string; |
| 102 | + |
| 103 | + constructor(scope: Construct, id: string, props: WebSocketAuthorizerProps) { |
| 104 | + super(scope, id); |
| 105 | + |
| 106 | + if (props.type === WebSocketAuthorizerType.LAMBDA && !props.authorizerUri) { |
| 107 | + throw new Error('authorizerUri is mandatory for Lambda authorizers'); |
| 108 | + } |
| 109 | + |
| 110 | + const resource = new CfnAuthorizer(this, 'Resource', { |
| 111 | + name: props.authorizerName ?? id, |
| 112 | + apiId: props.webSocketApi.apiId, |
| 113 | + authorizerType: props.type, |
| 114 | + identitySource: props.identitySource, |
| 115 | + authorizerUri: props.authorizerUri, |
| 116 | + }); |
| 117 | + |
| 118 | + this.authorizerId = resource.ref; |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * Input to the bind() operation, that binds an authorizer to a route. |
| 124 | + */ |
| 125 | +export interface WebSocketRouteAuthorizerBindOptions { |
| 126 | + /** |
| 127 | + * The route to which the authorizer is being bound. |
| 128 | + */ |
| 129 | + readonly route: IWebSocketRoute; |
| 130 | + /** |
| 131 | + * The scope for any constructs created as part of the bind. |
| 132 | + */ |
| 133 | + readonly scope: Construct; |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + * Results of binding an authorizer to an WebSocket route. |
| 138 | + */ |
| 139 | +export interface WebSocketRouteAuthorizerConfig { |
| 140 | + /** |
| 141 | + * The authorizer id |
| 142 | + * |
| 143 | + * @default - No authorizer id (useful for AWS_IAM route authorizer) |
| 144 | + */ |
| 145 | + readonly authorizerId?: string; |
| 146 | + |
| 147 | + /** |
| 148 | + * The type of authorization |
| 149 | + * |
| 150 | + * Possible values are: |
| 151 | + * - CUSTOM - Lambda Authorizer |
| 152 | + * - NONE - No Authorization |
| 153 | + */ |
| 154 | + readonly authorizationType: string; |
| 155 | +} |
| 156 | + |
| 157 | +/** |
| 158 | + * An authorizer that can attach to an WebSocket Route. |
| 159 | + */ |
| 160 | +export interface IWebSocketRouteAuthorizer { |
| 161 | + /** |
| 162 | + * Bind this authorizer to a specified WebSocket route. |
| 163 | + */ |
| 164 | + bind(options: WebSocketRouteAuthorizerBindOptions): WebSocketRouteAuthorizerConfig; |
| 165 | +} |
| 166 | + |
| 167 | +/** |
| 168 | + * Explicitly configure no authorizers on specific WebSocket API routes. |
| 169 | + */ |
| 170 | +export class WebSocketNoneAuthorizer implements IWebSocketRouteAuthorizer { |
| 171 | + public bind(_: WebSocketRouteAuthorizerBindOptions): WebSocketRouteAuthorizerConfig { |
| 172 | + return { |
| 173 | + authorizationType: 'NONE', |
| 174 | + }; |
| 175 | + } |
| 176 | +} |
0 commit comments