Skip to content

How to provide ID token in the Authorization header? #3120

@pavitra-infocusp

Description

@pavitra-infocusp

I'm using AWS Cognito User Pool with AWS API Gateway Cognito User Pool authorizer. It expects the Authorization header to be set to the ID token. However, AWS Amplify Android SDK passes the access token which is wrong. I have applied a workaround currently to intercept the requests and provide the correct value for the header. However, I feel like the Android SDK should support this OOTB.

Amplify configuration json

{
  "UserAgent": "aws-amplify-cli/2.0",
  "Version": "1.0",
  "auth": {
    "plugins": {
      "awsCognitoAuthPlugin": {
        "CognitoUserPool": {
          "Default": {
            "PoolId": "[redacted]",
            "AppClientId": "[redacted]",
            "Region": "us-east-1"
          }
        },
        "Auth": {
          "Default": {
            "authenticationFlowType": "USER_SRP_AUTH"
          }
        }
      }
    }
  },
  "api": {
    "plugins": {
      "awsAPIPlugin": {
        "myApi": {
          "endpointType": "REST",
          "endpoint": "https://[redacted].execute-api.us-east-1.amazonaws.com/dev",
          "region": "us-east-1",
          "authorizationType": "NONE" // "AMAZON_COGNITO_USER_POOLS" injects access token
        }
      }
    }
  }
}

Current Workaround

Added an interceptor to fetch the ID token from the current auth session.

val apiPlugin = AWSApiPlugin.builder()
    .configureClient("dynamicScreensApi") { okHttpBuilder ->
        okHttpBuilder.addInterceptor { chain ->
            runBlocking {
                val session = Amplify.Auth.fetchAuthSession() as AWSCognitoAuthSession

                val idToken = session.userPoolTokensResult.value?.idToken

                val originalRequest = chain.request()
                val updatedRequest = originalRequest.newBuilder()
                    .addHeader("Authorization", "$idToken")
                    .build()
                chain.proceed(updatedRequest)
            }
        }
    }
    .build()
Amplify.addPlugin(apiPlugin)

Metadata

Metadata

Assignees

No one assigned

    Labels

    REST APIRelated to the API (REST) category/pluginsapipending-maintainer-responseIssue is pending response from an Amplify team memberquestionGeneral question

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions