1

我可能误解了这种方法的预期行为,但这就是我试图使用它的目的:

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

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

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 始终为空(即使在初始化 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。我是如何尝试使用此类的吗?

4

1 回答 1

1

我有一个建议。CognitoCachingCredentialsProvider 是在 identityId 更改时保存它。但是,它不会开始侦听,直到它被初始化,并且更改发生在对您的身份提供者的刷新调用上。

您可以尝试将 CognitoCachingCredentialsProvider 的初始化移动到刷新调用之前(但在您的身份提供者初始化之后)?

编辑:

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

于 2014-12-04T23:46:24.847 回答