7

In a sample AWS serverless architecture, there would be S3 hosting a static website, calling API gateway endpoints via javascript, which in turn invokes Lambda functions.

How to authenticate against the AWS API gateway without making the credentials public by storing them in publicly accessible javascript source served from S3?

(not asking about backend credentials, these are easily stored (and encyprted) in environment variables accessible to the Lambda functions)

Marek
  • 10,307
  • 8
  • 70
  • 106
  • 2
    Your users would typically authenticate using Cognito or some other auth backend, exchanging their credentials for a JWT token or equivalent that is used to authenticate to API Gateway. – jarmod Nov 06 '18 at 16:07
  • Thank you for your very quick and to the point answer. Yes, AWS Cognito is the part I missed. Why a comment and not an answer though? – Marek Nov 06 '18 at 16:08
  • @jarmod however how about the public (non-logged in) portion of a website making API calls? – Marek Nov 06 '18 at 16:10
  • Have added answer plus info about unauthenticated API access. – jarmod Nov 06 '18 at 20:11

3 Answers3

2

Your users would typically authenticate using Cognito or some other auth backend, exchanging their credentials for a JWT token or equivalent that is used to authenticate to API Gateway. Here's an example of the steps involved.

It's also possible to support unauthenticated users with Cognito.

If there are situations in which you need API Gateway calls to be made without authentication, then see this response.

jarmod
  • 71,565
  • 16
  • 115
  • 122
1

You need to get a token from the client-side, using a third party identity provider, which you can pass to API Gateway. I personally use Auth0, which is free for up to two identity providers.

It's easy to integrate with your Single Page Application (Angular/React/Vue) to one or many Identity Providers, with good code examples.

It's also straightforward to integrate server-side validation of the auth tokens in API Gateway using a Custom Authenticator. Role Based Access can also be controlled using the Authorization Extension. The public part of the website can make API calls to API Endpoints that do not use a Custom Authenticator.

Here is a good guide from the Serverless Framework Website that shows Strategies for implementing user authentication in serverless applications, with a working example on GitHub.

Matt D
  • 3,289
  • 1
  • 15
  • 29
0

Using something like AWS Cognito is the best idea (without having to worry about managing your own authentication servers).

In AWS Cognito, you can simply give unauthenticated users access to the invoking the API. See the docs for more (here and here).

mostafazh
  • 4,144
  • 1
  • 20
  • 26