Skip to content

Commit 7a5ba72

Browse files
author
Steven Yuan
committed
feat(experimentalIdentityAndAuth): add codegen for HttpAuthExtensionConfiguration
Add code-generation for `HttpAuthExtensionConfiguration`, which allows configuring: - `httpAuthSchemes` - `httpAuthSchemeProvider` - Any `ConfigField` registered by `HttpAuthScheme`s Also, add relative import dependencies for: - `httpAuthSchemeProvider` - `httpAuthExtensionConfiguration`
1 parent d3daa89 commit 7a5ba72

11 files changed

+513
-41
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@
2020
@SmithyUnstableApi
2121
public final record ConfigField(
2222
String name,
23+
Type type,
2324
Consumer<TypeScriptWriter> source,
2425
Consumer<TypeScriptWriter> docs
25-
) {}
26+
) {
27+
/**
28+
* Defines the type of the config field.
29+
*/
30+
@SmithyUnstableApi
31+
public enum Type {
32+
/**
33+
* Specifies the property is important, e.g. {@code apiKey} for {@code @httpApiKeyAuth}
34+
*/
35+
MAIN,
36+
/**
37+
* Specifies the property is auxiliary, e.g. {@code region} for {@code @aws.auth#sigv4}
38+
*/
39+
AUXILIARY
40+
}
41+
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
package software.amazon.smithy.typescript.codegen;
1717

18-
import java.nio.file.Paths;
1918
import java.util.Collections;
2019
import java.util.Iterator;
2120
import java.util.List;
@@ -32,7 +31,6 @@
3231
import software.amazon.smithy.model.shapes.ShapeId;
3332
import software.amazon.smithy.typescript.codegen.auth.AuthUtils;
3433
import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthScheme;
35-
import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthSchemeProviderGenerator;
3634
import software.amazon.smithy.typescript.codegen.auth.http.SupportedHttpAuthSchemesIndex;
3735
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
3836
import software.amazon.smithy.utils.MapUtils;
@@ -212,9 +210,7 @@ private void generateHttpAuthSchemeConfig(
212210
if (target.equals(LanguageTarget.SHARED)) {
213211
configs.put("httpAuthSchemeProvider", w -> {
214212
String providerName = "default" + service.toShapeId().getName() + "HttpAuthSchemeProvider";
215-
w.addRelativeImport(providerName, null, Paths.get(".", CodegenUtils.SOURCE_FOLDER,
216-
HttpAuthSchemeProviderGenerator.HTTP_AUTH_FOLDER,
217-
HttpAuthSchemeProviderGenerator.HTTP_AUTH_SCHEME_RESOLVER_MODULE));
213+
w.addImport(providerName, null, AuthUtils.AUTH_HTTP_PROVIDER_DEPENDENCY);
218214
w.write(providerName);
219215
});
220216
}

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import software.amazon.smithy.model.shapes.OperationShape;
3333
import software.amazon.smithy.model.shapes.ServiceShape;
3434
import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait;
35-
import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthSchemeProviderGenerator;
35+
import software.amazon.smithy.typescript.codegen.auth.AuthUtils;
3636
import software.amazon.smithy.typescript.codegen.auth.http.integration.HttpAuthTypeScriptIntegration;
3737
import software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin;
3838
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
@@ -313,10 +313,7 @@ private void generateClientDefaults() {
313313
writer.write("httpAuthSchemes?: HttpAuthScheme[];\n");
314314

315315
String httpAuthSchemeProviderName = service.toShapeId().getName() + "HttpAuthSchemeProvider";
316-
writer.addRelativeImport(httpAuthSchemeProviderName, null, Paths.get(".",
317-
CodegenUtils.SOURCE_FOLDER,
318-
HttpAuthSchemeProviderGenerator.HTTP_AUTH_FOLDER,
319-
HttpAuthSchemeProviderGenerator.HTTP_AUTH_SCHEME_RESOLVER_MODULE));
316+
writer.addImport(httpAuthSchemeProviderName, null, AuthUtils.AUTH_HTTP_PROVIDER_DEPENDENCY);
320317
writer.writeDocs("""
321318
experimentalIdentityAndAuth: Configuration of an HttpAuthSchemeProvider for a client which \
322319
resolves which HttpAuthScheme to use.

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/AuthUtils.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@
55

66
package software.amazon.smithy.typescript.codegen.auth;
77

8+
import java.nio.file.Paths;
9+
import java.util.Collections;
10+
import java.util.List;
811
import java.util.Map;
912
import java.util.TreeMap;
13+
import software.amazon.smithy.codegen.core.SymbolDependency;
1014
import software.amazon.smithy.model.knowledge.ServiceIndex;
1115
import software.amazon.smithy.model.knowledge.ServiceIndex.AuthSchemeMode;
1216
import software.amazon.smithy.model.shapes.ServiceShape;
1317
import software.amazon.smithy.model.shapes.ShapeId;
18+
import software.amazon.smithy.typescript.codegen.CodegenUtils;
19+
import software.amazon.smithy.typescript.codegen.Dependency;
1420
import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthScheme;
1521
import software.amazon.smithy.typescript.codegen.auth.http.SupportedHttpAuthSchemesIndex;
1622
import software.amazon.smithy.utils.SmithyInternalApi;
@@ -20,6 +26,54 @@
2026
*/
2127
@SmithyInternalApi
2228
public final class AuthUtils {
29+
public static final String HTTP_AUTH_FOLDER = "auth";
30+
31+
public static final String HTTP_AUTH_SCHEME_PROVIDER_MODULE = "httpAuthSchemeProvider";
32+
33+
public static final String HTTP_AUTH_SCHEME_PROVIDER_FILE =
34+
HTTP_AUTH_SCHEME_PROVIDER_MODULE + ".ts";
35+
36+
public static final String HTTP_AUTH_SCHEME_PROVIDER_PATH =
37+
Paths.get(".", CodegenUtils.SOURCE_FOLDER, HTTP_AUTH_FOLDER, HTTP_AUTH_SCHEME_PROVIDER_FILE).toString();
38+
39+
public static final Dependency AUTH_HTTP_PROVIDER_DEPENDENCY = new Dependency() {
40+
@Override
41+
public String getPackageName() {
42+
return Paths.get(
43+
".", CodegenUtils.SOURCE_FOLDER,
44+
HTTP_AUTH_FOLDER, HTTP_AUTH_SCHEME_PROVIDER_MODULE
45+
).toString();
46+
}
47+
48+
@Override
49+
public List<SymbolDependency> getDependencies() {
50+
return Collections.emptyList();
51+
}
52+
};
53+
54+
public static final String HTTP_AUTH_SCHEME_EXTENSION_MODULE = "httpAuthExtensionConfiguration";
55+
56+
public static final String HTTP_AUTH_SCHEME_EXTENSION_FILE =
57+
HTTP_AUTH_SCHEME_EXTENSION_MODULE + ".ts";
58+
59+
public static final String HTTP_AUTH_SCHEME_EXTENSION_PATH =
60+
Paths.get(".", CodegenUtils.SOURCE_FOLDER, HTTP_AUTH_FOLDER, HTTP_AUTH_SCHEME_EXTENSION_FILE).toString();
61+
62+
public static final Dependency AUTH_HTTP_EXTENSION_DEPENDENCY = new Dependency() {
63+
@Override
64+
public String getPackageName() {
65+
return Paths.get(
66+
".", CodegenUtils.SOURCE_FOLDER,
67+
HTTP_AUTH_FOLDER, HTTP_AUTH_SCHEME_EXTENSION_MODULE
68+
).toString();
69+
}
70+
71+
@Override
72+
public List<SymbolDependency> getDependencies() {
73+
return Collections.emptyList();
74+
}
75+
};
76+
2377
private AuthUtils() {}
2478

2579
public static Map<ShapeId, HttpAuthScheme> getAllEffectiveNoAuthAwareAuthSchemes(

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/HttpAuthSchemeProviderGenerator.java

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,6 @@
4141
*/
4242
@SmithyInternalApi
4343
public class HttpAuthSchemeProviderGenerator implements Runnable {
44-
/**
45-
* Directory segment for {@code auth/}.
46-
*/
47-
public static final String HTTP_AUTH_FOLDER = "auth";
48-
/**
49-
* File name segment for {@code httpAuthSchemeProvder}.
50-
*/
51-
public static final String HTTP_AUTH_SCHEME_RESOLVER_MODULE = "httpAuthSchemeProvider";
52-
53-
private static final String HTTP_AUTH_SCHEME_RESOLVER_FILE =
54-
HTTP_AUTH_SCHEME_RESOLVER_MODULE + ".ts";
55-
private static final String HTTP_AUTH_SCHEME_RESOLVER_PATH =
56-
Paths.get(CodegenUtils.SOURCE_FOLDER, HTTP_AUTH_FOLDER, HTTP_AUTH_SCHEME_RESOLVER_FILE).toString();
57-
5844
private final TypeScriptDelegator delegator;
5945
private final Model model;
6046

@@ -104,7 +90,7 @@ export interface WeatherHttpAuthSchemeParameters {
10490
}
10591
*/
10692
private void generateHttpAuthSchemeParametersInterface() {
107-
delegator.useFileWriter(HTTP_AUTH_SCHEME_RESOLVER_PATH.toString(), w -> {
93+
delegator.useFileWriter(AuthUtils.HTTP_AUTH_SCHEME_PROVIDER_PATH, w -> {
10894
w.openBlock("""
10995
/**
11096
* @internal
@@ -138,7 +124,7 @@ export async function defaultWeatherHttpAuthSchemeParametersProvider(
138124
};
139125
*/
140126
private void generateDefaultHttpAuthSchemeParametersProviderFunction() {
141-
delegator.useFileWriter(HTTP_AUTH_SCHEME_RESOLVER_PATH.toString(), w -> {
127+
delegator.useFileWriter(AuthUtils.HTTP_AUTH_SCHEME_PROVIDER_PATH, w -> {
142128
w.addRelativeImport(serviceSymbol.getName() + "ResolvedConfig", null,
143129
Paths.get(".", serviceSymbol.getNamespace()));
144130
w.addImport("HandlerExecutionContext", null, TypeScriptDependency.SMITHY_TYPES);
@@ -190,7 +176,7 @@ function createSmithyApiHttpApiKeyAuthHttpAuthOption(authParameters: WeatherHttp
190176
};
191177
*/
192178
private void generateHttpAuthOptionFunction(ShapeId shapeId, HttpAuthScheme authScheme) {
193-
delegator.useFileWriter(HTTP_AUTH_SCHEME_RESOLVER_PATH.toString(), w -> {
179+
delegator.useFileWriter(AuthUtils.HTTP_AUTH_SCHEME_PROVIDER_PATH, w -> {
194180
String normalizedAuthSchemeName = normalizeAuthSchemeName(shapeId);
195181
w.addDependency(TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
196182
w.addImport("HttpAuthOption", null, TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
@@ -242,7 +228,7 @@ export interface WeatherHttpAuthSchemeProvider {
242228
}
243229
*/
244230
private void generateHttpAuthSchemeProviderInterface() {
245-
delegator.useFileWriter(HTTP_AUTH_SCHEME_RESOLVER_PATH.toString(), w -> {
231+
delegator.useFileWriter(AuthUtils.HTTP_AUTH_SCHEME_PROVIDER_PATH, w -> {
246232
w.write("""
247233
/**
248234
* @internal
@@ -267,7 +253,7 @@ export function defaultWeatherHttpAuthSchemeProvider(authParameters: WeatherHttp
267253
};
268254
*/
269255
private void generateHttpAuthSchemeProviderDefaultFunction() {
270-
delegator.useFileWriter(HTTP_AUTH_SCHEME_RESOLVER_PATH.toString(), w -> {
256+
delegator.useFileWriter(AuthUtils.HTTP_AUTH_SCHEME_PROVIDER_PATH, w -> {
271257
w.openBlock("""
272258
/**
273259
* @internal

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpApiKeyAuthPlugin.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import software.amazon.smithy.typescript.codegen.TypeScriptSettings;
1818
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
1919
import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthOptionProperty;
20-
import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthOptionProperty.Type;
2120
import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthScheme;
2221
import software.amazon.smithy.utils.SmithyInternalApi;
2322

@@ -47,20 +46,20 @@ public Optional<HttpAuthScheme> getHttpAuthScheme() {
4746
return Optional.of(HttpAuthScheme.builder()
4847
.schemeId(HttpApiKeyAuthTrait.ID)
4948
.applicationProtocol(ApplicationProtocol.createDefaultHttpApplicationProtocol())
50-
.addConfigField(new ConfigField("apiKey", w -> {
49+
.addConfigField(new ConfigField("apiKey", ConfigField.Type.MAIN, w -> {
5150
w.addImport("ApiKeyIdentity", null,
5251
TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
5352
w.addImport("ApiKeyIdentityProvider", null,
5453
TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
5554
w.write("ApiKeyIdentity | ApiKeyIdentityProvider");
5655
}, w -> w.write("The API key to use when making requests.")))
5756
.addHttpAuthOptionProperty(new HttpAuthOptionProperty(
58-
"name", Type.SIGNING, t -> w -> {
57+
"name", HttpAuthOptionProperty.Type.SIGNING, t -> w -> {
5958
HttpApiKeyAuthTrait httpApiKeyAuthTrait = (HttpApiKeyAuthTrait) t;
6059
w.write("$S", httpApiKeyAuthTrait.getName());
6160
}))
6261
.addHttpAuthOptionProperty(new HttpAuthOptionProperty(
63-
"in", Type.SIGNING, t -> w -> {
62+
"in", HttpAuthOptionProperty.Type.SIGNING, t -> w -> {
6463
w.addImport("HttpApiKeyAuthLocation", null,
6564
TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
6665
HttpApiKeyAuthTrait httpApiKeyAuthTrait = (HttpApiKeyAuthTrait) t;
@@ -74,7 +73,7 @@ public Optional<HttpAuthScheme> getHttpAuthScheme() {
7473
}
7574
}))
7675
.addHttpAuthOptionProperty(new HttpAuthOptionProperty(
77-
"scheme", Type.SIGNING, t -> w -> {
76+
"scheme", HttpAuthOptionProperty.Type.SIGNING, t -> w -> {
7877
HttpApiKeyAuthTrait httpApiKeyAuthTrait = (HttpApiKeyAuthTrait) t;
7978
httpApiKeyAuthTrait.getScheme().ifPresentOrElse(
8079
s -> w.write(s),

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpBearerAuthPlugin.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import software.amazon.smithy.model.traits.HttpBearerAuthTrait;
1111
import software.amazon.smithy.typescript.codegen.ApplicationProtocol;
1212
import software.amazon.smithy.typescript.codegen.ConfigField;
13+
import software.amazon.smithy.typescript.codegen.ConfigField.Type;
1314
import software.amazon.smithy.typescript.codegen.LanguageTarget;
1415
import software.amazon.smithy.typescript.codegen.TypeScriptDependency;
1516
import software.amazon.smithy.typescript.codegen.TypeScriptSettings;
@@ -43,7 +44,7 @@ public Optional<HttpAuthScheme> getHttpAuthScheme() {
4344
return Optional.of(HttpAuthScheme.builder()
4445
.schemeId(HttpBearerAuthTrait.ID)
4546
.applicationProtocol(ApplicationProtocol.createDefaultHttpApplicationProtocol())
46-
.addConfigField(new ConfigField("token", w -> {
47+
.addConfigField(new ConfigField("token", Type.MAIN, w -> {
4748
w.addDependency(TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
4849
w.addImport("TokenIdentity", null,
4950
TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddSigV4AuthPlugin.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import software.amazon.smithy.typescript.codegen.TypeScriptSettings;
1616
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
1717
import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthOptionProperty;
18-
import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthOptionProperty.Type;
1918
import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthScheme;
2019
import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthSchemeParameter;
2120
import software.amazon.smithy.utils.SmithyInternalApi;
@@ -47,12 +46,12 @@ public Optional<HttpAuthScheme> getHttpAuthScheme() {
4746
.schemeId(ShapeId.from("aws.auth#sigv4"))
4847
.applicationProtocol(ApplicationProtocol.createDefaultHttpApplicationProtocol())
4948
.putDefaultSigner(LanguageTarget.SHARED, AWS_SIGV4_AUTH_SIGNER)
50-
.addConfigField(new ConfigField("region", w -> {
49+
.addConfigField(new ConfigField("region", ConfigField.Type.AUXILIARY, w -> {
5150
w.addDependency(TypeScriptDependency.SMITHY_TYPES);
5251
w.addImport("Provider", "__Provider", TypeScriptDependency.SMITHY_TYPES);
5352
w.write("string | __Provider<string>");
5453
}, w -> w.write("The AWS region to which this client will send requests.")))
55-
.addConfigField(new ConfigField("credentials", w -> {
54+
.addConfigField(new ConfigField("credentials", ConfigField.Type.MAIN, w -> {
5655
w.addDependency(TypeScriptDependency.SMITHY_TYPES);
5756
w.addImport("AwsCredentialIdentity", null, TypeScriptDependency.SMITHY_TYPES);
5857
w.addImport("AwsCredentialIdentityProvider", null, TypeScriptDependency.SMITHY_TYPES);
@@ -67,11 +66,11 @@ public Optional<HttpAuthScheme> getHttpAuthScheme() {
6766
});
6867
}))
6968
.addHttpAuthOptionProperty(new HttpAuthOptionProperty(
70-
"name", Type.SIGNING, t -> w -> {
69+
"name", HttpAuthOptionProperty.Type.SIGNING, t -> w -> {
7170
w.write("$S", t.toNode().expectObjectNode().getMember("name"));
7271
}))
7372
.addHttpAuthOptionProperty(new HttpAuthOptionProperty(
74-
"region", Type.SIGNING, t -> w -> {
73+
"region", HttpAuthOptionProperty.Type.SIGNING, t -> w -> {
7574
w.write("authParameters.region");
7675
}))
7776
.build());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 software.amazon.smithy.typescript.codegen.Dependency;
9+
import software.amazon.smithy.typescript.codegen.auth.AuthUtils;
10+
import software.amazon.smithy.typescript.codegen.extensions.ExtensionConfigurationInterface;
11+
import software.amazon.smithy.utils.Pair;
12+
import software.amazon.smithy.utils.SmithyInternalApi;
13+
14+
/**
15+
* Adds the corresponding interface and functions for {@code HttpAuthExtensionConfiguration}.
16+
*
17+
* This is experimental for `experimentalIdentityAndAuth`.
18+
*/
19+
@SmithyInternalApi
20+
public class HttpAuthExtensionConfigurationInterface implements ExtensionConfigurationInterface {
21+
@Override
22+
public Pair<String, Dependency> name() {
23+
return Pair.of("HttpAuthExtensionConfiguration", AuthUtils.AUTH_HTTP_EXTENSION_DEPENDENCY);
24+
}
25+
26+
@Override
27+
public Pair<String, Dependency> getExtensionConfigurationFn() {
28+
return Pair.of("getHttpAuthExtensionConfiguration", AuthUtils.AUTH_HTTP_EXTENSION_DEPENDENCY);
29+
}
30+
31+
@Override
32+
public Pair<String, Dependency> resolveRuntimeConfigFn() {
33+
return Pair.of("resolveHttpAuthRuntimeConfig", AuthUtils.AUTH_HTTP_EXTENSION_DEPENDENCY);
34+
}
35+
}

0 commit comments

Comments
 (0)