【问题标题】:AWS Cognito SignOut AndroidAWS Cognito 注销 Android
【发布时间】:2019-04-06 21:06:07
【问题描述】:

我正在使用最新版本的适用于 Android 的 AWS 开发工具包。

implementation 'com.amazonaws:aws-android-sdk-core:2.7.6'
implementation 'com.amazonaws:aws-android-sdk-cognito:2.7.6'
implementation 'com.amazonaws:aws-android-sdk-cognitoidentityprovider:2.7.6'

我的身份验证处理程序大部分取自他们的示例代码。

    // create a handler for the sign-in process
    private AuthenticationHandler authenticationHandler = new AuthenticationHandler() {

        @Override
        public void onSuccess(CognitoUserSession userSession, CognitoDevice newDevice) {
//            String idToken = userSession.getIdToken().getJWTToken();
//            Map<String, String> logins = new HashMap<>();
//            logins.put("cognito-idp.us-east-1.amazonaws.com/" + getString(R.string.user_pool_id), idToken);
//            AuthHelper.getInstance().getCredentialsProvider().setLogins(logins);
//
//            new RefreshCognitoCredentials().execute();

            startActivity(new Intent(LoginActivity.this, MainActivity.class));
            finish();
        }

        @Override
        public void getAuthenticationDetails(AuthenticationContinuation authenticationContinuation, String userId) {
            String password = inputPassword.getText().toString();

            AuthenticationDetails authenticationDetails = new AuthenticationDetails(userId, password, null);
            authenticationContinuation.setAuthenticationDetails(authenticationDetails);
            authenticationContinuation.continueTask();
        }

        @Override
        public void getMFACode(MultiFactorAuthenticationContinuation continuation) {

        }

        @Override
        public void authenticationChallenge(ChallengeContinuation continuation) {

        }

        @Override
        public void onFailure(Exception exception) {
            String error = AuthHelper.formatException(exception);
            layoutUsername.setErrorEnabled(true);
            layoutUsername.setError(error);
        }
    };

身份验证工作得很好。并且它应该缓存。 在我的启动画面活动中,我可以检查 CognitoUser.getCurrentUser().getUserId()。

现在退出:

CognitoUser.getCurrentUser().signOut()

现在,如果我关闭应用并打开应用 - CognitoUser.getCurrentUser().getUserId() 仍会返回我之前登录的用户。

几个月前,我使用 2.2.+ 作为我的 sdk 版本进行了 AWS 实施,这个示例按预期工作。

注意* 如果我尝试 CognitoUser.getCurrentUser().globalSignout() - 它会返回“用户未通过身份验证”错误。

如果我有一个有效的用户/会话,我如何检查应用启动?我讨厌 AWS 在没有文档或无法找到的文档的情况下每天都在进行更改。

【问题讨论】:

  • 你如何访问inputPassword/android 那里的东西?在 Cognito 课程中!

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


【解决方案1】:

signOut 从 SharedPreferences 中清除缓存的令牌。它清除存储在 access、id 和 refresh 令牌下的数据。但是 LastAuthUser 键包含的用户 ID 未被 signOut 清除。

当您调用cognitoUserPool.getCurrentUser().getUserId() 时,它会检查 SharedPreferences 中是否存在 LastAuthUser 键,因此它会返回 userId。我正在调查这个问题。当我可以确认预期的行为时,将更新此答案。

【讨论】:

  • 文档指出“这已清除所有令牌,此用户必须通过身份验证过程才能获取令牌。”有没有我看不到的方法来检查令牌是否已被清除?我可以复制两个版本的库之间的差异。 signOut 清除了用户会话并为 getUserId() 返回了 null,但现在它没有。我只是想弄清楚发生了什么变化以及我需要做什么。 docs.aws.amazon.com/cognito/latest/developerguide/…
  • 我相信我应该使用 getSession() 而不是检查 userId。它似乎按预期工作。
  • 是的,您绝对可以在用户退出后再次拨打getSession()再次显示SignIn。不过,我会就getUserId() 上行为变化的原因回复您。谢谢!
  • getSession() 仅在用户通过 Cognito 用户池而不是从联合身份提供者登录时才有效?我真的必须手动完成所有令牌解析吗?
  • @Karthikeyan 在这里我有一个关于 getUserId() 的问题。当应用程序在长时间间隔后打开时,getUserId 返回 null。所以在这里我必须清除所有缓存并重新登录?
【解决方案2】:

好像是AWS SDK的bug。

我几乎有同样的问题。 我的步骤:

 1. Sign in as user 1
 2. Sing out
 3. Sign in as user 2
 4. Surprise that user 1 is signed in instead of user 2

所以,决定升级到新的 SDK

com.amazonaws:aws-android-sdk-mobile-client:2.13.4

使用

implementation 'com.amazonaws:aws-android-sdk-core:2.13.4'
implementation 'com.amazonaws:aws-android-sdk-cognito:2.13.4'
implementation 'com.amazonaws:aws-android-sdk-cognitoidentityprovider:2.13.4'

但这无济于事。

调查中发现

与 /data/data/applicationId/shared_prefs/CognitoIdentityProviderCache.xml 中的文件 CognitoIdentityProviderCache.xml 相关的问题

CognitoIdentityProviderCache.xml 文件在注销后未清除。

登出后的文件内容示例:

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <string name="CognitoIdentityProvider.app_id.LastAuthUser.encrypted">...</string>
    <string name="CognitoIdentityProvider.app_id.LastAuthUser.encrypted.iv">...</string>
    <string name="CognitoIdentityProvider.app_id.LastAuthUser.encrypted.keyvaluestoreversion">1</string>
</map>

从文件中删除这 3 行可以解决问题。

解决方法:

  1. 退出时清除文件。

但文件名可以在下一个版本中更改。

  1. 清除应用程序内部文件夹。 (应用一)

相当于在系统设置->应用->AwesomeApp中按“清除数据”

 context.cacheDir.parentFile.deleteRecursively()

您可以在他们的错误跟踪器上关注我的错误报告以获取详细信息

https://github.com/aws-amplify/aws-sdk-android/issues/1015

【讨论】:

    【解决方案3】:

    我在更新用户的电子邮件时遇到了这种情况。就我而言,当用户切换回以前使用的电子邮件时,cognito 不会更新最后一个经过身份验证的用户的值。

    我的解决方案是手动更新共享首选项中的值,其中 cognito 会更新最后一个经过身份验证的用户:

    SharedPreferences.Editor editor = context.getSharedPreferences("CognitoIdentityProviderCache", 0).edit();
    String csiLastUserKey = "CognitoIdentityProvider." + cognitoUserPool.getClientId() + ".LastAuthUser";
    editor.putString(csiLastUserKey, newEmail);
    editor.commit();
    

    这是一个 hacky 解决方法,因为我强制 cognito 应该自动执行。

    【讨论】:

      猜你喜欢
      • 2019-04-12
      • 1970-01-01
      • 2022-11-27
      • 2019-04-24
      • 2018-11-28
      • 2018-12-10
      • 1970-01-01
      • 2022-12-10
      • 2021-10-18
      相关资源
      最近更新 更多