javascalaamazon-cognitoaws-java-sdkplayframework-2.6

Cognito user pool: How to use refreshToken to get new accessToken after accessToken gets expired in aws cognito java sdk?


I am using aws cognito in scala play framework based web app as user management solution. I am using following code to login.

var mIdentityProvider: AWSCognitoIdentityProvider = getAmazonCognitoIdentityClient;

def sessionLogin(userName: String, password: String): AdminInitiateAuthResult = {
val authParams: java.util.Map[String, String] = new java.util.HashMap[String, String]()
    authParams.put("USERNAME", userName)
    authParams.put("PASSWORD", password)
    val authRequest = new AdminInitiateAuthRequest()
      .withAuthFlow(AuthFlowType.ADMIN_NO_SRP_AUTH)
      .withUserPoolId("***")
      .withClientId("***")
      .withAuthParameters(authParams)
    val authResult = mIdentityProvider.adminInitiateAuth(authRequest)
    authResult
}

Above code returns accessToken, expiresIn, tokenType, refreshToken and idToken from aws cognito server. As per aws documentation, we can use refreshToken to get new accessToken or idToken when accessToken gets expired in order to continue user session. But in document it is not mentioned how to use refreshToken for this purpose. Any help regarding this would be appreciable. Thanks in advance.


Solution

  • I figured it out myself. Following is working code

    def refreshAccessToken(refreshToken: String): AuthenticationResultType = {
        val authParams: java.util.Map[String, String] = new java.util.HashMap[String, String]()
        authParams.put("REFRESH_TOKEN", refreshToken)
        val authRequest = new AdminInitiateAuthRequest()
          .withAuthFlow(AuthFlowType.REFRESH_TOKEN_AUTH)
          .withUserPoolId("***")
          .withClientId("***")
          .withAuthParameters(authParams)
        val authResult = mIdentityProvider.adminInitiateAuth(authRequest)
        val resultType: AuthenticationResultType = authResult.getAuthenticationResult
        resultType
      }