diff --git a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddSigv4aPlugin.java b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddSigv4aPlugin.java new file mode 100644 index 000000000000..5bb508e06296 --- /dev/null +++ b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddSigv4aPlugin.java @@ -0,0 +1,63 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +package software.amazon.smithy.aws.typescript.codegen; + +import java.util.Collections; +import java.util.Map; +import java.util.function.Consumer; +import software.amazon.smithy.aws.traits.auth.SigV4ATrait; +import software.amazon.smithy.codegen.core.SymbolProvider; +import software.amazon.smithy.model.Model; +import software.amazon.smithy.model.knowledge.ServiceIndex; +import software.amazon.smithy.typescript.codegen.LanguageTarget; +import software.amazon.smithy.typescript.codegen.TypeScriptSettings; +import software.amazon.smithy.typescript.codegen.TypeScriptWriter; +import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration; +import software.amazon.smithy.utils.MapUtils; +import software.amazon.smithy.utils.SmithyInternalApi; + +/** + * Detects when to add sigv4a signer. + */ +@SmithyInternalApi +public final class AddSigv4aPlugin implements TypeScriptIntegration { + + @Override + public boolean matchesSettings(TypeScriptSettings settings) { + return !settings.getExperimentalIdentityAndAuth(); + } + + public Map> getRuntimeConfigWriters( + TypeScriptSettings settings, + Model model, + SymbolProvider symbolProvider, + LanguageTarget target + ) { + final boolean addSigv4aSignerToConfig = ServiceIndex.of(model) + .getEffectiveAuthSchemes(settings.getService(), ServiceIndex.AuthSchemeMode.NO_AUTH_AWARE) + .containsKey(SigV4ATrait.ID); + + // the sigv4a trait also appears on operations, but we will not check + // them individually because it must appear on the service as well in that case. + + if (!addSigv4aSignerToConfig) { + return Collections.emptyMap(); + } + + // TODO: add to shared config when sigv4a implementation is available in browser. + switch (target) { + case NODE: + return MapUtils.of("signerConstructor", writer -> { + writer + .addDependency(AwsDependency.SIGNATURE_V4_MULTIREGION) + .addImport("SignatureV4MultiRegion", null, AwsDependency.SIGNATURE_V4_MULTIREGION) + .write("SignatureV4MultiRegion"); + }); + default: + return Collections.emptyMap(); + } + } +} diff --git a/codegen/smithy-aws-typescript-codegen/src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration b/codegen/smithy-aws-typescript-codegen/src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration index fe9a30a3ff6a..8a4c954bb6c2 100644 --- a/codegen/smithy-aws-typescript-codegen/src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration +++ b/codegen/smithy-aws-typescript-codegen/src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration @@ -24,6 +24,7 @@ software.amazon.smithy.aws.typescript.codegen.AddDocumentClientPlugin software.amazon.smithy.aws.typescript.codegen.AddEndpointDiscoveryPlugin software.amazon.smithy.aws.typescript.codegen.AddHttpChecksumDependency software.amazon.smithy.aws.typescript.codegen.AddEventBridgePlugin +software.amazon.smithy.aws.typescript.codegen.AddSigv4aPlugin software.amazon.smithy.aws.typescript.codegen.AddCloudFrontKeyValueStorePlugin software.amazon.smithy.aws.typescript.codegen.auth.http.integration.AwsSdkCustomizeHttpBearerTokenAuth software.amazon.smithy.aws.typescript.codegen.auth.http.integration.SupportSigV4Auth