如何在 Amplify for Android 中将 Cognito 身份池与 UnAuthenticatd 用户一起使用

How to use Cognito identity pool with UnAuthenticatd users in Amplify for Android

我一直在研究 AWS Amplify docs and tutorials,了解如何将 Amplify 和 Cognito 身份池与未经身份验证的用户一起使用。 Amplify 文档给出的示例是:

Amplify.Auth.fetchAuthSession(
    result -> {
        AWSCognitoAuthSession cognitoAuthSession = (AWSCognitoAuthSession) result;
            switch(cognitoAuthSession.getIdentityId().getType()) {
                case SUCCESS:
                    Log.i("AuthQuickStart", "IdentityId: " + cognitoAuthSession.getIdentityId().getValue());
                    break;
                case FAILURE:
                    Log.i("AuthQuickStart", "IdentityId not present because: " + cognitoAuthSession.getIdentityId().getError().toString());
            }
        },
        error -> Log.e("AuthQuickStart", error.toString())
);

但在实践中,当我使用此代码时 - 我在 LogCat:

中打印出错误
AuthQuickStart: FAILURE IdentityId not present because: AmplifyException {message=You are currently signed out., cause=null, recoverySuggestion=Please sign in and reattempt the operation.}

注意:我确实配置了 AWS Cognito 以支持未经身份验证的用户!

我也到处寻找 Amplify Android API 文档以查看支持的其他 API - 找不到任何 Android API 文档。 并查看 AWS Amplify.Auth 方法我找不到任何处理未经身份验证的用户的函数

问题:

关于如何使用 Amplify (Android) 并通过 AWS Cognito 为未经身份验证的用户获取 AWS 凭据的任何线索???

除非您有登录用户,否则您将无法检索经过身份验证的会话。

如果您的身份池(不是用户池)是为未经身份验证的用户或来宾用户配置的,您可以简单地调用 GetId 端点:

GetId

生成(或检索)Cognito ID。提供多个登录名将创建一个隐式 linked 帐户。

这是一个publicAPI。您不需要任何凭据即可调用此 API.

请求语法

{
   "AccountId": "string",
   "IdentityPoolId": "string",
   "Logins": { 
      "string" : "string" 
   }
}

https://docs.aws.amazon.com/cognitoidentity/latest/APIReference/API_GetId.html

然后您应该能够使用该 ID 通过以下方式检索令牌:

获取OpenIdToken

使用已知的 Cognito ID 获取 OpenID 令牌。这个已知的 Cognito ID 由 GetId 返回。您可以选择为身份添加其他登录名。提供多个登录会创建一个隐式 link.

OpenID 令牌的有效期为 10 分钟。

这是一个publicAPI。您不需要任何凭据即可调用此 API.

请求语法

{
   "IdentityId": "string",
   "Logins": { 
      "string" : "string" 
   }
}

https://docs.aws.amazon.com/cognitoidentity/latest/APIReference/API_GetOpenIdToken.html

我是 Amplify Android 团队的 David。实际上,前几天我只是在研究这个,目前有一个 hack 需要让 unauth 用户工作。

通过 CLI 设置 unauth/guest 用户后(正如您提到的那样),您必须在底层逃生舱口上调用一次 getAWSCredentials 方法,应用程序才能运行。

这是我编写的代码片段,您可以在 Amplify.configure 之后 运行(同样,每个应用安装只需要 运行 一次):

AWSMobileClient mobileClient = (AWSMobileClient) Amplify.Auth.getPlugin("awsCognitoAuthPlugin").getEscapeHatch();

mobileClient.getAWSCredentials(new Callback<AWSCredentials>() {
     @Override
     public void onResult(AWSCredentials result) {

        // Now you'll see the Identity ID and AWSCredentials in the resulting auth session object.
        Amplify.Auth.fetchAuthSession(
            result2 -> Log.i(TAG, result2.toString()),
            error -> Log.e(TAG, error.toString()));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
     }

     @Override
     public void onError(Exception e) {
          // Handle the error however is best for your app
     }
});

我正在研究一个解决方案来避免这种黑客攻击,并在我们的网站上添加一个关于 Unauth 用户的文档部分,但与此同时,这应该能让它为你工作。

再次请注意,您只需执行一次此操作,之后,它应该在您调用 fetchAuthSession.

时正常工作

更新:未打补丁的(官方)版本:

Amplify.Auth.fetchAuthSession(
    result -> {
        AWSCognitoAuthSession cognitoAuthSession = (AWSCognitoAuthSession) result;
        switch (cognitoAuthSession.getIdentityId().getType()) {
            case SUCCESS:
                Log.i(TAG, "identity: " + cognitoAuthSession.getIdentityId().getValue());
                Log.i(TAG, "credentials: " + cognitoAuthSession.getAWSCredentials().getValue(););
                break;
            case FAILURE:
                Log.i(TAG, "FAILURE IdentityId not present because: " + cognitoAuthSession.getIdentityId().getError().toString());
        }
    },
    error -> Log.e(TAG, "UNAUTH USERS ERR: " + error.toString()));