【问题标题】:Amplify "Unable to verify secret hash for client"放大“无法验证客户端的秘密哈希”
【发布时间】:2019-02-28 10:38:36
【问题描述】:

我们一直在使用 Amplify 和 Cognito 为我们的用户注册部署到 Lambda 的 Angular6 应用程序。客户希望从电子邮件转换为用户名作为主要用户标识。所以我们创建了一个新的用户池/客户端。我无法查看配置设置,我只是获得了新的用户池、身份池和客户端 ID。然后我将应用程序注册的代码更改为如下所示:

  return from(Auth.signUp({
  'username': username, // was email
  'password': password, 
  attributes: { // added these
    'email': email,
    'phone_number': phone_number,
    'family_name': name,
    'birthdate': DOB,
    'custom:last_4_ssn': SSN // custom attribute
  }}));

我得到的响应没有进行其他更改是:无法验证客户端的秘密哈希。 Google 声称问题在于 secretAccess 目前是不受支持的配置,但有权访问这些服务的人向我发誓,我们的设置中没有配置 secretAccess。

对于无法访问配置,我深表歉意,但还有其他可能的原因会收到此错误吗?

【问题讨论】:

  • 我在 aws 文档中发现,默认情况下,新用户池将启用秘密访问密钥。我相信配置这个新用户池的人没有意识到他需要禁用它或处理它。

标签: amazon-cognito aws-amplify


【解决方案1】:

该错误可能源于您连接的应用程序客户端具有关联的密钥这一事实。创建用户池应用程序客户端时,默认情况下会生成一个密钥:

现在,使用 React-Native Amplify,您必须使用没有生成密钥的应用客户端。因此,当您创建具有所需属性的新应用程序客户端时,请确保未选中“生成客户端密码”框。

【讨论】:

【解决方案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();

【讨论】:

    猜你喜欢
    • 2017-09-25
    • 2019-06-23
    • 2019-03-03
    • 2020-12-11
    • 2018-05-05
    • 2016-09-23
    • 2019-04-21
    • 2021-01-01
    • 1970-01-01
    相关资源
    最近更新 更多