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

时间:2014-12-04 11:42:43

标签: 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};
        }

    }

}

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

1 个答案:

答案 0 :(得分:1)

注销将分为两个步骤,首先您需要从验证用户身份的身份提供商(亚马逊,谷歌,Facebook或您自己的身份)注销。如何执行此操作的说明特定于您的提供商。

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