Skip to content

Commit

Permalink
fix: allow apiKeyAuthorizationMode to be undefined if defaultAuthoriz…
Browse files Browse the repository at this point in the history
…ationMode is apiKey (#2329)
  • Loading branch information
dpilch authored Dec 17, 2024
1 parent f1dbf71 commit 07fe7d4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/itchy-flowers-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@aws-amplify/backend-data': patch
'@aws-amplify/backend': patch
---

Allow apiKeyAuthorizationMode to be undefined if defaultAuthorizationMode is apiKey
21 changes: 21 additions & 0 deletions packages/backend-data/src/convert_authorization_modes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ void describe('convertAuthorizationModesToCDK', () => {
defaultAuthorizationMode: 'API_KEY',
apiKeyConfig: {
expires: Duration.days(7),
description: undefined,
},
iamConfig: {
enableIamAuthorizationMode: true,
Expand All @@ -103,6 +104,26 @@ void describe('convertAuthorizationModesToCDK', () => {
);
});

void it('defaults api key expiry if default auth mode is api key and apiKeyConfig is undefined', () => {
const expectedOutput: CDKAuthorizationModes = {
defaultAuthorizationMode: 'API_KEY',
apiKeyConfig: {
expires: Duration.days(7),
description: undefined,
},
iamConfig: {
enableIamAuthorizationMode: true,
},
};

assert.deepStrictEqual(
convertAuthorizationModesToCDK(getInstancePropsStub, undefined, {
defaultAuthorizationMode: 'apiKey',
}),
expectedOutput
);
});

void it('defaults to user pool auth if a user pool is present in provided auth resources', () => {
const expectedOutput: CDKAuthorizationModes = {
defaultAuthorizationMode: 'AMAZON_COGNITO_USER_POOLS',
Expand Down
11 changes: 7 additions & 4 deletions packages/backend-data/src/convert_authorization_modes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const buildConstructFactoryProvidedAuthConfig = (
const convertApiKeyAuthConfigToCDK = ({
description,
expiresInDays = DEFAULT_API_KEY_EXPIRATION_DAYS,
}: ApiKeyAuthorizationModeProps): CDKApiKeyAuthorizationConfig => ({
}: ApiKeyAuthorizationModeProps = {}): CDKApiKeyAuthorizationConfig => ({
description,
expires: Duration.days(expiresInDays),
});
Expand Down Expand Up @@ -207,9 +207,12 @@ export const convertAuthorizationModesToCDK = (
const cdkAuthorizationMode = convertAuthorizationModeToCDK(
defaultAuthorizationMode
);
const apiKeyConfig = authModes?.apiKeyAuthorizationMode
? convertApiKeyAuthConfigToCDK(authModes.apiKeyAuthorizationMode)
: computeApiKeyAuthFromResource(authResources, authModes);
const apiKeyConfig =
authModes?.apiKeyAuthorizationMode ||
// If default auth mode is apiKey, don't require apiKeyAuthorizationMode to be defined
defaultAuthorizationMode === 'apiKey'
? convertApiKeyAuthConfigToCDK(authModes?.apiKeyAuthorizationMode)
: computeApiKeyAuthFromResource(authResources, authModes);
const userPoolConfig = computeUserPoolAuthFromResource(authResources);
const identityPoolConfig = computeIdentityPoolAuthFromResource(authResources);
const lambdaConfig = authModes?.lambdaAuthorizationMode
Expand Down

0 comments on commit 07fe7d4

Please sign in to comment.