我想使用 boto3 和 python 为 AWS Cognito 创建/计算 SECRET_HASH。这将合并到我的 fork 中warrant.
我将我的 Cognito 应用程序客户端配置为使用app client secret
。但是,这破坏了以下代码。
def renew_access_token(self):
"""
Sets a new access token on the User using the refresh token.
NOTE:
Does not work if "App client secret" is enabled. 'SECRET_HASH' is needed in AuthParameters.
'SECRET_HASH' requires HMAC calculations.
Does not work if "Device Tracking" is turned on.
https://stackoverflow.com/a/40875783/1783439
'DEVICE_KEY' is needed in AuthParameters. See AuthParameters section.
https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_InitiateAuth.html
"""
refresh_response = self.client.initiate_auth(
ClientId=self.client_id,
AuthFlow='REFRESH_TOKEN',
AuthParameters={
'REFRESH_TOKEN': self.refresh_token
# 'SECRET_HASH': How to generate this?
},
)
self._set_attributes(
refresh_response,
{
'access_token': refresh_response['AuthenticationResult']['AccessToken'],
'id_token': refresh_response['AuthenticationResult']['IdToken'],
'token_type': refresh_response['AuthenticationResult']['TokenType']
}
)
当我运行此程序时,我收到以下异常:
botocore.errorfactory.NotAuthorizedException:
An error occurred (NotAuthorizedException) when calling the InitiateAuth operation:
Unable to verify secret hash for client <client id echoed here>.
这个答案通知我使用 cognito 客户端密钥需要 SECRET_HASH。
The AWS API 参考文档AuthParameters 部分说明以下内容:
对于 REFRESH_TOKEN_AUTH/REFRESH_TOKEN:USERNAME(必需)、SECRET_HASH
(如果应用程序客户端配置了客户端密钥,则为必需),
REFRESH_TOKEN(必需)、DEVICE_KEY
The boto3 文档声明 SECRET_HASH 是
使用以下方法计算的密钥哈希消息身份验证代码 (HMAC)
用户池客户端的密钥和用户名加上客户端 ID
消息。
该文档解释了需要什么,但没有解释如何实现这一点。