【发布时间】:2018-10-24 12:33:13
【问题描述】:
我在使用 Go 中的服务器端身份验证刷新 AWS Cognito 令牌时遇到问题。我可以使用cognitoidentityprovider.AdminInitiateAuth 方法获得id_token、access_token 和refresh_token。我已经创建了一个带有秘密的用户池客户端,所以我必须在AuthParameters 中提供SECRET_HASH。
登录时一切正常,但刷新令牌时相同的秘密哈希不起作用。我已经三次检查了代码并验证了我在登录和刷新令牌时发送的秘密哈希值是相同的(它应该是相同的,因为它使用不会改变的用户名、clientID 和 clientSecret)。
AWS API 返回以下错误:
{
"error": "NotAuthorizedException: Unable to verify secret hash for client myClientIdHere\n\tstatus code: 400, request id: c186ecf2-57a7-11e8-a01e-f97ed64650c9"
}
我已检查设备跟踪是否已关闭,因为文档提到这是在服务器端刷新令牌时出现的问题(请注意在“管理员身份验证流程”下,https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-authentication-flow.html#amazon-cognito-user-pools-server-side-authentication-flow)。
我的刷新代码是:
AWSRefreshToken := aws.String(refreshToken)
secretHash := secretHash(email, auth.Config.ClientID, auth.Config.ClientSecret)
AWSUserPoolID := aws.String(auth.Config.UserPoolID)
input := cognitoidentityprovider.AdminInitiateAuthInput{
AuthFlow: aws.String("REFRESH_TOKEN_AUTH"),
AuthParameters: map[string]*string{
"REFRESH_TOKEN": AWSRefreshToken,
"SECRET_HASH": &secretHash,
},
ClientId: &auth.Config.ClientID,
UserPoolId: AWSUserPoolID,
}
output, err := auth.AWSCognitoIdentityProvider.AdminInitiateAuth(&input)
秘密哈希码(来自https://stackoverflow.com/a/46163403/3515197):
func secretHash(username, clientID, clientSecret string) string {
mac := hmac.New(sha256.New, []byte(clientSecret))
mac.Write([]byte(username + clientID))
return base64.StdEncoding.EncodeToString(mac.Sum(nil))
}
我检查了其他 Stack Overflow 问题,但他们只提到了设备跟踪问题并且需要秘密哈希。我在这里错过了什么?
【问题讨论】:
-
“无法验证客户端 myClientId 的秘密哈希”,因此您将“myClientId”作为您的客户端 ID 发送。
标签: go aws-sdk-go cognito