-
Notifications
You must be signed in to change notification settings - Fork 587
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(experimentalIdentityAndAuth): customize
@httpBearerAuth
identi…
…ty 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`
- Loading branch information
Steven Yuan
authored
Sep 8, 2023
1 parent
53ef8f9
commit 4e65c3e
Showing
4 changed files
with
76 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...y/aws/typescript/codegen/auth/http/integration/AwsCustomizeHttpBearerTokenAuthPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.aws.typescript.codegen.auth.http.integration; | ||
|
||
import java.util.List; | ||
import software.amazon.smithy.aws.typescript.codegen.AwsDependency; | ||
import software.amazon.smithy.model.traits.HttpBearerAuthTrait; | ||
import software.amazon.smithy.typescript.codegen.LanguageTarget; | ||
import software.amazon.smithy.typescript.codegen.TypeScriptSettings; | ||
import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthScheme; | ||
import software.amazon.smithy.typescript.codegen.auth.http.SupportedHttpAuthSchemesIndex; | ||
import software.amazon.smithy.typescript.codegen.auth.http.integration.AddHttpBearerAuthPlugin; | ||
import software.amazon.smithy.typescript.codegen.auth.http.integration.HttpAuthTypeScriptIntegration; | ||
import software.amazon.smithy.utils.SmithyInternalApi; | ||
|
||
/** | ||
* Customize @httpBearerAuth for AWS SDKs. | ||
* | ||
* This is the experimental behavior for `experimentalIdentityAndAuth`. | ||
*/ | ||
@SmithyInternalApi | ||
public final class AwsCustomizeHttpBearerTokenAuthPlugin implements HttpAuthTypeScriptIntegration { | ||
|
||
/** | ||
* Integration should only be used if `experimentalIdentityAndAuth` flag is true. | ||
*/ | ||
@Override | ||
public boolean matchesSettings(TypeScriptSettings settings) { | ||
return settings.getExperimentalIdentityAndAuth(); | ||
} | ||
|
||
/** | ||
* Run after default AddHttpBearerAuthPlugin. | ||
*/ | ||
@Override | ||
public List<String> runAfter() { | ||
return List.of(AddHttpBearerAuthPlugin.class.getCanonicalName()); | ||
} | ||
|
||
@Override | ||
public void customizeSupportedHttpAuthSchemes(SupportedHttpAuthSchemesIndex supportedHttpAuthSchemesIndex) { | ||
HttpAuthScheme authScheme = supportedHttpAuthSchemesIndex.getHttpAuthScheme(HttpBearerAuthTrait.ID).toBuilder() | ||
// Current behavior of unconfigured `token` is to throw an error. | ||
// This may need to be customized if a service is released with multiple auth schemes. | ||
.putDefaultIdentityProvider(LanguageTarget.BROWSER, w -> | ||
w.write("async () => { throw new Error(\"`token` is missing\"); }")) | ||
// Use `@aws-sdk/token-providers` as the default identity provider chain for Node.js | ||
.putDefaultIdentityProvider(LanguageTarget.NODE, w -> { | ||
w.addDependency(AwsDependency.TOKEN_PROVIDERS); | ||
w.addImport("nodeProvider", null, AwsDependency.TOKEN_PROVIDERS); | ||
w.write("nodeProvider"); | ||
}) | ||
.build(); | ||
supportedHttpAuthSchemesIndex.putHttpAuthScheme(authScheme.getSchemeId(), authScheme); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters