【问题标题】:AWS Cognito Unable to verify secret hash for clientAWS Cognito 无法验证客户端的秘密哈希
【发布时间】:2017-09-25 08:19:17
【问题描述】:

当我尝试authenticateUser 时,我得到了

Error: Unable to verify secret hash for client <CLIENT_ID_HERE>

怎么了?我的代码如下:

import {
  Config,
  CognitoIdentityCredentials
} from "aws-sdk"
import {
  CognitoUserPool,
  CognitoUserAttribute,
  AuthenticationDetails,
  CognitoUser
} from "amazon-cognito-identity-js"

Config.region = "ap-northeast-2"

var userpool = new CognitoUserPool({
  UserPoolId: "ap-northeast-2_QosOiWMkd",
  ClientId: "1bd6s9mv98bo2lucen2vesbqls"
})

var userData = {
  Username: "jiewmeng@gmail.com",
  Pool: userpool
}

var authData = new AuthenticationDetails({
  Username: "jiewmeng@gmail.com",
  Password: "P@$$w0rd"
})

var cognitoUser = new CognitoUser(userData)
cognitoUser.authenticateUser(authData, {
  onSuccess: function (result) {
    console.log("authenticated with", result)
  },
  onFailure: function (err) {
    console.error(err)
  }
})

在 AWS 上,客户端密码已被禁用

【问题讨论】:

    标签: javascript amazon-web-services aws-sdk amazon-cognito


    【解决方案1】:

    适用于 JavaScript 的 Amazon Cognito 身份开发工具包不支持具有客户端密钥的应用程序。这在SDK documentation 中有说明:

    创建App时,生成客户端密码框必须是 未选中,因为 JavaScript SDK 不支持具有 客户端密码。

    看来您将不得不重新配置您的应用程序。

    【讨论】:

    • 但是从我发布的图片来看,客户端密码已经被禁用了?
    • 我发现你是对的,奇怪的是,今天当我再次检查时有一个客户秘密......不确定是否需要一段时间才能显示或什么......
    【解决方案2】:

    解决方案是将 secret_hash 与 adminAuthInitiate 请求一起传递。要计算秘密哈希,您可以使用以下方法:

    public static String calculateSecretHash(String userPoolClientId, String userPoolClientSecret, String userName) {
    final String HMAC_SHA256_ALGORITHM = "HmacSHA256";
            SecretKeySpec signingKey = new SecretKeySpec(
                    userPoolClientSecret.getBytes(StandardCharsets.UTF_8),
                    HMAC_SHA256_ALGORITHM);
            try {
                Mac mac = Mac.getInstance(HMAC_SHA256_ALGORITHM);
                mac.init(signingKey);
                mac.update(userName.getBytes(StandardCharsets.UTF_8));
                byte[] rawHmac = mac.doFinal(userPoolClientId.getBytes(StandardCharsets.UTF_8));
                return Base64.getEncoder().encodeToString(rawHmac);
            } catch (Exception e) {
                throw new RuntimeException("Error while calculating ");
            }
        }
    

    如何传递 Secret_Hash

    Map<String, String> authParams = new HashMap<>(2);
    authParams.put("USERNAME", <username>);
    authParams.put("PASSWORD", <password>);
                        authParams.put("SECRET_HASH", calculateSecretHash(cognitoClientId, cognitoClientSecret, <username>));
                        AdminInitiateAuthRequest authRequest = new AdminInitiateAuthRequest()
                                .withClientId(userPool.getClientId()).withUserPoolId(userPool.getUserPoolId())
                                .withAuthFlow(AuthFlowType.ADMIN_NO_SRP_AUTH).withAuthParameters(authParams);
                        AdminInitiateAuthResult result = cognito.adminInitiateAuth(authRequest);
                        auth = result.getAuthenticationResult();
    

    【讨论】:

      猜你喜欢
      • 2020-12-11
      • 2016-09-23
      • 2018-05-05
      • 2019-02-28
      • 2019-06-23
      • 2019-03-03
      • 2018-06-03
      • 2018-10-24
      • 2019-04-21
      相关资源
      最近更新 更多