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
Try OAuth 2.0 Flow
-
Issue an access token
Authlete does not host an authorization server, so issue an access token for your service to test the integration. The quickest way is Authlete’s Create Access Token API (
/api/{Service ID}/auth/token/create), which mints an access token without running a full OAuth flow. Replace<Service ID>,<Service Access Token>and<Client ID>with your own values, and setsubjectandscopesto values supported by your service.
accessToken field of the response.
- 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.