amazon-web-services - CognitoCachingCredentialsProvider (Android) - 如何注销和删除缓存的凭据

标签 amazon-web-services amazon-cognito

我正在使用经过开发人员验证的技术来实现此类,如here所述。 。到目前为止,我已经能够实现此类并构建一个框架,在其中检查 CognitoCachingCredentialsProvider.getCachedIdentityId() 以查看用户是否已登录(因此不需要通过输入电子邮件和密码来重新进行身份验证) )。为此,我在名为 Util 的类中使用了一系列静态方法,因为这些方法只需实例化一次。它看起来像这样:

package com.pranskee.boxesapp;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.amazonaws.auth.AWSAbstractCognitoIdentityProvider;
import com.amazonaws.auth.CognitoCachingCredentialsProvider;
import com.amazonaws.mobileconnectors.cognito.*;
import com.amazonaws.regions.Regions;

public class Util {
    private final static String TAG = "Util";

    private static final String AWS_ACCOUNT_ID = {acct id};
    private static final String COGNITO_POOL_ID = {pool id};
    private static final String COGNITO_ROLE_AUTH = {auth arn};
    private static final String COGNITO_ROLE_UNAUTH = {unauth arn}

    private static CognitoCachingCredentialsProvider sCredProvider;
    private static UserIdentityProvider sIdProvider;
    private static CognitoSyncManager sSyncManager;

    private Util() {
    }

    public static CognitoCachingCredentialsProvider getCredProvider(
            Context context) {
        if (sCredProvider == null) {
            if (sIdProvider == null) {
                CognitoCachingCredentialsProvider tmpProvider = new CognitoCachingCredentialsProvider(
                        context.getApplicationContext(), AWS_ACCOUNT_ID,
                        COGNITO_POOL_ID, COGNITO_ROLE_UNAUTH,
                        COGNITO_ROLE_AUTH, Regions.US_EAST_1);
                if (tmpProvider.getCachedIdentityId() != null) {
                    sCredProvider = tmpProvider;
                } else {
                    sCredProvider = null;
                }
            } else {
                sCredProvider = new CognitoCachingCredentialsProvider(
                        context.getApplicationContext(), sIdProvider,
                        COGNITO_ROLE_UNAUTH, COGNITO_ROLE_AUTH);
            }
        }
        return sCredProvider;
    }

    public static UserIdentityProvider getIdentityProvider(Context context,
            String email, String pwd) {
        if (sIdProvider == null) {
            sIdProvider = new UserIdentityProvider(AWS_ACCOUNT_ID,
                    COGNITO_POOL_ID, context.getApplicationContext(), email,
                    pwd);
            Map logins = new HashMap();
            logins.put({Developer Provider Name}, sIdProvider.getToken());
            sIdProvider.setLogins(logins);
        }
        return sIdProvider;
    }

    public static boolean isLoggedIn(Context context) {
        if (getCredProvider(context) == null) {
            return false;
        }
        return true;
    }

    private static CognitoSyncManager getSyncManager(Context context) {
        if (sSyncManager == null) {
            sSyncManager = new CognitoSyncManager(
                    context.getApplicationContext(), Regions.US_EAST_1,
                    sCredProvider);
        }
        return sSyncManager;
    }

    protected static class UserIdentityProvider extends
            AWSAbstractCognitoIdentityProvider {

        private Context context;
        private String email;
        private String password;

        public UserIdentityProvider(String accountId, String identityPoolId,
                Context c, String em, String pwd) {
            super(accountId, identityPoolId);
            context = c;
            email = em;
            password = pwd;
        }

        @Override
        public String refresh() {
            try {
                ServerCommunicator server = new ServerCommunicator(context);
                //this is a server call, which makes the call GetOpenIdTokenForDeveloperIdentityRequest after I authenticate the user and send AWS my user's token
                String response = server.initUserLoginAsyncTask()
                        .execute(email, password).get();
                JSONObject responseJSON = new JSONObject(response);
                String identityId = responseJSON.getString("id");
                String token = responseJSON.getString("token");
                this.setToken(token);
                this.setIdentityId(identityId);
                update(identityId, token);
                return token;
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        public String getProviderName() {
            return {Developer Provider Name};
        }

    }

}

现在,我还想实现注销。我认为我需要做的是以某种方式删除缓存的身份 ID,但我不确定最好的方法是什么。或者,也许根本不是那样,我需要做一些完全不同的事情。无论哪种方式,我只想实现允许用户选择“注销”我的应用程序的预期行为,这会导致 Cognito 忘记该 ID 已登录到身份池中,并使再次建立身份 ID 的任何尝试无效无需再次执行我的身份验证过程。

最佳答案

注销过程分为两个步骤,首先您需要从对您的用户进行身份验证的身份提供商(Amazon、Google、Facebook 或您自己的)注销。有关如何执行此操作的说明特定于您的提供商。

从 CognitoIdentity 方面,您需要告诉 CredentialsProvider 清除与您的身份相关的所有状态和缓存。使用 Android SDK,您可以在 CredentialsProvider 上调用clear()(请参阅 http://docs.aws.amazon.com/AWSAndroidSDK/latest/javadoc/com/amazonaws/auth/CognitoCredentialsProvider.html)

关于amazon-web-services - CognitoCachingCredentialsProvider (Android) - 如何注销和删除缓存的凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27293179/

相关文章:

node.js - 在 Elastic Beanstalk 上以集群模式运行 pm2

amazon-web-services - eip 分配 id 的 Terraform 输出

swift - 无法使用 AWS Cognito 登录和访问 token

javascript - 服务器不理解所请求的操作。 (AWS)

mysql - 如何将 RedShift 上的表格卸载到单个 CSV 文件?

amazon-web-services - CloudFormation - 将标签应用于其他 AWS 资源

amazon-web-services - 批量AWS S3文件是否存在验证?

amazon-cognito - Cognito 无法注册尚未确认状态的用户

ios - 如果没有用户登录,AWS Cognito iOS 会尝试转到启动页面而不是登录页面

node.js - Cognito 登录 MFA 短信验证问题