Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authorize AWS API Gateway with either API Key or Authorizer

In AWS API Gateway,
- We can set up a resource to reqiure API Key for access.
- We can also set up another resource to require Authorization (e.g. JWT token, handled via a lambda function or AWS Cognito).

The question: can we configure a resource to be accessible in either of the above two situations? Currently, if we enable "API Key Required" and "Authorization" simultaneously, the request needs both the API Key and the Authorization. We were hoping for it to pass with only one of the two.

Hack/workaround: Create two copies of the same resource, and authorize each separately, one with API Key and the other one with an authorizer.

like image 403
Amir Avatar asked Jun 03 '20 00:06

Amir


People also ask

How do I pass API key to API gateway?

Sign in to the API Gateway console. Choose an existing API or create a new one. In the primary navigation pane, choose Settings under the chosen or newly created API. Under the API Key Source section in the Settings pane, choose HEADER or AUTHORIZER from the drop-down list.

Can API Gateway have multiple authorizers?

An API can have multiple custom authorizers and each method within your API can use a different authorizer. For example, the POST method for the /login resource can use a different authorizer than the GET method for the /pets resource.

How do I authenticate API gateway?

API Gateway supports multiple authentication methods that are suited to different applications and use cases. API Gateway uses the authentication method that you specify in your service configuration to validate incoming requests before passing them to your API backend.

What is authorizer in AWS API gateway?

The AWS::ApiGateway::Authorizer resource creates an authorization layer that API Gateway activates for methods that have authorization enabled. API Gateway activates the authorizer when a client calls those methods. What is AWS API Gateway Authorizer? AWS API Gateway Authorizer is a resource for API Gateway of Amazon Web Service.

Why does API gateway cache the policy returned by the authorizer?

Custom authorizers must return AWS Identity and Access Management (IAM) policies. These policies are used to authorize the request. If the policy returned by the authorizer is valid, API Gateway caches the returned policy associated with the incoming token for up to 1 hour so that your Lambda function doesn’t need to be invoked again.

How can I secure API gateway HTTP API endpoints with JWT authorizers?

AWS enabled the ability to manage access to an HTTP API in API Gateway in multiple ways: with Lambda authorizers, IAM roles and policies, and JWT authorizers. This post demonstrated how you can secure API Gateway HTTP API endpoints with JWT authorizers. We configured a JWT authorizer using Amazon Cognito as the identity provider (IdP).

Is it possible to enable both API key and authorization?

Currently, if we enable "API Key Required" and "Authorization" simultaneously, the request needs both the API Key and the Authorization. We were hoping for it to pass with only one of the two. Hack/workaround: Create two copies of the same resource, and authorize each separately, one with API Key and the other one with an authorizer.


2 Answers

Let authorizer generate/map the API key for you

You have a Lambda authorizer return the API key as part of the authorization response. For more information on the authorization response, see Output from an Amazon API Gateway Lambda authorizer.

Pros:

  • Single end-point

  • API key is more for usage plan than authorization. Keep it that way.

Cons:

  • Authorizer will run on each request. Which cost money
like image 83
qkhanhpro Avatar answered Sep 26 '22 01:09

qkhanhpro


Authentication, Identification, Authorization are intertwined concepts. As I got more educated on Auth, here is my answer:

  • API Keys are used for project/application identification and authorization
  • JWT are used for user authentication and authorization.
  • API Key is on project/application scope and JWT is on user scope. In other words, API Key only identifies the application, not the user of the application.

Accordingly, it makes sense not to authorize the same endpoint with both JWT and API Key as it would reduce the governance granularity for users and applications. But, if you have a usecase that requires that type of authorization, the suggested workaround could work.

like image 41
Amir Avatar answered Sep 22 '22 01:09

Amir