go - AWS Cognito 刷新 token 在 secret 哈希上失败

标签 go aws-sdk-go cognito

我在使用 Go 中的服务器端身份验证刷新 AWS Cognito token 时遇到问题。我可以使用 cognitoidentityprovider.AdminInitiateAuth 方法获取 id_tokenaccess_tokenrefresh_token。我已经创建了一个带有 secret 的用户池客户端,因此我必须在 AuthParameters 中提供 SECRET_HASH

登录时一切正常,但刷新 token 时相同的 secret 哈希不起作用。我已经对代码进行了三重检查,并验证了我在登录时和刷新 token 时发送的 secret 哈希值是相同的(它应该是相同的,因为它使用的用户名、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"
}

我检查过设备跟踪已关闭,因为文档提到这是在服务器端刷新 token 时出现的问题(请注意“管理员身份验证流程”下的 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)

secret 哈希码(来自 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 问题,但它们只提到了设备跟踪问题并且需要 secret 哈希。我在这里缺少什么?

最佳答案

在生成 secret 哈希时使用用户 ID (sub) 而不是电子邮件。

secretHash := secretHash(sub, auth.Config.ClientID, auth.Config.ClientSecret)

关于go - AWS Cognito 刷新 token 在 secret 哈希上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50337252/

相关文章:

interface - 如何在 Go 中声明复合接口(interface)?

sql - db.Exec()未在golang中为SQL Server返回正确的RowsAffected()值

go - 如何在 Go 中导入本地包?

go - 如何将 AWS 凭证传递给我的应用程序(Golang SDK)?

ios - iOS 上的 Mobile Hub Cognito

amazon-web-services - Cognito 注销不像记录的那样工作

go - 在导入包时出现此错误知道如何解决这个问题吗?

json - 如何在 GO 的 dynamodb 中保存 JSON 响应

go - dynamodbattribute.MarshalMap 返回空 map

amazon-web-services - AWS Cognito - 重置用户 MFA