Skip to content

Commit 4e65c3e

Browse files
author
Steven Yuan
authored
feat(experimentalIdentityAndAuth): customize @httpBearerAuth identity providers (#5169)
Register `AwsCustomizeHttpBearerTokenAuthPlugin` integration to customize `@httpBearerAuth` to use: - Browser: a function that throws an error saying `token` is missing - Node.js: `nodeProvider` from `@aws-sdk/token-providers`
1 parent 53ef8f9 commit 4e65c3e

File tree

4 files changed

+76
-4
lines changed

4 files changed

+76
-4
lines changed

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddTokenAuthPlugin.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,38 @@
2020
import static software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin.Convention.HAS_MIDDLEWARE;
2121

2222
import java.util.List;
23+
import software.amazon.smithy.typescript.codegen.TypeScriptSettings;
2324
import software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin;
2425
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
2526
import software.amazon.smithy.utils.ListUtils;
2627
import software.amazon.smithy.utils.SmithyInternalApi;
2728

2829
/**
2930
* Configure clients with Token auth configurations and plugin.
31+
*
32+
* This is the existing control behavior for `experimentalIdentityAndAuth`.
3033
*/
3134
@SmithyInternalApi
3235
public final class AddTokenAuthPlugin implements TypeScriptIntegration {
36+
37+
/**
38+
* Integration should only be used if `experimentalIdentityAndAuth` flag is false.
39+
*/
40+
@Override
41+
public boolean matchesSettings(TypeScriptSettings settings) {
42+
return !settings.getExperimentalIdentityAndAuth();
43+
}
44+
3345
@Override
3446
public List<RuntimeClientPlugin> getClientPlugins() {
3547
return ListUtils.of(
3648
RuntimeClientPlugin.builder()
3749
.withConventions(AwsDependency.MIDDLEWARE_TOKEN.dependency, "Token", HAS_CONFIG)
3850
.servicePredicate((m, s) -> isHttpBearerAuthService(s))
39-
.settingsPredicate((m, s, settings) -> !settings.getExperimentalIdentityAndAuth())
4051
.build(),
4152
RuntimeClientPlugin.builder()
4253
.withConventions(AwsDependency.MIDDLEWARE_TOKEN.dependency, "Token", HAS_MIDDLEWARE)
4354
.servicePredicate((m, s) -> isHttpBearerAuthService(s))
44-
.settingsPredicate((m, s, settings) -> !settings.getExperimentalIdentityAndAuth())
4555
.build()
4656
);
4757
}

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsDependency.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ public enum AwsDependency implements PackageContainer, SymbolDependencyContainer
8383
FLEXIBLE_CHECKSUMS_MIDDLEWARE(NORMAL_DEPENDENCY, "@aws-sdk/middleware-flexible-checksums"),
8484

8585
// Conditionally added when auth trait is present
86-
MIDDLEWARE_API_KEY(NORMAL_DEPENDENCY, "@aws-sdk/middleware-api-key");
86+
MIDDLEWARE_API_KEY(NORMAL_DEPENDENCY, "@aws-sdk/middleware-api-key"),
87+
88+
// feat(experimentalIdentityAndAuth): Conditionally added when @httpBearerAuth is used in an AWS service
89+
TOKEN_PROVIDERS(NORMAL_DEPENDENCY, "@aws-sdk/token-providers");
8790

8891
public final String packageName;
8992
public final String version;
@@ -140,4 +143,3 @@ private static String expectVersion(String packageName) {
140143
}
141144
}
142145
}
143-
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.aws.typescript.codegen.auth.http.integration;
7+
8+
import java.util.List;
9+
import software.amazon.smithy.aws.typescript.codegen.AwsDependency;
10+
import software.amazon.smithy.model.traits.HttpBearerAuthTrait;
11+
import software.amazon.smithy.typescript.codegen.LanguageTarget;
12+
import software.amazon.smithy.typescript.codegen.TypeScriptSettings;
13+
import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthScheme;
14+
import software.amazon.smithy.typescript.codegen.auth.http.SupportedHttpAuthSchemesIndex;
15+
import software.amazon.smithy.typescript.codegen.auth.http.integration.AddHttpBearerAuthPlugin;
16+
import software.amazon.smithy.typescript.codegen.auth.http.integration.HttpAuthTypeScriptIntegration;
17+
import software.amazon.smithy.utils.SmithyInternalApi;
18+
19+
/**
20+
* Customize @httpBearerAuth for AWS SDKs.
21+
*
22+
* This is the experimental behavior for `experimentalIdentityAndAuth`.
23+
*/
24+
@SmithyInternalApi
25+
public final class AwsCustomizeHttpBearerTokenAuthPlugin implements HttpAuthTypeScriptIntegration {
26+
27+
/**
28+
* Integration should only be used if `experimentalIdentityAndAuth` flag is true.
29+
*/
30+
@Override
31+
public boolean matchesSettings(TypeScriptSettings settings) {
32+
return settings.getExperimentalIdentityAndAuth();
33+
}
34+
35+
/**
36+
* Run after default AddHttpBearerAuthPlugin.
37+
*/
38+
@Override
39+
public List<String> runAfter() {
40+
return List.of(AddHttpBearerAuthPlugin.class.getCanonicalName());
41+
}
42+
43+
@Override
44+
public void customizeSupportedHttpAuthSchemes(SupportedHttpAuthSchemesIndex supportedHttpAuthSchemesIndex) {
45+
HttpAuthScheme authScheme = supportedHttpAuthSchemesIndex.getHttpAuthScheme(HttpBearerAuthTrait.ID).toBuilder()
46+
// Current behavior of unconfigured `token` is to throw an error.
47+
// This may need to be customized if a service is released with multiple auth schemes.
48+
.putDefaultIdentityProvider(LanguageTarget.BROWSER, w ->
49+
w.write("async () => { throw new Error(\"`token` is missing\"); }"))
50+
// Use `@aws-sdk/token-providers` as the default identity provider chain for Node.js
51+
.putDefaultIdentityProvider(LanguageTarget.NODE, w -> {
52+
w.addDependency(AwsDependency.TOKEN_PROVIDERS);
53+
w.addImport("nodeProvider", null, AwsDependency.TOKEN_PROVIDERS);
54+
w.write("nodeProvider");
55+
})
56+
.build();
57+
supportedHttpAuthSchemesIndex.putHttpAuthScheme(authScheme.getSchemeId(), authScheme);
58+
}
59+
}

codegen/smithy-aws-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
@@ -24,4 +24,5 @@ software.amazon.smithy.aws.typescript.codegen.AddDocumentClientPlugin
2424
software.amazon.smithy.aws.typescript.codegen.AddEndpointDiscoveryPlugin
2525
software.amazon.smithy.aws.typescript.codegen.AddHttpChecksumDependency
2626
software.amazon.smithy.aws.typescript.codegen.AddEventBridgePlugin
27+
software.amazon.smithy.aws.typescript.codegen.auth.http.integration.AwsCustomizeHttpBearerTokenAuthPlugin
2728
software.amazon.smithy.aws.typescript.codegen.auth.http.integration.AwsCustomizeSigv4AuthPlugin

0 commit comments

Comments
 (0)