应用程序关闭并重新打开后,CognitoCachingCredentialsProvider getCachedIdentityId为null

时间:2014-12-04 15:59:19

标签: amazon-web-services amazon-cognito

我可能误解了这种方法的预期行为,但这正是我试图用它来做的:

- 用户成功登录 - 用户完全关闭应用程序(在后台关闭) -User再次打开应用程序,无需再次登录,因为CognitoCachingCredentialsProvider可以在设备上本地检查以查看她仍然登录

我尝试完成此操作的方法是在提示登录之前检查getCachedIdentityId()返回的内容。如果它返回非null,则表示她仍然登录,因为没有任何东西可以从设备中清除她的凭据。这是我的框架的样子。我正在使用开发人员验证的方法:

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);
            Map logins = new HashMap();
            logins.put({Developer Provider Name}, sIdProvider.getToken());
            sCredProvider.setLogins(logins);
        }
    }
    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());
        }
        return sIdProvider;
    }

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

    protected static class UserIdentityProvider extends
            AWSAbstractCognitoIdentityProvider {

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

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

        @Override
        public String refresh() {
            try {
                ServerCommunicator server = new ServerCommunicator(context);
                if (email != null && password != null) {
  //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();
                    prefs.setAllUserSharedPrefs(response);
                    JSONObject responseJSON = new JSONObject(response);
                    String identityId = responseJSON.getString("id");
                    String token = responseJSON.getString("token");
                    if (token != null && identityId != null) {
                        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};
        }

    }

} 

我只是从这个类调用isLoggedIn方法来查看是否存在本地存储的IdentityId。但是,这没有按预期工作。我可以从调试中看到getCachedIdentityId始终为null(甚至直接在初始化CognitoCachingCredentialsProvider并将令牌添加到登录映射之后),并且每当我关闭应用程序后打开应用程序时,我总会再次登录。 IdentityId什么时候实际存储在本地,一般来说我的逻辑是正确的?

附加代码

import java.util.concurrent.ExecutionException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class LoginActivity extends Activity {

private final String TAG = "LoginActivity";

private EditText etEmail, etPwd;
private Button bLogin, bGoToRegister;
private ServerCommunicator server;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i(TAG, "onCreate");
    server = new ServerCommunicator(this);
    if (Util.isLoggedIn(this)) {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        return;
    }
    this.setContentView(R.layout.activity_login);
    etEmail = (EditText) findViewById(R.id.etEmail);
    etPwd = (EditText) findViewById(R.id.etPassword);
    bLogin = (Button) findViewById(R.id.bLogin);
    bGoToRegister = (Button) findViewById(R.id.bGoToRegister);
    bLogin.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            String email = etEmail.getText().toString();
            String pwd = etPwd.getText().toString();
            Util.getIdentityProvider(v.getContext()).setEmail(email);
            Util.getIdentityProvider(v.getContext()).setPassword(pwd);
            String token = Util.getIdentityProvider(v.getContext()).refresh();
            if (token != null) { 
                Intent intent = new Intent(v.getContext(), MainActivity.class); 
                startActivity(intent);
            }  else {
                 Toast.makeText(v.getContext(), "Invalid username/password",
                 Toast.LENGTH_SHORT).show();
            }
        }
    });
}
}

以上是我的LoginActivity。当应用程序启动MainActivity时,我在onCreate方法的开头有以下代码段:

if (!Util.isLoggedIn(this)) {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
}

此调用初始化CognitoCachingCredentialsProvider。我认为这将是IdentityId被缓存的时候,但我的调试显示,即使在此块之后,getCachedIdentityId()仍然返回null。我是否会尝试使用这门课程?

1 个答案:

答案 0 :(得分:1)

我有一个建议。 CognitoCachingCredentialsProvider是在更改后保存identityId的内容。但是,它不会开始监听,直到它被初始化,并且更新发生在刷新呼叫到您的身份提供者。

您是否可以尝试将CognitoCachingCredentialsProvider的初始化移至刷新调用之前(但在身份提供程序初始化之后)?

编辑:

更新将设置identityId和token,但是事先进行的显式调用可能会导致它认为没有进行任何更改。您也可以尝试取消设置者调用吗?