

# 向 API Gateway Lambda 授权方的输入
<a name="api-gateway-lambda-authorizer-input"></a>

以下部分介绍从 API Gateway 到 Lambda 授权方的输入的格式。

## `TOKEN` 输入格式
<a name="w2aac15b9c23c25c19b5"></a>

 对于 `TOKEN` 类型的 Lambda 授权方（以前称为自定义授权方），您必须在为 API 配置授权方时指定自定义标头作为**令牌源**。API 客户端必须在传入请求中传递该标头中必需的授权令牌。在收到传入方法请求后，API Gateway 将从自定义标头中提取此令牌。随后，它将此令牌作为 Lambda 函数的 `authorizationToken` 对象的 `event` 属性传递，并将方法 ARN 作为 `methodArn` 属性传递：

```
{
    "type":"TOKEN",
    "authorizationToken":"{caller-supplied-token}",
    "methodArn":"arn:aws:execute-api:{regionId}:{accountId}:{apiId}/{stage}/{httpVerb}/[{resource}/[{child-resources}]]"
}
```

 在此示例中，`type` 属性指定授权方类型，后者是 `TOKEN` 授权方。`{caller-supplied-token}` 源自客户端请求中的授权标头，可以是任何字符串值。`methodArn` 是传入方法请求的 ARN，由 API Gateway 根据 Lambda 授权方配置填充。

## `REQUEST` 输入格式
<a name="w2aac15b9c23c25c19b7"></a>

对于 `REQUEST` 类型的 Lambda 授权方，API Gateway 会将请求参数作为 `event` 对象的一部分传递到授权方 Lambda 函数。请求参数包括标头、路径参数、查询字符串参数、阶段变量以及一些请求上下文变量。API 调用方可以设置路径参数、标头和查询字符串参数。API 开发人员必须在 API 部署期间设置阶段变量，并且 API Gateway 将在运行时提供请求上下文。

**注意**  
路径参数可作为请求参数传递到 Lambda 授权方函数，但它们不能作身份来源。

 以下示例显示针对带代理集成的 API 方法 (`REQUEST`) 的 `GET /request` 授权方的输入：

```
{
  "type": "REQUEST",
  "methodArn": "arn:aws:execute-api:us-east-1:123456789012:abcdef123/test/GET/request",
  "resource": "/request",
  "path": "/request",
  "httpMethod": "GET",
  "headers": {
    "X-AMZ-Date": "20170718T062915Z",
    "Accept": "*/*",
    "HeaderAuth1": "headerValue1",
    "CloudFront-Viewer-Country": "US",
    "CloudFront-Forwarded-Proto": "https",
    "CloudFront-Is-Tablet-Viewer": "false",
    "CloudFront-Is-Mobile-Viewer": "false",
    "User-Agent": "..."
  },
  "queryStringParameters": {
    "QueryString1": "queryValue1"
  },
  "pathParameters": {},
  "stageVariables": {
    "StageVar1": "stageValue1"
  },
  "requestContext": {
    "path": "/request",
    "accountId": "123456789012",
    "resourceId": "05c7jb",
    "stage": "test",
    "requestId": "...",
    "identity": {
      "apiKey": "...",
      "sourceIp": "...",
      "clientCert": {
        "clientCertPem": "CERT_CONTENT",
        "subjectDN": "www.example.com",
        "issuerDN": "Example issuer",
        "serialNumber": "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1",
        "validity": {
          "notBefore": "May 28 12:30:02 2019 GMT",
          "notAfter": "Aug  5 09:36:04 2021 GMT"
        }
      }
    },
    "resourcePath": "/request",
    "httpMethod": "GET",
    "apiId": "abcdef123"
  }
}
```

 `requestContext` 是键值对的映射，与 [\$1context](api-gateway-mapping-template-reference.md#context-variable-reference) 变量相对应。其结果与 API 相关。

 API Gateway 可能会向映射中添加新键。有关 Lambda 代理集成中的 Lambda 函数输入的更多信息，请参阅 [用于代理集成的 Lambda 函数的输入格式](set-up-lambda-proxy-integrations.md#api-gateway-simple-proxy-for-lambda-input-format)。