aws-sdk-js-v3 icon indicating copy to clipboard operation
aws-sdk-js-v3 copied to clipboard

ADMIN_NO_SRP_AUTH + Rotating Refresh Tokens --> Invalid Refresh Token

Open enchorb opened this issue 6 months ago • 2 comments

Checkboxes for prior research

Describe the bug

Refresh token from ADMIN_NO_SRP_AUTH does not work with rotating refresh tokens getTokensFromRefreshToken . Get the error NotAuthorizedException - Invalid Refresh Token.

Regression Issue

  • [ ] Select this option if this issue appears to be a regression.

SDK version number

@aws-sdk/[email protected]

Which JavaScript Runtime is this issue in?

Node.js

Details of the browser/Node.js/ReactNative version

v22.16.0

Reproduction Steps

Pass refresh token from cognitoIdentityProvider.adminInitiateAuth({ AuthFlow: 'ADMIN_NO_SRP_AUTH', ... }) to cognitoIdentityProvider.getTokensFromRefreshToken

Observed Behavior

 err: {
      "type": "NotAuthorizedException",
      "message": "Invalid Refresh Token.",
      "stack":
          NotAuthorizedException: Invalid Refresh Token.
              at de_NotAuthorizedExceptionRes (D:\Code\node_modules\@aws-sdk\client-cognito-identity-provider\dist-cjs\index.js:4792:21)
              at de_CommandError (D:\Code\node_modules\@aws-sdk\client-cognito-identity-provider\dist-cjs\index.js:4419:19)
              at processTicksAndRejections (node:internal/process/task_queues:105:5)
              at D:\Code\node_modules\@smithy\middleware-serde\dist-cjs\index.js:36:20
              at D:\Code\node_modules\@smithy\core\dist-cjs\index.js:193:18
              at D:\Code\node_modules\@smithy\middleware-retry\dist-cjs\index.js:320:38
              at data (D:\Code\node_modules\@aws-sdk\middleware-logger\dist-cjs\index.js:33:22)
              at data (D:\\Code\\dist\apps\backend\flow\webpack:\libs\core\src\functions\utils.ts:40:9)
              at t.tokenHandler (D:\\Code\\dist\apps\backend\flow\webpack:\libs\backend\auth\src\handlers\oauth.ts:319:11)
      "name": "NotAuthorizedException",
      "$fault": "client",
      "$metadata": {
        "httpStatusCode": 400,
        "requestId": "2c81fed1-47e1-49b3-9a34-29a5840bc256",
        "attempts": 1,
        "totalRetryDelay": 0
      },
      "__type": "NotAuthorizedException"
    }

Expected Behavior

Refresh token works

Possible Solution

No response

Additional Information/Context

No response

enchorb avatar Jun 28 '25 16:06 enchorb

Hi @enchorb - thanks for reaching out.

This error typically occurs because of how refresh tokens work with Cognito's different authentication flows. When using ADMIN_NO_SRP_AUTH, you should use adminInitiateAuth followed by adminRespondToAuthChallenge for refreshing tokens, not getTokensFromRefreshToken.

If you're still getting the error, check:

  • The refresh token's validity
  • Whether token rotation is enabled in your user pool
  • The app client settings in Cognito to ensure refresh tokens are allowed

aBurmeseDev avatar Jul 06 '25 08:07 aBurmeseDev

When passing in the refresh token to cognitoIdentityProvider.adminInitiateAuth({AuthFlow: 'REFRESH_TOKEN', ...}) we get the error UnsupportedOperationException" - This API does not support refresh token rotation. Also from initial testing it doesn't look like adminInitiateAuth passes along the ClientMetadata to the pre_token_generation lambda while getTokensFromRefreshToken does.

enchorb avatar Jul 06 '25 18:07 enchorb