我想创建一个使用AWS Cognito进行身份验证的AWS无服务器应用程序。我的出发点是Visual Studio2017中C#的AWS Serverless Application with Tests (.NET Core)
模板。部署后,该模板将创建一些Lambda函数并设置AWS API Gateway,以便我可以通过REST请求连接到Lambda函数。这是可行的。
我已经在AWS Cognito中创建了一个用户池,以及一个允许用户使用AWS Cognito登录的javascript客户端(一个单页面应用程序)。在javascript客户端中,我能够连接到AWS Cognito,并以JWT的形式获取id、访问和刷新令牌。我还可以在Authorization: Bearer eyblablabla...
报头中将这些令牌发送到后端( AWS无服务器应用程序)。因此,javascript客户端身份验证和AWS Cognito设置似乎可以正常工作。我还在AWS API Gateway中设置了一个指向AWS Cognito用户池的授权器。
我的问题如下:后端似乎不知道authorization头。在检查请求时,我没有得到该用户的任何声明。我对获得sub
声明特别感兴趣,这样我就可以识别用户。另外,我希望应用程序接口网关为特定的方法自动发送Unauthorized
响应,但我不知道如何设置。
作为Lambda函数签名的一部分,我得到了一个APIGatewayProxyRequest
请求对象。显然,request.RequestContext.Authorizer.Claims
应该包含用户声明,但.Authorizer
为空。
我能够从读取请求头的javascript客户端获得我发送的任何JWT,因此我可以解析令牌来获得用户声明。但是我想一定是我的设置出了问题,因为.Authorizer
没有被填充。
到目前为止,我找到的建议只涉及serverless.yml
或Swagger模板文件中的内容,这两个文件都不是AWS无服务器应用程序VS2017模板的一部分。相反,我有一个serverless.template
JSON文件,没有明显的方法可以向该文件添加身份验证/安全设置。
到目前为止,我的代码与VS2017中的AWS无服务器应用程序模板完全相同。
我们将非常感谢您的帮助。
发布于 2018-12-13 21:29:34
您需要在API方法集成请求中启用使用Lambda代理集成。然后,您的APIGatewayProxyRequest对象将如下所示:
{
"Resource": "/test",
"Path": "/test",
"HttpMethod": "GET",
"Headers": {
"Accept": "*/*",
"accept-encoding": "gzip, deflate",
"Authorization": "your token",
"cache-control": "no-cache",
"CloudFront-Forwarded-Proto": "https",
"CloudFront-Is-Desktop-Viewer": "true",
"CloudFront-Is-Mobile-Viewer": "false",
"CloudFront-Is-SmartTV-Viewer": "false",
"CloudFront-Is-Tablet-Viewer": "false",
"CloudFront-Viewer-Country": "US",
"Host": "xxxxxxx.execute-api.us-east-1.amazonaws.com",
"Postman-Token": "08820d50-c5d4-498a-bfee-c76994bb91f1",
"User-Agent": "PostmanRuntime/7.4.0",
"Via": "1.1 dd169cfdbbafbb3da513bede6bc6640e.cloudfront.net (CloudFront)",
"X-Amz-Cf-Id": "89ftx9aaVK0k2KOFu-5QESLXzGUGAw17gNCCY03in-hF2hd-LvRhIg==",
"X-Amzn-Trace-Id": "Root=1-5c125bb9-1e8b9fea8d1beb20147a24d2",
"X-Forwarded-For": "50.196.109.21, 70.132.33.133",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https"
},
"QueryStringParameters": null,
"PathParameters": null,
"StageVariables": null,
"RequestContext": {
"Path": "/test_oauth/token",
"AccountId": "xxxxxxxxxxxxx",
"ResourceId": "luy67k",
"Stage": "test_oauth",
"RequestId": "5455133d-fed9-11e8-8f41-ef35907ced2d",
"Identity": {
"CognitoIdentityPoolId": null,
"AccountId": null,
"CognitoIdentityId": null,
"Caller": null,
"ApiKey": null,
"SourceIp": "50.196.109.21",
"CognitoAuthenticationType": null,
"CognitoAuthenticationProvider": null,
"UserArn": null,
"UserAgent": "PostmanRuntime/7.4.0",
"User": null
},
"ResourcePath": "/token",
"HttpMethod": "GET",
"ApiId": "8xxg9ez961",
"Authorizer": {
"claims": {
"sub": "4560ac4b-54a0-4184-8831-e3cb2583726b",
"aud": "xxxxxxxxxxxxxxxx",
"email_verified": "false",
"event_id": "467633ad-fed9-11e8-88ff-25be6cd15697",
"token_use": "id",
"custom:ApplicationId": "12345",
"auth_time": "1544706978",
"iss": "https://cognito-idp.us-east-1.amazonaws.com/us-east-xxxxxxxxx",
"cognito:username": "username",
"exp": "Thu Dec 13 14:16:18 UTC 2018",
"iat": "Thu Dec 13 13:16:18 UTC 2018",
"email": "user@email.com"
}
}
},
"Body": null,
"IsBase64Encoded": false
}
请注意,如果您的API Gateway设置为使用id令牌与访问令牌(这是在您的API Gateway设置中使用或不使用自定义OAuth作用域的区别),您会发现得到不同的值。您还会发现结果取决于用户池应用程序客户端的设置-启用了哪些允许的Oauth范围,以及为您的应用程序客户端启用了哪些属性。
https://stackoverflow.com/questions/51183625
复制相似问题