Skip to content

Commit 53b1a21

Browse files
authored
feat: http bearer token auth scheme support (#850)
1 parent fa52523 commit 53b1a21

File tree

33 files changed

+740
-114
lines changed

33 files changed

+740
-114
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": "05b66c6f-0404-4b56-941a-a35fe64de9f9",
3+
"type": "feature",
4+
"description": "Add support for writing a file via PlatformProvider"
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": "5f6f317e-f5ca-4354-af23-e5c9e7d75d03",
3+
"type": "misc",
4+
"description": "Refactor CredentialsProviderChain into generic/re-usable IdentityProviderChain"
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": "967d3aa7-0921-4feb-a690-16e2f2592904",
3+
"type": "feature",
4+
"description": "Add support for bearer token auth schemes"
5+
}

codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/core/RuntimeTypes.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,10 @@ object RuntimeTypes {
291291
val AnonymousIdentityProvider = symbol("AnonymousIdentityProvider")
292292
val HttpAuthConfig = symbol("HttpAuthConfig")
293293
val HttpAuthScheme = symbol("HttpAuthScheme")
294+
295+
val BearerTokenAuthScheme = symbol("BearerTokenAuthScheme")
296+
val BearerTokenProviderConfig = symbol("BearerTokenProviderConfig")
297+
val BearerTokenProvider = symbol("BearerTokenProvider")
294298
}
295299

296300
object HttpAuthAws : RuntimeTypePackage(KotlinDependency.HTTP_AUTH_AWS) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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.kotlin.codegen.rendering.auth
7+
8+
import software.amazon.smithy.codegen.core.Symbol
9+
import software.amazon.smithy.codegen.core.SymbolReference
10+
import software.amazon.smithy.kotlin.codegen.KotlinSettings
11+
import software.amazon.smithy.kotlin.codegen.core.CodegenContext
12+
import software.amazon.smithy.kotlin.codegen.core.KotlinWriter
13+
import software.amazon.smithy.kotlin.codegen.core.RuntimeTypes
14+
import software.amazon.smithy.kotlin.codegen.integration.AuthSchemeHandler
15+
import software.amazon.smithy.kotlin.codegen.integration.KotlinIntegration
16+
import software.amazon.smithy.kotlin.codegen.model.buildSymbol
17+
import software.amazon.smithy.kotlin.codegen.rendering.protocol.ProtocolGenerator
18+
import software.amazon.smithy.kotlin.codegen.rendering.util.ConfigProperty
19+
import software.amazon.smithy.kotlin.codegen.rendering.util.ConfigPropertyType
20+
import software.amazon.smithy.model.Model
21+
import software.amazon.smithy.model.knowledge.ServiceIndex
22+
import software.amazon.smithy.model.shapes.OperationShape
23+
import software.amazon.smithy.model.shapes.ShapeId
24+
import software.amazon.smithy.model.traits.HttpBearerAuthTrait
25+
26+
/**
27+
* Register support for the `smithy.api#HTTPBearerAuth` auth scheme.
28+
*/
29+
class BearerTokenAuthSchemeIntegration : KotlinIntegration {
30+
// Allow integrations to customize the service config props, later integrations take precedence
31+
override val order: Byte = -50
32+
33+
override fun enabledForService(model: Model, settings: KotlinSettings): Boolean =
34+
ServiceIndex.of(model)
35+
.getAuthSchemes(settings.service)
36+
.containsKey(HttpBearerAuthTrait.ID)
37+
override fun authSchemes(ctx: ProtocolGenerator.GenerationContext): List<AuthSchemeHandler> = listOf(BearerTokenAuthSchemeHandler())
38+
39+
override fun additionalServiceConfigProps(ctx: CodegenContext): List<ConfigProperty> {
40+
val bearerTokenProviderProp = ConfigProperty {
41+
name = "bearerTokenProvider"
42+
symbol = RuntimeTypes.Auth.HttpAuth.BearerTokenProvider
43+
baseClass = RuntimeTypes.Auth.HttpAuth.BearerTokenProviderConfig
44+
useNestedBuilderBaseClass()
45+
documentation = """
46+
The token provider to use for authenticating requests when using [${RuntimeTypes.Auth.HttpAuth.BearerTokenAuthScheme.fullName}].
47+
NOTE: The caller is responsible for managing the lifetime of the provider when set. The SDK
48+
client will not close it when the client is closed.
49+
""".trimIndent()
50+
51+
// FIXME - this isn't necessarily required if a service supports multiple authentication traits...
52+
propertyType = ConfigPropertyType.Required()
53+
}
54+
55+
return listOf(bearerTokenProviderProp)
56+
}
57+
}
58+
59+
class BearerTokenAuthSchemeHandler : AuthSchemeHandler {
60+
override val authSchemeId: ShapeId = HttpBearerAuthTrait.ID
61+
62+
override val authSchemeIdSymbol: Symbol = buildSymbol {
63+
name = "AuthSchemeId.HttpBearer"
64+
val ref = RuntimeTypes.Auth.Identity.AuthSchemeId
65+
objectRef = ref
66+
namespace = ref.namespace
67+
reference(ref, SymbolReference.ContextOption.USE)
68+
}
69+
70+
override fun identityProviderAdapterExpression(writer: KotlinWriter) {
71+
writer.write("config.bearerTokenProvider")
72+
}
73+
74+
override fun authSchemeProviderInstantiateAuthOptionExpr(
75+
ctx: ProtocolGenerator.GenerationContext,
76+
op: OperationShape?,
77+
writer: KotlinWriter,
78+
) {
79+
writer.write("#T(#T.HttpBearer)", RuntimeTypes.Auth.Identity.AuthSchemeOption, RuntimeTypes.Auth.Identity.AuthSchemeId)
80+
}
81+
82+
override fun instantiateAuthSchemeExpr(ctx: ProtocolGenerator.GenerationContext, writer: KotlinWriter) {
83+
writer.write("#T()", RuntimeTypes.Auth.HttpAuth.BearerTokenAuthScheme)
84+
}
85+
}

codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/protocol/HttpProtocolClientGenerator.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ abstract class HttpProtocolClientGenerator(
8686
// render auth resolver related properties
8787
writer.write("private val identityProviderConfig = #T(config)", IdentityProviderConfigGenerator.getSymbol(ctx.settings))
8888

89+
// FIXME - we probably need a way for auth handlers to signal that they are configured (e.g. config properties are not null). Right now this assumes
90+
// they are all configured but a service may support multiple auth schemes and a client may not need to configure all of them
8991
writer.withBlock(
9092
"private val configuredAuthSchemes = with(config.authSchemes.associateBy(#T::schemeId).toMutableMap()){",
9193
"}",

codegen/smithy-kotlin-codegen/src/main/resources/META-INF/services/software.amazon.smithy.kotlin.codegen.integration.KotlinIntegration

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ software.amazon.smithy.kotlin.codegen.rendering.PaginatorGenerator
55
software.amazon.smithy.kotlin.codegen.rendering.waiters.ServiceWaitersGenerator
66
software.amazon.smithy.kotlin.codegen.rendering.auth.Sigv4AuthSchemeIntegration
77
software.amazon.smithy.kotlin.codegen.rendering.auth.AnonymousAuthSchemeIntegration
8+
software.amazon.smithy.kotlin.codegen.rendering.auth.BearerTokenAuthSchemeIntegration

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ kotlin.native.ignoreDisabledTargets=true
66
kotlin.mpp.enableCompatibilityMetadataVariant=true
77

88
# SDK
9-
sdkVersion=0.18.1-SNAPSHOT
9+
sdkVersion=0.19.0-SNAPSHOT
1010

1111
# kotlin
1212
kotlinVersion=1.8.10
@@ -44,4 +44,4 @@ kotlinLoggingVersion=3.0.0
4444
slf4jVersion=2.0.6
4545

4646
# crt
47-
crtKotlinVersion=0.6.8
47+
crtKotlinVersion=0.6.8

runtime/auth/aws-credentials/api/aws-credentials.api

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,9 @@ public abstract interface class aws/smithy/kotlin/runtime/auth/awscredentials/Cr
3838
public abstract fun resolve (Laws/smithy/kotlin/runtime/util/Attributes;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
3939
}
4040

41-
public class aws/smithy/kotlin/runtime/auth/awscredentials/CredentialsProviderChain : aws/smithy/kotlin/runtime/auth/awscredentials/CloseableCredentialsProvider {
41+
public final class aws/smithy/kotlin/runtime/auth/awscredentials/CredentialsProviderChain : aws/smithy/kotlin/runtime/identity/IdentityProviderChain, aws/smithy/kotlin/runtime/auth/awscredentials/CredentialsProvider {
4242
public fun <init> ([Laws/smithy/kotlin/runtime/auth/awscredentials/CredentialsProvider;)V
43-
public fun close ()V
44-
protected final fun getProviders ()[Laws/smithy/kotlin/runtime/auth/awscredentials/CredentialsProvider;
4543
public fun resolve (Laws/smithy/kotlin/runtime/util/Attributes;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
46-
public fun toString ()Ljava/lang/String;
4744
}
4845

4946
public abstract interface class aws/smithy/kotlin/runtime/auth/awscredentials/CredentialsProviderConfig {

runtime/auth/aws-credentials/common/src/aws/smithy/kotlin/runtime/auth/awscredentials/Credentials.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import aws.smithy.kotlin.runtime.util.mutableAttributes
1515
*
1616
* For more information see [AWS security credentials](https://docs.aws.amazon.com/general/latest/gr/aws-security-credentials.html#AccessKeys)
1717
*/
18+
// FIXME - should probably be an interface
1819
public data class Credentials(
1920
val accessKeyId: String,
2021
val secretAccessKey: String,

0 commit comments

Comments
 (0)