Skip to content

Commit

Permalink
feat(experimentalIdentityAndAuth): customize default `HttpAuthScheme/…
Browse files Browse the repository at this point in the history
…Parameters` providers (#943)
  • Loading branch information
Steven Yuan authored Sep 19, 2023
1 parent 075c3c9 commit de80356
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.function.Consumer;
import software.amazon.smithy.build.SmithyBuildException;
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;
Expand Down Expand Up @@ -206,17 +207,20 @@ private void generateHttpAuthSchemeConfig(
TypeScriptWriter writer,
LanguageTarget target
) {
SupportedHttpAuthSchemesIndex authIndex = new SupportedHttpAuthSchemesIndex(integrations);

// feat(experimentalIdentityAndAuth): write the default imported HttpAuthSchemeProvider
if (target.equals(LanguageTarget.SHARED)) {
configs.put("httpAuthSchemeProvider", w -> {
String providerName = "default" + service.toShapeId().getName() + "HttpAuthSchemeProvider";
w.addImport(providerName, null, AuthUtils.AUTH_HTTP_PROVIDER_DEPENDENCY);
w.write(providerName);
w.write("$T", authIndex.getDefaultHttpAuthSchemeProvider()
.orElse(Symbol.builder()
.name("default" + service.toShapeId().getName() + "HttpAuthSchemeProvider")
.namespace(AuthUtils.AUTH_HTTP_PROVIDER_DEPENDENCY.getPackageName(), "/")
.build()));
});
}

// feat(experimentalIdentityAndAuth): gather HttpAuthSchemes to generate
SupportedHttpAuthSchemesIndex authIndex = new SupportedHttpAuthSchemesIndex(integrations);
ServiceIndex serviceIndex = ServiceIndex.of(model);
Map<ShapeId, HttpAuthScheme> allEffectiveHttpAuthSchemes =
AuthUtils.getAllEffectiveNoAuthAwareAuthSchemes(service, serviceIndex, authIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Logger;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.typescript.codegen.auth.http.integration.HttpAuthTypeScriptIntegration;
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
Expand All @@ -24,7 +27,11 @@
*/
@SmithyInternalApi
public final class SupportedHttpAuthSchemesIndex {
private static final Logger LOGGER = Logger.getLogger(SupportedHttpAuthSchemesIndex.class.getName());

private final Map<ShapeId, HttpAuthScheme> supportedHttpAuthSchemes = new HashMap<>();
private Optional<Symbol> defaultHttpAuthSchemeProvider = Optional.empty();
private Optional<Symbol> defaultHttpAuthSchemeParametersProvider = Optional.empty();

/**
* Creates an index from registered {@link HttpAuthScheme}s in {@link TypeScriptIntegration}s.
Expand All @@ -41,6 +48,30 @@ public SupportedHttpAuthSchemesIndex(List<TypeScriptIntegration> integrations) {
this.putHttpAuthScheme(authScheme.getSchemeId(), authScheme);
}
httpAuthIntegration.customizeSupportedHttpAuthSchemes(this);
if (httpAuthIntegration.getDefaultHttpAuthSchemeProvider().isPresent()) {
Optional<Symbol> override =
httpAuthIntegration.getDefaultHttpAuthSchemeProvider();
if (defaultHttpAuthSchemeProvider.isPresent()) {
LOGGER.warning("An existing `HttpAuthSchemeProvider` registration `"
+ defaultHttpAuthSchemeProvider.get()
+ "` is being overwritten by `"
+ override.get()
+ "`");
}
defaultHttpAuthSchemeProvider = override;
}
if (httpAuthIntegration.getDefaultHttpAuthSchemeParametersProvider().isPresent()) {
Optional<Symbol> override =
httpAuthIntegration.getDefaultHttpAuthSchemeParametersProvider();
if (defaultHttpAuthSchemeParametersProvider.isPresent()) {
LOGGER.warning("An existing `HttpAuthSchemeParametersProvider` registration `"
+ defaultHttpAuthSchemeParametersProvider.get()
+ "` is being overwritten by `"
+ override.get()
+ "`");
}
defaultHttpAuthSchemeParametersProvider = override;
}
}
}

Expand Down Expand Up @@ -79,4 +110,20 @@ public HttpAuthScheme removeHttpAuthScheme(ShapeId schemeId) {
public Map<ShapeId, HttpAuthScheme> getSupportedHttpAuthSchemes() {
return supportedHttpAuthSchemes;
}

/**
* Gets the default {@code HttpAuthSchemeProvider} symbol.
* @return an optional with the symbol or empty.
*/
public Optional<Symbol> getDefaultHttpAuthSchemeProvider() {
return defaultHttpAuthSchemeProvider;
}

/**
* Gets the default {@code HttpAuthSchemeParametersProvider} symbol.
* @return an optional with the symbol or empty.
*/
public Optional<Symbol> getDefaultHttpAuthSchemeParametersProvider() {
return defaultHttpAuthSchemeParametersProvider;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package software.amazon.smithy.typescript.codegen.auth.http.integration;

import java.util.Optional;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthScheme;
import software.amazon.smithy.typescript.codegen.auth.http.SupportedHttpAuthSchemesIndex;
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
Expand Down Expand Up @@ -34,4 +35,22 @@ default Optional<HttpAuthScheme> getHttpAuthScheme() {
*/
default void customizeSupportedHttpAuthSchemes(SupportedHttpAuthSchemesIndex supportedHttpAuthSchemesIndex) {
}

/**
* feat(experimentalIdentityAndAuth): Register an {@link Symbol} that points to an {@code HttpAuthSchemeProvider}
* implementation.
* @return an empty optional.
*/
default Optional<Symbol> getDefaultHttpAuthSchemeProvider() {
return Optional.empty();
}

/**
* feat(experimentalIdentityAndAuth): Register an {@link Symbol} that points to an
* {@code HttpAuthSchemeParametersProvider} implementation.
* @return an empty optional.
*/
default Optional<Symbol> getDefaultHttpAuthSchemeParametersProvider() {
return Optional.empty();
}
}

0 comments on commit de80356

Please sign in to comment.