constructor()

in source/patterns/@aws-solutions-constructs/aws-cognito-apigateway-lambda/lib/index.ts [82:122]


  constructor(scope: Construct, id: string, props: CognitoToApiGatewayToLambdaProps) {
    super(scope, id);
    defaults.CheckProps(props);

    // This Construct requires that the auth type be COGNITO regardless of what is specified in the props
    if (props.apiGatewayProps) {
      if (props.apiGatewayProps.defaultMethodOptions === undefined) {
        props.apiGatewayProps.defaultMethodOptions = {
          authorizationType: api.AuthorizationType.COGNITO,
        };
      } else if (props.apiGatewayProps?.defaultMethodOptions.authorizationType === undefined) {
        props.apiGatewayProps.defaultMethodOptions.authorizationType = api.AuthorizationType.COGNITO;
      } else if (props.apiGatewayProps?.defaultMethodOptions.authorizationType !== 'COGNITO_USER_POOLS') {
        defaults.printWarning('Overriding Authorization type to be AuthorizationType.COGNITO');
        props.apiGatewayProps.defaultMethodOptions.authorizationType = api.AuthorizationType.COGNITO;
      }
    }

    if (props.apiGatewayProps && (typeof props.apiGatewayProps.proxy !== 'undefined') && (props.apiGatewayProps.proxy === false)) {
      defaults.printWarning('For non-proxy API, addAuthorizers() method must be called after all the resources and methods for API are fuly defined. Not calling addAuthorizers() will result in API methods NOT protected by Cognito.');
    }

    this.lambdaFunction = defaults.buildLambdaFunction(this, {
      existingLambdaObj: props.existingLambdaObj,
      lambdaFunctionProps: props.lambdaFunctionProps
    });
    [this.apiGateway, this.apiGatewayCloudWatchRole, this.apiGatewayLogGroup] =
      defaults.GlobalLambdaRestApi(this, this.lambdaFunction, props.apiGatewayProps, props.logGroupProps);
    this.userPool = defaults.buildUserPool(this, props.cognitoUserPoolProps);
    this.userPoolClient = defaults.buildUserPoolClient(this, this.userPool, props.cognitoUserPoolClientProps);

    this.apiGatewayAuthorizer = new api.CfnAuthorizer(this, 'CognitoAuthorizer', {
      restApiId: this.apiGateway.restApiId,
      type: 'COGNITO_USER_POOLS',
      providerArns: [this.userPool.userPoolArn],
      identitySource: "method.request.header.Authorization",
      name: "authorizer"
    });

    this.addAuthorizers();
  }