首页 > 解决方案 > 如文档中所述,如何使访问或 id 令牌的寿命延长 10 年?

问题描述

我正在使用 Amazon Cognito 进行身份验证,我想问一下我的访问令牌和 id 令牌是否会在一小时内过期,此外,我已经经历了许多他们告诉使用刷新令牌的答案,因为它们的有效期为 10 年,所以我的问题是如何在 android 中使用刷新令牌?

现在登录后,我正在将CognitoCachingCredentialProvider登录映射设置为令牌 - 我同时使用 Facebook 和电子邮件登录。

val authenticationHandler = object : AuthenticationHandler {
            override fun onSuccess(userSession: CognitoUserSession?, newDevice: CognitoDevice?) {

            //After Authentication User  Cognito Access Id and Access Secret Extraction
            currentSession = userSession!!

            //Getting Session Token
            val id = currentSession.idToken.jwtToken
            //Credential Provider
            val cognitoCachingCredentialsProvider = CognitoCachingCredentialsProvider(this@LoginActivity,resources.getString(R.string.cognito_identity_pool_id),Regions.myRegion)
            cognitoCachingCredentialsProvider.clear()
            //Login Map

            val login = HashMap<String,String>()
            login["myString"] = id
            cognitoCachingCredentialsProvider.logins = login
            //Off the main thread
            SimpleAsyncTask(this@LoginActivity,cognitoCachingCredentialsProvider).execute()
        }

        override fun authenticationChallenge(continuation: ChallengeContinuation?) {
            continuation?.continueTask()
        }

        override fun getAuthenticationDetails(authenticationContinuation: AuthenticationContinuation, userId: String) {
            // The API needs user sign-in credentials to continue
            Log.d(TAG, "userId is : $userId")
            val authenticationDetails = AuthenticationDetails(userId, password, null)
            authenticationDetails.authenticationType = "USER_PASSWORD"

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

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

        override fun getMFACode(multiFactorAuthenticationContinuation: MultiFactorAuthenticationContinuation) {
            // Multi-factor authentication is required; get the verification code from user
            multiFactorAuthenticationContinuation.setMfaCode(null)
            // Allow the sign-in process to continue
            multiFactorAuthenticationContinuation.continueTask()
        }

        override fun onFailure(exception: Exception) {
            // Sign-in failed, check exception for the cause
            Log.e(TAG, "${exception.message}")
        }
    }
    // Sign in the user
    user.getSessionInBackground(authenticationHandler)
}
internal class SimpleAsyncTask(private val activity: Activity,private val credential:CognitoCachingCredentialsProvider) :
        AsyncTask<Void, Void, Void>() {
    override fun doInBackground(vararg p0: Void?):Void ?{
        credential.refresh()
        credential.setPersistenceEnabled(true)
        return null
    }

类似的代码也用于像这样的facebook登录

FacebookCallback<LoginResult> {
                        override fun onSuccess(loginResult: LoginResult) {




                        //Getting access Token

                        val accessToken = loginResult.accessToken.token

                        //Credentials Extraction
                        val credentials = CognitoCachingCredentialsProvider(this@LoginActivity,resources.getString(R.string.cognito_identity_pool_id),Regions.myRegion)
                        credentials.clear()
                        //Map of login
                        val login = HashMap<String,String>()
                        login["graph.facebook.com"] = accessToken

                        //Setting the value of map
                        credentials.logins = login


                        //Off the main thread
                        SimpleAsyncTask(this@LoginActivity,credentials).execute()

                    }

                    override fun onCancel() {

                        //Cancel code
                        Toast.makeText(this@LoginActivity,"Canceled",Toast.LENGTH_SHORT).show()

                    }

                    override fun onError(exception: FacebookException) {
                        //Error code
                        Toast.makeText(this@LoginActivity,exception.toString(),Toast.LENGTH_SHORT).show()
                    }
                })

现在我用它来检查用户登录状态,我检查cognitoCachingCredentialProvider.cachedId!=null检查用户登录的条件。但是它登录了一个小时如何让用户长时间登录

标签: androidamazon-web-serviceskotlinamazon-cognito

解决方案


刷新令牌与 id 或访问令牌明显不同。您可以使用刷新令牌来获取新的访问权限和 id 令牌(顾名思义)。当您调用 getSession 时,如果您的令牌已过期并且您的刷新令牌尚未过期,它应该会自动刷新您的令牌。

更多信息:https ://stackoverflow.com/a/39480690/6941447


推荐阅读