Skip to main content

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 GetHelloWorld that returns {"Hello":"World"}.
  • You have /mydemoresource resource that supports GET method 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

  1. [API Gateway] Complete steps in Getting Started with Amazon API Gateway
  2. [Authlete] Complete steps in Getting Started
  3. [Lambda] Renew GetHelloWorld lambda function
Create a Deployment Package and upload it as the implementation of GetHelloWorld lambda function.
Download index.js and put it in this directory.
Create a ZIP file containing index.js and node_modules directory. 4. [Lambda] Edit the timeout value Increase the timeout value of the lambda function (e.g. to 30 sec). 5. [API Gateway] [Method Request] Add access_token as a query parameter to GET /mydemoresource 6. [API Gateway] [Integration Request] Set a template as is shown below
  1. [Amazon API Gateway] [Method Response] Add 400, 401, 403 and 500 as HTTP status codes
  2. [Amazon API Gateway] [Integration Response] Set mappings as shown below

Try OAuth 2.0 Flow

  1. 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 set subject and scopes to values supported by your service.
The issued access token is returned in the accessToken field of the response.
  1. Access /mydemoresource endpoint 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.
You will receive a JSON like below with HTTP status code 200 (OK).