0

我正在使用 terraform 使用他们的 AWS gatewayv2 资源创建一个 websocket,例如aws_apigatewayv2_routeaws_apigatewayv2_authorizer

当我的授权 lambda 运行时,它"type": "REQUEST"通过 headers.Auth 从传入事件中获取一个令牌,它看起来像“Bearer eyJmaWOiQiI3Y...JTMjU2In0.eyJxpWIiOi...”(一个很长的字符串)。令牌是“Bearer”之后的部分。

该代码处理令牌以获取“孩子”等,并将其与从 cognito jwks.json 文件中检索到的密钥进行匹配(这是我从某处 AWS 网站获得的示例代码)。

代码流向“签名成功验证”点——到目前为止很棒!

问题是:授权者 lambda 应该返回什么?

成功验证签名的示例代码表明应该返回一个声明对象。看起来像这样:

claims:
{
    "sub": "2jjtzzzyyyxxx888g2pppp8sqqqqjagn",
    "token_use": "access",
    "scope": "transactions/post",
    "auth_time": 1596108906,
    "iss": "https://cognito-idp.us-east-1.amazonaws.com/us-east-1_tZfQltfya",
    "exp": 1596112506,
    "iat": 1596108906,
    "version": 2,
    "jti": "f55a0c1d-b9ac-3b2f-b8da-0ee93335c828",
    "client_id": "2ku7unsnkde8g1i9n8s2usjbgo"
}

其他示例代码表明应该返回一个 authReponse,如下所示:

authResponse: 
{
    "principalId": "xxxyyyzzz", // <--- I have tried various things here.
    "policyDocument": {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Action": "execute-api:Invoke",
                "Effect": "Allow",
                "Resource": [
                    "arn:aws:execute-api:us-east-1:11122223334444:n10gr0cw7m/test-stage/POST/*"
                ]
            }
        ]
    }
}

一些价值观已经改变以保护无辜者:-)

我已经尝试了很多事情,但是我发起请求的 wscat cli 命令要么得到要么Unexpected server response: 403--Unexpected server response: 500取决于我尝试的变体。

我也尝试过返回一个简单的{ "statusCode": 200 }对象——这也不起作用。

4

1 回答 1

1

Gateway API V2授权方 lambda 应该返回策略响应,但有一些修复。

这是一个带有注释的工作示例(同样,为了安全而更改了一些值):

{
  // "App client ID" from the App integration section of Cognito
  "principalId": "7p9f415hnxxbfbch17jnaenccs", 
  "policyDocument": {
    "Version": "2012-10-17",
    "Statement": [
      {
        "Action": [  // <--- this must be an array (a V2 change!)
          "execute-api:Invoke"
        ],
        "Effect": "Allow",
        "Resource": [
          "arn:aws:execute-api:us-east-1:111222233444:1c9kv22z8g/stage-dev/$connect"
        ]
      }
    ]
  },
  "context": null, // <-- optional values to pass to the route integration
  "usageIdentifierKey": null
}
于 2020-08-01T15:08:32.947 回答