Skip to content

Commit 5cf3bb9

Browse files
Steven Yuansyall
authored andcommitted
feat(experimentalIdentityAndAuth): Add generic @aws.auth#sigv4 support
Registers the `@aws.auth#sigv4` scheme.
1 parent 019109d commit 5cf3bb9

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptDependency.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ public enum TypeScriptDependency implements Dependency {
9090
AWS_SDK_NODE_HTTP_HANDLER("dependencies", "@smithy/node-http-handler", "^2.0.5", false),
9191

9292
// Conditionally added when setting the auth middleware.
93-
AWS_SDK_UTIL_MIDDLEWARE("dependencies", "@smithy/util-middleware", "^2.0.0", false),
93+
UTIL_MIDDLEWARE("dependencies", "@smithy/util-middleware", "^2.0.0", false),
94+
@Deprecated AWS_SDK_UTIL_MIDDLEWARE("dependencies", "@smithy/util-middleware", "^2.0.0", false),
9495

9596
// Conditionally added if a event stream shape is found anywhere in the model
9697
AWS_SDK_EVENTSTREAM_SERDE_CONFIG_RESOLVER(
@@ -114,6 +115,9 @@ public enum TypeScriptDependency implements Dependency {
114115
@Deprecated UTIL_STREAM_BROWSER("dependencies", "@smithy/util-stream-browser", "^2.0.5", false),
115116
UTIL_STREAM("dependencies", "@smithy/util-stream", "^2.0.5", false),
116117

118+
// Conditionally added when @aws.auth#sigv4 is used
119+
SIGNATURE_V4("dependencies", "@smithy/signature-v4", "^2.0.4", false),
120+
117121
// feat(experimentalIdentityAndAuth): Conditionally added dependencies for `experimentalIdentityAndAuth`.
118122
// This package should never have a major version, and should only use minor and patch versions in development.
119123
EXPERIMENTAL_IDENTITY_AND_AUTH("dependencies", "@smithy/experimental-identity-and-auth", "~0.0.1", false),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
}

smithy-typescript-codegen/src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ software.amazon.smithy.typescript.codegen.integration.AddDefaultsModeDependency
55
software.amazon.smithy.typescript.codegen.auth.http.integration.AddNoAuthPlugin
66
software.amazon.smithy.typescript.codegen.auth.http.integration.AddHttpApiKeyAuthPlugin
77
software.amazon.smithy.typescript.codegen.auth.http.integration.AddHttpBearerAuthPlugin
8+
software.amazon.smithy.typescript.codegen.auth.http.integration.AddSigV4AuthPlugin
89
software.amazon.smithy.typescript.codegen.integration.AddHttpApiKeyAuthPlugin
910
software.amazon.smithy.typescript.codegen.integration.AddBaseServiceExceptionClass
1011
software.amazon.smithy.typescript.codegen.integration.AddSdkStreamMixinDependency

0 commit comments

Comments
 (0)