Skip to content

Commit

Permalink
feat(experimentalIdentityAndAuth): update AddHttpAuthSchemeMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Yuan committed Nov 9, 2023
1 parent 2724ccf commit 1d4d78b
Showing 1 changed file with 23 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@

package software.amazon.smithy.typescript.codegen.auth.http.integration;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import software.amazon.smithy.codegen.core.CodegenException;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.knowledge.ServiceIndex;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.ShapeId;
Expand Down Expand Up @@ -76,39 +72,14 @@ public List<RuntimeClientPlugin> getClientPlugins() {
);
}

@Override
public void addConfigInterfaceFields(
TypeScriptSettings settings,
Model model,
SymbolProvider symbolProvider,
TypeScriptWriter writer
) {
writer.addDependency(TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
writer.addImport("HttpAuthScheme", null, TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
writer.writeDocs("""
experimentalIdentityAndAuth: Configuration of HttpAuthSchemes for a client which provides \
default identity providers and signers per auth scheme.
@internal""");
writer.write("httpAuthSchemes?: HttpAuthScheme[];\n");

String httpAuthSchemeProviderName = CodegenUtils.getServiceName(settings, model, symbolProvider)
+ "HttpAuthSchemeProvider";
writer.addImport(httpAuthSchemeProviderName, null, AuthUtils.AUTH_HTTP_PROVIDER_DEPENDENCY);
writer.writeDocs("""
experimentalIdentityAndAuth: Configuration of an HttpAuthSchemeProvider for a client which \
resolves which HttpAuthScheme to use.
@internal""");
writer.write("httpAuthSchemeProvider?: $L;\n", httpAuthSchemeProviderName);
}

@Override
public void customize(TypeScriptCodegenContext codegenContext) {
if (!codegenContext.settings().generateClient()) {
return;
}
codegenContext.writerDelegator().useFileWriter(AuthUtils.HTTP_AUTH_SCHEME_PROVIDER_PATH, w -> {
SupportedHttpAuthSchemesIndex authIndex = new SupportedHttpAuthSchemesIndex(codegenContext.integrations());
String service = CodegenUtils.getServiceName(
String serviceName = CodegenUtils.getServiceName(
codegenContext.settings(), codegenContext.model(), codegenContext.symbolProvider());
ServiceShape serviceShape = codegenContext.settings().getService(codegenContext.model());
ServiceIndex serviceIndex = ServiceIndex.of(codegenContext.model());
Expand All @@ -117,9 +88,9 @@ public void customize(TypeScriptCodegenContext codegenContext) {
Map<String, ConfigField> configFields =
AuthUtils.collectConfigFields(httpAuthSchemes.values());

generateHttpAuthSchemeInputConfigInterface(w, configFields);
generateHttpAuthSchemeResolvedConfigInterface(w, configFields, service);
generateResolveHttpAuthSchemeConfigFunction(w, configFields, httpAuthSchemes, authIndex, service);
generateHttpAuthSchemeInputConfigInterface(w, configFields, serviceName);
generateHttpAuthSchemeResolvedConfigInterface(w, configFields);
generateResolveHttpAuthSchemeConfigFunction(w, configFields, httpAuthSchemes, authIndex);
});
}

Expand All @@ -136,13 +107,29 @@ export interface HttpAuthSchemeInputConfig {
*/
private void generateHttpAuthSchemeInputConfigInterface(
TypeScriptWriter w,
Map<String, ConfigField> configFields
Map<String, ConfigField> configFields,
String serviceName
) {
w.openBlock("""
/**
* @internal
*/
export interface HttpAuthSchemeInputConfig {""", "}\n", () -> {
w.addDependency(TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
w.addImport("HttpAuthScheme", null, TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
w.writeDocs("""
experimentalIdentityAndAuth: Configuration of HttpAuthSchemes for a client which provides \
default identity providers and signers per auth scheme.
@internal""");
w.write("httpAuthSchemes?: HttpAuthScheme[];\n");

String httpAuthSchemeProviderName = serviceName + "HttpAuthSchemeProvider";
w.writeDocs("""
experimentalIdentityAndAuth: Configuration of an HttpAuthSchemeProvider for a client which \
resolves which HttpAuthScheme to use.
@internal""");
w.write("httpAuthSchemeProvider?: $L;\n", httpAuthSchemeProviderName);

for (ConfigField configField : configFields.values()) {
w.writeDocs(() -> w.write("$C", configField.docs()));
w.write("$L?: $C;", configField.name(), configField.inputType());
Expand All @@ -159,16 +146,11 @@ export interface HttpAuthSchemeResolvedConfig {
readonly region?: __Provider<string>;
readonly credentials?: AwsCredentialIdentityProvider;
readonly httpAuthSchemeParametersProvider: WeatherHttpAuthSchemeParametersProvider;
readonly identityProviderConfig: IdentityProviderConfig;
}
*/
private void generateHttpAuthSchemeResolvedConfigInterface(
TypeScriptWriter w,
Map<String, ConfigField> configFields,
String service
Map<String, ConfigField> configFields
) {
w.openBlock("""
/**
Expand All @@ -179,18 +161,6 @@ export interface HttpAuthSchemeResolvedConfig {""", "}\n", () -> {
w.writeDocs(() -> w.write("$C", configField.docs()));
w.write("readonly $L?: $C;", configField.name(), configField.resolvedType());
}
w.writeDocs("""
experimentalIdentityAndAuth: provides parameters for HttpAuthSchemeProvider.
@internal""");
w.write("readonly httpAuthSchemeParametersProvider: $LHttpAuthSchemeParametersProvider;",
service);

w.addDependency(TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
w.addImport("IdentityProviderConfig", null, TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
w.writeDocs("""
experimentalIdentityAndAuth: abstraction around identity configuration fields
@internal""");
w.write("readonly identityProviderConfig: IdentityProviderConfig;");
});
}

Expand All @@ -206,21 +176,14 @@ export interface HttpAuthSchemeResolvedConfig {""", "}\n", () -> {
region,
apiKey,
token,
httpAuthSchemeParametersProvider: defaultWeatherHttpAuthSchemeParametersProvider,
identityProviderConfig: new DefaultIdentityProviderConfig({
"aws.auth#sigv4": credentials,
"smithy.api#httpApiKeyAuth": apiKey,
"smithy.api#httpBearerAuth": token,
}),
};
};
*/
private void generateResolveHttpAuthSchemeConfigFunction(
TypeScriptWriter w,
Map<String, ConfigField> configFields,
Map<ShapeId, HttpAuthScheme> httpAuthSchemes,
SupportedHttpAuthSchemesIndex authIndex,
String service
SupportedHttpAuthSchemesIndex authIndex
) {
w.openBlock("""
/**
Expand All @@ -229,8 +192,6 @@ private void generateResolveHttpAuthSchemeConfigFunction(
export const resolveHttpAuthSchemeConfig = (config: HttpAuthSchemeInputConfig): \
HttpAuthSchemeResolvedConfig => {""", "};", () -> {
w.addDependency(TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
w.addImport("DefaultIdentityProviderConfig", null,
TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
for (ConfigField configField : configFields.values()) {
if (configField.type().equals(ConfigField.Type.MAIN)) {
w.addDependency(TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
Expand Down Expand Up @@ -260,38 +221,6 @@ private void generateResolveHttpAuthSchemeConfigFunction(
for (ConfigField configField : configFields.values()) {
w.write("$L,", configField.name());
}
w.write("httpAuthSchemeParametersProvider: $T,",
authIndex.getDefaultHttpAuthSchemeParametersProvider()
.orElse(Symbol.builder()
.name("default" + service + "HttpAuthSchemeParametersProvider")
.build()));

w.openBlock("identityProviderConfig: new DefaultIdentityProviderConfig({", "}),", () -> {
Map<String, ConfigField> visitedConfigFields = new HashMap<>();
for (HttpAuthScheme scheme : httpAuthSchemes.values()) {
if (scheme == null) {
continue;
}
for (ConfigField configField : scheme.getConfigFields()) {
if (visitedConfigFields.containsKey(configField.name())) {
ConfigField visitedConfigField = visitedConfigFields.get(configField.name());
if (!configField.equals(visitedConfigField)) {
throw new CodegenException("Contradicting `ConfigField` defintions for `"
+ configField.name()
+ "`; existing: "
+ visitedConfigField
+ ", conflict: "
+ configField);
}
} else {
visitedConfigFields.put(configField.name(), configField);
if (configField.type().equals(ConfigField.Type.MAIN)) {
w.write("$S: $L,", scheme.getSchemeId().toString(), configField.name());
}
}
}
}
});
});
});
}
Expand Down

0 comments on commit 1d4d78b

Please sign in to comment.