Overview
This document describes how to protect a Web API implemented using Amazon API Gateway + AWS Lambda with an OAuth 2.0 access token.2016-Apr-6: Amazon API Gateway introduced Custom Authorizer on Feb 11, 2016. It should be utilized. See our new document Amazon API Gateway Custom Authorizer + OAuth.
Prerequisites
The following sections assume:- You have a lambda function
GetHelloWorldthat returns{"Hello":"World"}. - You have
/mydemoresourceresource that supportsGETmethod on Amazon API Gateway. - You have a service in Authlete. A service is created automatically on sign-up and you can use it.
- You have a client application in Authlete. A client application is created automatically on sign-up and you can use it.
Quick Guide
Setup
- [API Gateway] Complete steps in Getting Started with Amazon API Gateway
- [Authlete] Complete steps in Getting Started
- [Lambda] Renew
GetHelloWorldlambda function
GetHelloWorld lambda function.
access_token as a query parameter to GET /mydemoresource
6. [API Gateway] [Integration Request] Set a template as is shown below
- [Amazon API Gateway] [Method Response] Add 400, 401, 403 and 500 as HTTP status codes
-
[Amazon API Gateway] [Integration Response] Set mappings as shown below
Lambda Error Regex Method response status BAD_REQUEST:* 400 UNAUTHORIZED:.* 401 FORBIDDEN:.* 403 INTERNAL_SERVER_ERROR:.* 500
Try OAuth 2.0 Flow
- Make an authorization request Access the URL below with your browser. An authorization page will appear. Don’t forget to replace your-service-api-key and your-client-id.
- Authorize the client app In the authorization page, input the API key and the API secret of your Authlete service and press “Authorize” button. You will get an access token. You can see the API key and the API secret of the service at https://so.authlete.com/services/service-api-key.
- Access
/mydemoresourceendpoint with an access token Access the URL below with your browser. Don’t forget to replace your-api-id, region-id and your-access-token with your own.