Skip to content

Commit

Permalink
refactor iamAuthApplicable
Browse files Browse the repository at this point in the history
  • Loading branch information
ashika112 committed May 23, 2024
1 parent 2ce82f9 commit e7842a8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
10 changes: 5 additions & 5 deletions packages/api-rest/src/apis/common/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
} from '../../utils';
import { resolveHeaders } from '../../utils/resolveHeaders';
import { RestApiResponse } from '../../types';
import { iamAuthApplicableGQL } from '../../utils/iamAuthApplicable';

type HandlerOptions = Omit<HttpRequest, 'body' | 'headers'> & {
body?: DocumentType | FormData;
Expand All @@ -47,6 +48,10 @@ export const transferHandler = async (
amplify: AmplifyClassV6,
options: HandlerOptions & { abortSignal: AbortSignal },
signingServiceInfo?: SigningServiceInfo,
iamAuthApplicable: (
{ headers }: HttpRequest,
signingServiceInfo?: SigningServiceInfo,
) => boolean = iamAuthApplicableGQL,
): Promise<RestApiResponse> => {
const { url, method, headers, body, withCredentials, abortSignal } = options;
const resolvedBody = body
Expand Down Expand Up @@ -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<AWSCredentials | null> => {
Expand Down
2 changes: 2 additions & 0 deletions packages/api-rest/src/apis/common/publicApis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
parseSigningInfo,
resolveApiUrl,
} from '../../utils';
import { iamAuthApplicablePublic } from '../../utils/iamAuthApplicable';

import { transferHandler } from './handler';

Expand Down Expand Up @@ -72,6 +73,7 @@ const publicHandler = (
abortSignal,
},
signingServiceInfo,
iamAuthApplicablePublic,
);
});

Expand Down
19 changes: 19 additions & 0 deletions packages/api-rest/src/utils/iamAuthApplicable.ts
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit e7842a8

Please sign in to comment.