Skip to main content

Authorizers for AWS APIGateway V2

Project description

AWS APIGatewayv2 Authorizers

---

cdk-constructs: Experimental

The APIs of higher level constructs in this module are experimental and under active development. They are subject to non-backward compatible changes or removal in any future version. These are not subject to the Semantic Versioning model and breaking changes will be announced in the release notes. This means that while you may use them, you may need to update your source code when upgrading to a newer version of this package.


Table of Contents

Introduction

API Gateway supports multiple mechanisms for controlling and managing access to your HTTP API. They are mainly classified into Lambda Authorizers, JWT authorizers and standard AWS IAM roles and policies. More information is available at Controlling and managing access to an HTTP API.

HTTP APIs

Access control for Http Apis is managed by restricting which routes can be invoked via.

Authorizers, and scopes can either be applied to the api, or specifically for each route.

Default Authorization

When using default authorization, all routes of the api will inherit the configuration.

In the example below, all routes will require the manage:books scope present in order to invoke the integration.

# Example automatically generated. See https://github.com/aws/jsii/issues/826
from aws_cdk.aws_apigatewayv2_authorizers import HttpJwtAuthorizer


authorizer = HttpJwtAuthorizer(
    jwt_audience=["3131231"],
    jwt_issuer="https://test.us.auth0.com"
)

api = apigwv2.HttpApi(self, "HttpApi",
    default_authorizer=authorizer,
    default_authorization_scopes=["manage:books"]
)

Route Authorization

Authorization can also configured for each Route. When a route authorization is configured, it takes precedence over default authorization.

The example below showcases default authorization, along with route authorization. It also shows how to remove authorization entirely for a route.

  • GET /books and GET /books/{id} use the default authorizer settings on the api
  • POST /books will require the [write:books] scope
  • POST /login removes the default authorizer (unauthenticated route)
# Example automatically generated. See https://github.com/aws/jsii/issues/826
from aws_cdk.aws_apigatewayv2_authorizers import HttpJwtAuthorizer
from aws_cdk.aws_apigatewayv2_integrations import HttpProxyIntegration


authorizer = HttpJwtAuthorizer(
    jwt_audience=["3131231"],
    jwt_issuer="https://test.us.auth0.com"
)

api = apigwv2.HttpApi(self, "HttpApi",
    default_authorizer=authorizer,
    default_authorization_scopes=["read:books"]
)

api.add_routes(
    integration=HttpProxyIntegration(
        url="https://get-books-proxy.myproxy.internal"
    ),
    path="/books",
    methods=[apigwv2.HttpMethod.GET]
)

api.add_routes(
    integration=HttpProxyIntegration(
        url="https://get-books-proxy.myproxy.internal"
    ),
    path="/books/{id}",
    methods=[apigwv2.HttpMethod.GET]
)

api.add_routes(
    integration=HttpProxyIntegration(
        url="https://get-books-proxy.myproxy.internal"
    ),
    path="/books",
    methods=[apigwv2.HttpMethod.POST],
    authorization_scopes=["write:books"]
)

api.add_routes(
    integration=HttpProxyIntegration(
        url="https://get-books-proxy.myproxy.internal"
    ),
    path="/login",
    methods=[apigwv2.HttpMethod.POST],
    authorizer=apigwv2.HttpNoneAuthorizer()
)

JWT Authorizers

JWT authorizers allow the use of JSON Web Tokens (JWTs) as part of OpenID Connect and OAuth 2.0 frameworks to allow and restrict clients from accessing HTTP APIs.

When configured, API Gateway validates the JWT submitted by the client, and allows or denies access based on its content.

The location of the token is defined by the identitySource which defaults to the http Authorization header. However it also supports a number of other options. It then decodes the JWT and validates the signature and claims, against the options defined in the authorizer and route (scopes). For more information check the JWT Authorizer documentation.

Clients that fail authorization are presented with either 2 responses:

  • 401 - Unauthorized - When the JWT validation fails
  • 403 - Forbidden - When the JWT validation is successful but the required scopes are not met
# Example automatically generated. See https://github.com/aws/jsii/issues/826
from aws_cdk.aws_apigatewayv2_authorizers import HttpJwtAuthorizer
from aws_cdk.aws_apigatewayv2_integrations import HttpProxyIntegration


