简体   繁体   English

如何在 Android 中存储和检索 AWS Cognito JWT 令牌

[英]How to store and retrieve AWS Cognito JWT Token in Android

My Android app authenticates with Cognito in an AsyncTask and receives the JWT token as part of the CognitoUserSession .我的 Android 应用程序在AsyncTask使用 Cognito 进行身份验证,并接收 JWT 令牌作为CognitoUserSession一部分。 This is then set in the CognitoCachingCredentialsProvider using setLogins() .然后使用setLogins()CognitoCachingCredentialsProvider设置它。

I would like to retrieve this JWT token in another AsyncTask .我想在另一个AsyncTask检索这个 JWT 令牌。 However getLogins() on the CognitoCachingCredentialsProvider is returning a size 0 Map.但是, CognitoCachingCredentialsProvider上的getLogins()返回大小为 0 的映射。

What is the easiest way to get back the token?取回令牌的最简单方法是什么? Should I store it in Shared Preferences again?我应该再次将它存储在共享首选项中吗?

Get/SetLogins in the SDK just update a map inside the credentials provider, they don't save it long term. SDK 中的 Get/SetLogins 只是更新凭据提供程序中的映射,它们不会长期保存它。 If you need to access it across threads at some arbitrary time, that would be a reasonable way to accomplish it.如果您需要在任意时间跨线程访问它,那将是实现它的合理方法。 Otherwise, just use the exact same credentials provider and it'll be there.否则,只需使用完全相同的凭据提供程序,它就会在那里。

CognitoCachingCredentialsProvider is actually using the shared preferences to save data, but getLogins is returning a size 0 hash. CognitoCachingCredentialsProvider实际上使用共享首选项来保存数据,但getLogins返回大小为 0 的哈希值。

The easy way to persist data (JWT) was the shared preferences in the AsyncTask doInBackgraound method and retrieve wherever it is required.保存数据 (JWT) 的简单方法是AsyncTask doInBackgraound方法中的共享首选项,并在需要的地方检索。

Could get these token(refresh token / access token / id token) from aws in login time and put in preference like following:可以在登录时从 aws 获取这些令牌(刷新令牌 / 访问令牌 / id 令牌),并设置如下优先级:

 final AuthenticationHandler authenticationCallBackManager = new AuthenticationHandler() {
        @Override
        public void onSuccess(CognitoUserSession userSession, CognitoDevice newDevice) {
            /*SharedPreferenceCacheManager.writeString(
                    LoginActivity.this,
                    "ACCESS_TOKEN",
                    userSession.getAccessToken().getJWTToken());*/
            //System.out.println(userSession.getAccessToken().getJWTToken());//<<-------access token

            /*SharedPreferenceCacheManager.writeString(
                    LoginActivity.this,
                    "REFRESH_TOKEN",
                    userSession.getRefreshToken().getToken());*/
            //System.out.println(userSession.getRefreshToken().getToken());//<<---------refresh token

            SharedPreferenceCacheManager.writeString(
                    LoginActivity.this,
                    "ID_TOKEN",
                    userSession.getIdToken().getJWTToken());
            //System.out.println(userSession.getIdToken().getJWTToken());//<<-----------id token(this id uses Authorization in aws-side)

            //....
        }

        @Override
        public void getAuthenticationDetails(AuthenticationContinuation authenticationContinuation, String userId) {
            AuthenticationDetails authenticationDetails = new AuthenticationDetails(
                    userId,
                    String.valueOf(editTextPassword.getText()), null);

            //System.out.println(CognitoServiceConstants.AUTH_PARAM_USERNAME);
            //System.out.println(CognitoServiceConstants.AUTH_PARAM_REFRESH_TOKEN);

            // Pass the user sign-in credentials to the continuation
            authenticationContinuation.setAuthenticationDetails(authenticationDetails);

            // Allow the sign-in to continue
            authenticationContinuation.continueTask();
        }

        @Override
        public void getMFACode(MultiFactorAuthenticationContinuation continuation) {
           //....
        }

        @Override
        public void authenticationChallenge(ChallengeContinuation continuation) {
           //....
        }

        @Override
        public void onFailure(Exception exception) {
           //....
        }
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM