|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package software.amazon.smithy.aws.typescript.codegen; |
| 7 | + |
| 8 | +import java.util.Collections; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.function.Consumer; |
| 11 | +import software.amazon.smithy.aws.traits.auth.SigV4ATrait; |
| 12 | +import software.amazon.smithy.codegen.core.SymbolProvider; |
| 13 | +import software.amazon.smithy.model.Model; |
| 14 | +import software.amazon.smithy.model.knowledge.ServiceIndex; |
| 15 | +import software.amazon.smithy.typescript.codegen.LanguageTarget; |
| 16 | +import software.amazon.smithy.typescript.codegen.TypeScriptSettings; |
| 17 | +import software.amazon.smithy.typescript.codegen.TypeScriptWriter; |
| 18 | +import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration; |
| 19 | +import software.amazon.smithy.utils.MapUtils; |
| 20 | +import software.amazon.smithy.utils.SmithyInternalApi; |
| 21 | + |
| 22 | +/** |
| 23 | + * Detects when to add sigv4a signer. |
| 24 | + */ |
| 25 | +@SmithyInternalApi |
| 26 | +public final class AddSigv4aPlugin implements TypeScriptIntegration { |
| 27 | + |
| 28 | + @Override |
| 29 | + public boolean matchesSettings(TypeScriptSettings settings) { |
| 30 | + return !settings.getExperimentalIdentityAndAuth(); |
| 31 | + } |
| 32 | + |
| 33 | + public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters( |
| 34 | + TypeScriptSettings settings, |
| 35 | + Model model, |
| 36 | + SymbolProvider symbolProvider, |
| 37 | + LanguageTarget target |
| 38 | + ) { |
| 39 | + final boolean addSigv4aSignerToConfig = ServiceIndex.of(model) |
| 40 | + .getEffectiveAuthSchemes(settings.getService(), ServiceIndex.AuthSchemeMode.NO_AUTH_AWARE) |
| 41 | + .containsKey(SigV4ATrait.ID); |
| 42 | + |
| 43 | + // the sigv4a trait also appears on operations, but we will not check |
| 44 | + // them individually because it must appear on the service as well in that case. |
| 45 | + |
| 46 | + if (!addSigv4aSignerToConfig) { |
| 47 | + return Collections.emptyMap(); |
| 48 | + } |
| 49 | + |
| 50 | + // TODO: add to shared config when sigv4a implementation is available in browser. |
| 51 | + switch (target) { |
| 52 | + case NODE: |
| 53 | + return MapUtils.of("signerConstructor", writer -> { |
| 54 | + writer |
| 55 | + .addDependency(AwsDependency.SIGNATURE_V4_MULTIREGION) |
| 56 | + .addImport("SignatureV4MultiRegion", null, AwsDependency.SIGNATURE_V4_MULTIREGION) |
| 57 | + .write("SignatureV4MultiRegion"); |
| 58 | + }); |
| 59 | + default: |
| 60 | + return Collections.emptyMap(); |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments