diff --git a/packages/api-rest/src/apis/common/handler.ts b/packages/api-rest/src/apis/common/handler.ts index 9ae021e4e4e..4980269ed1c 100644 --- a/packages/api-rest/src/apis/common/handler.ts +++ b/packages/api-rest/src/apis/common/handler.ts @@ -21,6 +21,7 @@ import { } from '../../utils'; import { resolveHeaders } from '../../utils/resolveHeaders'; import { RestApiResponse } from '../../types'; +import { iamAuthApplicableGQL } from '../../utils/iamAuthApplicable'; type HandlerOptions = Omit & { body?: DocumentType | FormData; @@ -47,6 +48,10 @@ export const transferHandler = async ( amplify: AmplifyClassV6, options: HandlerOptions & { abortSignal: AbortSignal }, signingServiceInfo?: SigningServiceInfo, + iamAuthApplicable: ( + { headers }: HttpRequest, + signingServiceInfo?: SigningServiceInfo, + ) => boolean = iamAuthApplicableGQL, ): Promise => { const { url, method, headers, body, withCredentials, abortSignal } = options; const resolvedBody = body @@ -97,11 +102,6 @@ export const transferHandler = async ( }; }; -const iamAuthApplicable = ( - { headers }: HttpRequest, - signingServiceInfo?: SigningServiceInfo, -) => !headers.authorization && !headers['x-api-key'] && !!signingServiceInfo; - const resolveCredentials = async ( amplify: AmplifyClassV6, ): Promise => { diff --git a/packages/api-rest/src/apis/common/publicApis.ts b/packages/api-rest/src/apis/common/publicApis.ts index 6a132a6b277..0bd7f0a189e 100644 --- a/packages/api-rest/src/apis/common/publicApis.ts +++ b/packages/api-rest/src/apis/common/publicApis.ts @@ -25,6 +25,7 @@ import { parseSigningInfo, resolveApiUrl, } from '../../utils'; +import { iamAuthApplicablePublic } from '../../utils/iamAuthApplicable'; import { transferHandler } from './handler'; @@ -72,6 +73,7 @@ const publicHandler = ( abortSignal, }, signingServiceInfo, + iamAuthApplicablePublic, ); }); diff --git a/packages/api-rest/src/utils/iamAuthApplicable.ts b/packages/api-rest/src/utils/iamAuthApplicable.ts new file mode 100644 index 00000000000..18c5f9ce8aa --- /dev/null +++ b/packages/api-rest/src/utils/iamAuthApplicable.ts @@ -0,0 +1,19 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { HttpRequest } from '@aws-amplify/core/internals/aws-client-utils'; + +interface SigningServiceInfo { + service?: string; + region?: string; +} + +export const iamAuthApplicableGQL = ( + { headers }: HttpRequest, + signingServiceInfo?: SigningServiceInfo, +) => !headers.authorization && !headers['x-api-key'] && !!signingServiceInfo; + +export const iamAuthApplicablePublic = ( + { headers }: HttpRequest, + signingServiceInfo?: SigningServiceInfo, +) => !headers.authorization && !!signingServiceInfo;