|
| 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 | +} |
0 commit comments