-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow x-api-key for public rest api (#13422)
* refactor iamAuthApplicable * cleanup & test * update to sample token * clean lint error * cleanup signingService type * update function maing * moving default to internalPost * rename file * update ts docs * add test with specified service in test suite
- Loading branch information
Showing
7 changed files
with
179 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
packages/api-rest/__tests__/utils/isIamAuthApplicable.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// 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'; | ||
|
||
import { | ||
isIamAuthApplicableForGraphQL, | ||
isIamAuthApplicableForRest, | ||
} from '../../src/utils/isIamAuthApplicable'; | ||
|
||
describe('iamAuthApplicable', () => { | ||
const url = new URL('https://url'); | ||
const baseRequest: HttpRequest = { | ||
headers: {}, | ||
url, | ||
method: 'put', | ||
}; | ||
|
||
describe('iamAuthApplicableForGraphQL', () => { | ||
it('should return true if there is no authorization header, no x-api-key header, and signingServiceInfo is provided', () => { | ||
const signingServiceInfo = {}; | ||
expect( | ||
isIamAuthApplicableForGraphQL(baseRequest, signingServiceInfo), | ||
).toBe(true); | ||
}); | ||
|
||
it('should return false if there is an authorization header', () => { | ||
const request = { | ||
...baseRequest, | ||
headers: { authorization: 'SampleToken' }, | ||
}; | ||
const signingServiceInfo = {}; | ||
expect(isIamAuthApplicableForGraphQL(request, signingServiceInfo)).toBe( | ||
false, | ||
); | ||
}); | ||
|
||
it('should return false if there is an x-api-key header', () => { | ||
const request = { ...baseRequest, headers: { 'x-api-key': 'key' } }; | ||
const signingServiceInfo = {}; | ||
expect(isIamAuthApplicableForGraphQL(request, signingServiceInfo)).toBe( | ||
false, | ||
); | ||
}); | ||
|
||
it('should return false if signingServiceInfo is not provided', () => { | ||
expect(isIamAuthApplicableForGraphQL(baseRequest)).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('iamAuthApplicableForPublic', () => { | ||
it('should return true if there is no authorization header and signingServiceInfo is provided', () => { | ||
const signingServiceInfo = {}; | ||
expect(isIamAuthApplicableForRest(baseRequest, signingServiceInfo)).toBe( | ||
true, | ||
); | ||
}); | ||
|
||
it('should return false if there is an authorization header', () => { | ||
const request = { | ||
...baseRequest, | ||
headers: { authorization: 'SampleToken' }, | ||
}; | ||
const signingServiceInfo = {}; | ||
expect(isIamAuthApplicableForRest(request, signingServiceInfo)).toBe( | ||
false, | ||
); | ||
}); | ||
|
||
it('should return false if signingServiceInfo is not provided', () => { | ||
expect(isIamAuthApplicableForRest(baseRequest)).toBe(false); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// 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'; | ||
|
||
import { SigningServiceInfo } from '../types'; | ||
|
||
/** | ||
* Determines if IAM authentication should be applied for a GraphQL request. | ||
* | ||
* This function checks the `headers` of the HTTP request to determine if IAM authentication | ||
* is applicable. IAM authentication is considered applicable if there is no `authorization` | ||
* header, no `x-api-key` header, and `signingServiceInfo` is provided. | ||
* | ||
* @param request - The HTTP request object containing headers. | ||
* @param signingServiceInfo - Optional signing service information, | ||
* including service and region. | ||
* @returns A boolean `true` if IAM authentication should be applied. | ||
* | ||
* @internal | ||
*/ | ||
export const isIamAuthApplicableForGraphQL = ( | ||
{ headers }: HttpRequest, | ||
signingServiceInfo?: SigningServiceInfo, | ||
) => !headers.authorization && !headers['x-api-key'] && !!signingServiceInfo; | ||
|
||
/** | ||
* Determines if IAM authentication should be applied for a REST request. | ||
* | ||
* This function checks the `headers` of the HTTP request to determine if IAM authentication | ||
* is applicable. IAM authentication is considered applicable if there is no `authorization` | ||
* header and `signingServiceInfo` is provided. | ||
* | ||
* @param request - The HTTP request object containing headers. | ||
* @param signingServiceInfo - Optional signing service information, | ||
* including service and region. | ||
* @returns A boolean `true` if IAM authentication should be applied. | ||
* | ||
* @internal | ||
*/ | ||
export const isIamAuthApplicableForRest = ( | ||
{ headers }: HttpRequest, | ||
signingServiceInfo?: SigningServiceInfo, | ||
) => !headers.authorization && !!signingServiceInfo; |