我们正在尝试设置一个桌面应用程序,需要多因素身份验证登录才能连接到亚马逊的 API 网关。为此,我们使用 Amazon Cognito 进行身份验证,旨在将其与 API 网关集成。设置的 Cognito 和 API 网关部分都是独立工作的。问题在于添加安全性。
按照此处的文档进行操作:https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-integrate-with-cognito.html,我们已将 API 网关配置为使用授权者和令牌,通过 Cognito 成功登录时收到。在AWS控制台测试授权者时,收到的token有效,认证通过。
尝试从桌面应用程序执行此操作时会出现问题。经过几个小时的搜寻文档和无休止的谷歌搜索后,我们仍然距离让它发挥作用还很远。
文档说我们必须将令牌与“授权”密钥一起放入请求的标头中。我们这样做,但只是不断收到 HTTP 401(未经授权)。
发送的请求标头如下(第一个黑色矩形是 api 端点,第二个是我们传入的令牌):
使用 3rd 方应用程序(例如邮递员)时会看到相同的结果。
根据以下文档:https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-troubleshooting-jwt.html,我们尝试了curl命令:
curl -v -H "Authorization: <token>" <endpoint>
但只要继续收到回复即可:
* Trying <ip>...
* TCP_NODELAY set
* Connected to <endpoint> port 443 (#0)
* schannel: SSL/TLS connection with <endpoint> port 443 (step 1/3)
* schannel: checking server certificate revocation
* schannel: sending initial handshake data: sending 217 bytes...
* schannel: sent initial handshake data: sent 217 bytes
* schannel: SSL/TLS connection with <endpoint> port 443 (step 2/3)
* schannel: failed to receive handshake, need more data
* schannel: SSL/TLS connection with <endpoint> port 443 (step 2/3)
* schannel: encrypted data got 4096
* schannel: encrypted data buffer: offset 4096 length 4096
* schannel: encrypted data length: 4030
* schannel: encrypted data buffer: offset 4030 length 4096
* schannel: received incomplete message, need more data
* schannel: SSL/TLS connection with <endpoint> port 443 (step 2/3)
* schannel: encrypted data got 1024
* schannel: encrypted data buffer: offset 5054 length 5054
* schannel: encrypted data length: 180
* schannel: encrypted data buffer: offset 180 length 5054
* schannel: received incomplete message, need more data
* schannel: SSL/TLS connection with <endpoint> port 443 (step 2/3)
* schannel: encrypted data got 167
* schannel: encrypted data buffer: offset 347 length 5054
* schannel: next InitializeSecurityContext failed: Unknown error (0x80092012) - The revocation function was unable to check revocation for the certificate.
* Closing connection 0
* schannel: shutting down SSL/TLS connection with <endpoint> port 443
* schannel: clear security context handle
curl: (35) schannel: next InitializeSecurityContext failed: Unknown error (0x80092012) - The revocation function was unable to check revocation for the certificate.
有人知道我们做错了什么吗?
对于将来遇到此问题的任何人。该问题与传递的令牌无关。检查令牌后,我们发现范围不匹配。
我们尝试在 Cognito 中使用自定义范围,但由于某种原因,自定义范围在桌面应用程序中实际上不起作用(范围一直默认为 aws.cognito.signin.user.admin)。
无论如何,长话短说。我们现在使用 lambda 授权者来验证从我们的 cognito 用户池获取的 ID jwt 令牌。然后,我们根据发出请求的用户组分配权限。