authorizer = HttpJwtAuthorizer(
    jwt_audience=["3131231"],
    jwt_issuer="https://test.us.auth0.com"
)

api = apigwv2.HttpApi(self, "HttpApi")

api.add_routes(
    integration=HttpProxyIntegration(
        url="https://get-books-proxy.myproxy.internal"
    ),
    path="/books",
    authorizer=authorizer
)

User Pool Authorizer

User Pool Authorizer is a type of JWT Authorizer that uses a Cognito user pool and app client to control who can access your Api. After a successful authorization from the app client, the generated access token will be used as the JWT.

Clients accessing an API that uses a user pool authorizer must first sign in to a user pool and obtain an identity or access token. They must then use this token in the specified identitySource for the API call. More information is available at using Amazon Cognito user pools as authorizer.

# Example automatically generated. See https://github.com/aws/jsii/issues/826
import aws_cdk.aws_cognito as cognito
from aws_cdk.aws_apigatewayv2_authorizers import HttpUserPoolAuthorizer
from aws_cdk.aws_apigatewayv2_integrations import HttpProxyIntegration


user_pool = cognito.UserPool(self, "UserPool")
user_pool_client = user_pool.add_client("UserPoolClient")

authorizer = HttpUserPoolAuthorizer(
    user_pool=user_pool,
    user_pool_clients=[user_pool_client]
)

api = apigwv2.HttpApi(self, "HttpApi")

api.add_routes(
    integration=HttpProxyIntegration(
        url="https://get-books-proxy.myproxy.internal"
    ),
    path="/books",
    authorizer=authorizer
)

Lambda Authorizers

Lambda authorizers use a Lambda function to control access to your HTTP API. When a client calls your API, API Gateway invokes your Lambda function and uses the response to determine whether the client can access your API.

Lambda authorizers depending on their response, fall into either two types - Simple or IAM. You can learn about differences here.

# Example automatically generated. See https://github.com/aws/jsii/issues/826
from aws_cdk.aws_apigatewayv2_authorizers import HttpLambdaAuthorizer, HttpLambdaResponseType
from aws_cdk.aws_apigatewayv2_integrations import HttpProxyIntegration

# This function handles your auth logic
# auth_handler is of type Function


authorizer = HttpLambdaAuthorizer(
    authorizer_name="lambda-authorizer",
    response_types=[HttpLambdaResponseType.SIMPLE],  # Define if returns simple and/or iam response
    handler=auth_handler
)

api = apigwv2.HttpApi(self, "HttpApi")

api.add_routes(
    integration=HttpProxyIntegration(
        url="https://get-books-proxy.myproxy.internal"
    ),
    path="/books",
    authorizer=authorizer
)

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

File details

Details for the file aws-cdk.aws-apigatewayv2-authorizers-alpha-2.0.0a6.tar.gz.

File metadata

  • Download URL: aws-cdk.aws-apigatewayv2-authorizers-alpha-2.0.0a6.tar.gz
  • Upload date:
  • Size: 301.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.6.5

File hashes

Hashes for aws-cdk.aws-apigatewayv2-authorizers-alpha-2.0.0a6.tar.gz
Algorithm Hash digest
SHA256 35f9be6da2952f617205c068c7ff84a2e159ac7fed4bd9dd06619d8c1a673507
MD5 9d0147df3e68cc319624468d8c30277a
BLAKE2b-256 6ffc1417a067d2894abe60ee88a7da1da9592bd2e09e4ad7561ecf0bac755229

See more details on using hashes here.

File details

Details for the file aws_cdk.aws_apigatewayv2_authorizers_alpha-2.0.0a6-py3-none-any.whl.

File metadata

File hashes

Hashes for aws_cdk.aws_apigatewayv2_authorizers_alpha-2.0.0a6-py3-none-any.whl
Algorithm Hash digest
SHA256 99e8ffeff03f66be93335993bdbd678304b40b6e2ad2d89aceb81b2705a7beba
MD5 0a8578144e8f57d58d435dc2b6cf86de
BLAKE2b-256 312716deaed481a60316a4b7655eaff59ab5c482ce5dfe606feafeeb17268f49

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page