|
| 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.typescript.codegen.auth.http.integration; |
| 7 | + |
| 8 | +import java.util.Optional; |
| 9 | +import software.amazon.smithy.model.shapes.ShapeId; |
| 10 | +import software.amazon.smithy.typescript.codegen.ApplicationProtocol; |
| 11 | +import software.amazon.smithy.typescript.codegen.ConfigField; |
| 12 | +import software.amazon.smithy.typescript.codegen.LanguageTarget; |
| 13 | +import software.amazon.smithy.typescript.codegen.TypeScriptDependency; |
| 14 | +import software.amazon.smithy.typescript.codegen.TypeScriptSettings; |
| 15 | +import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthOptionProperty; |
| 16 | +import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthOptionProperty.Type; |
| 17 | +import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthScheme; |
| 18 | +import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthSchemeParameter; |
| 19 | +import software.amazon.smithy.utils.SmithyInternalApi; |
| 20 | + |
| 21 | +/** |
| 22 | + * Support for generic @aws.auth#sigv4. |
| 23 | + * |
| 24 | + * This is the experimental behavior for `experimentalIdentityAndAuth`. |
| 25 | + */ |
| 26 | +@SmithyInternalApi |
| 27 | +public final class AddSigV4AuthPlugin implements HttpAuthTypeScriptIntegration { |
| 28 | + |
| 29 | + /** |
| 30 | + * Integration should only be used if `experimentalIdentityAndAuth` flag is true. |
| 31 | + */ |
| 32 | + @Override |
| 33 | + public boolean matchesSettings(TypeScriptSettings settings) { |
| 34 | + return settings.getExperimentalIdentityAndAuth(); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public Optional<HttpAuthScheme> getHttpAuthScheme() { |
| 39 | + return Optional.of(HttpAuthScheme.builder() |
| 40 | + .schemeId(ShapeId.from("aws.auth#sigv4")) |
| 41 | + .applicationProtocol(ApplicationProtocol.createDefaultHttpApplicationProtocol()) |
| 42 | + .putDefaultIdentityProvider(LanguageTarget.SHARED, w -> { |
| 43 | + w.write("async () => { throw new Error(\"`credentials` is missing\"); }"); |
| 44 | + }) |
| 45 | + .putDefaultSigner(LanguageTarget.SHARED, w -> { |
| 46 | + w.addDependency(TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH); |
| 47 | + w.addImport("SigV4Signer", null, |
| 48 | + TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH); |
| 49 | + w.write("new SigV4Signer()"); |
| 50 | + }) |
| 51 | + .addConfigField(new ConfigField("region", w -> { |
| 52 | + w.addDependency(TypeScriptDependency.SMITHY_TYPES); |
| 53 | + w.addImport("Provider", "__Provider", TypeScriptDependency.SMITHY_TYPES); |
| 54 | + w.write("string | __Provider<string>"); |
| 55 | + }, w -> w.write("The AWS region to which this client will send requests."))) |
| 56 | + .addConfigField(new ConfigField("credentials", w -> { |
| 57 | + w.addDependency(TypeScriptDependency.SMITHY_TYPES); |
| 58 | + w.addImport("AwsCredentialIdentity", null, TypeScriptDependency.SMITHY_TYPES); |
| 59 | + w.addImport("AwsCredentialIdentityProvider", null, TypeScriptDependency.SMITHY_TYPES); |
| 60 | + w.write("AwsCredentialIdentity | AwsCredentialIdentityProvider"); |
| 61 | + }, w -> w.write("The credentials used to sign requests."))) |
| 62 | + .addHttpAuthSchemeParameter(new HttpAuthSchemeParameter( |
| 63 | + "region", w -> w.write("string"), w -> { |
| 64 | + w.addDependency(TypeScriptDependency.UTIL_MIDDLEWARE); |
| 65 | + w.addImport("normalizeProvider", null, TypeScriptDependency.UTIL_MIDDLEWARE); |
| 66 | + w.openBlock("await normalizeProvider(config.region)() || (() => {", "})()", () -> { |
| 67 | + w.write("throw new Error(\"expected `region` to be configured for `aws.auth#sigv4`\");"); |
| 68 | + }); |
| 69 | + })) |
| 70 | + .addHttpAuthOptionProperty(new HttpAuthOptionProperty( |
| 71 | + "name", Type.SIGNING, t -> w -> { |
| 72 | + w.write("$S", t.toNode().expectObjectNode().getMember("name")); |
| 73 | + })) |
| 74 | + .addHttpAuthOptionProperty(new HttpAuthOptionProperty( |
| 75 | + "region", Type.SIGNING, t -> w -> { |
| 76 | + w.write("authParameters.region"); |
| 77 | + })) |
| 78 | + .build()); |
| 79 | + } |
| 80 | +} |
0 commit comments