Skip to content

Commit 07fe7d4

Browse files
author
Dane Pilcher
authored
fix: allow apiKeyAuthorizationMode to be undefined if defaultAuthorizationMode is apiKey (#2329)
1 parent f1dbf71 commit 07fe7d4

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

.changeset/itchy-flowers-reply.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@aws-amplify/backend-data': patch
3+
'@aws-amplify/backend': patch
4+
---
5+
6+
Allow apiKeyAuthorizationMode to be undefined if defaultAuthorizationMode is apiKey

packages/backend-data/src/convert_authorization_modes.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ void describe('convertAuthorizationModesToCDK', () => {
8787
defaultAuthorizationMode: 'API_KEY',
8888
apiKeyConfig: {
8989
expires: Duration.days(7),
90+
description: undefined,
9091
},
9192
iamConfig: {
9293
enableIamAuthorizationMode: true,
@@ -103,6 +104,26 @@ void describe('convertAuthorizationModesToCDK', () => {
103104
);
104105
});
105106

107+
void it('defaults api key expiry if default auth mode is api key and apiKeyConfig is undefined', () => {
108+
const expectedOutput: CDKAuthorizationModes = {
109+
defaultAuthorizationMode: 'API_KEY',
110+
apiKeyConfig: {
111+
expires: Duration.days(7),
112+
description: undefined,
113+
},
114+
iamConfig: {
115+
enableIamAuthorizationMode: true,
116+
},
117+
};
118+
119+
assert.deepStrictEqual(
120+
convertAuthorizationModesToCDK(getInstancePropsStub, undefined, {
121+
defaultAuthorizationMode: 'apiKey',
122+
}),
123+
expectedOutput
124+
);
125+
});
126+
106127
void it('defaults to user pool auth if a user pool is present in provided auth resources', () => {
107128
const expectedOutput: CDKAuthorizationModes = {
108129
defaultAuthorizationMode: 'AMAZON_COGNITO_USER_POOLS',

packages/backend-data/src/convert_authorization_modes.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export const buildConstructFactoryProvidedAuthConfig = (
6060
const convertApiKeyAuthConfigToCDK = ({
6161
description,
6262
expiresInDays = DEFAULT_API_KEY_EXPIRATION_DAYS,
63-
}: ApiKeyAuthorizationModeProps): CDKApiKeyAuthorizationConfig => ({
63+
}: ApiKeyAuthorizationModeProps = {}): CDKApiKeyAuthorizationConfig => ({
6464
description,
6565
expires: Duration.days(expiresInDays),
6666
});
@@ -207,9 +207,12 @@ export const convertAuthorizationModesToCDK = (
207207
const cdkAuthorizationMode = convertAuthorizationModeToCDK(
208208
defaultAuthorizationMode
209209
);
210-
const apiKeyConfig = authModes?.apiKeyAuthorizationMode
211-
? convertApiKeyAuthConfigToCDK(authModes.apiKeyAuthorizationMode)
212-
: computeApiKeyAuthFromResource(authResources, authModes);
210+
const apiKeyConfig =
211+
authModes?.apiKeyAuthorizationMode ||
212+
// If default auth mode is apiKey, don't require apiKeyAuthorizationMode to be defined
213+
defaultAuthorizationMode === 'apiKey'
214+
? convertApiKeyAuthConfigToCDK(authModes?.apiKeyAuthorizationMode)
215+
: computeApiKeyAuthFromResource(authResources, authModes);
213216
const userPoolConfig = computeUserPoolAuthFromResource(authResources);
214217
const identityPoolConfig = computeIdentityPoolAuthFromResource(authResources);
215218
const lambdaConfig = authModes?.lambdaAuthorizationMode

0 commit comments

Comments
 (0)