Skip to content

Commit f6de03d

Browse files
authored
feat: add support for bedrock api key auth (#1317)
* add runtime type * lint * pr feedback, move env bearer token provider to smithy kotlin * api * add tests for env bearer token provider * api * remove unnecessary test
1 parent 444ff8a commit f6de03d

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,8 @@ object RuntimeTypes {
374374
val BearerTokenAuthScheme = symbol("BearerTokenAuthScheme")
375375
val BearerTokenProviderConfig = symbol("BearerTokenProviderConfig")
376376
val BearerTokenProvider = symbol("BearerTokenProvider")
377+
val BearerToken = symbol("BearerToken")
378+
val EnvironmentBearerTokenProvider = symbol("EnvironmentBearerTokenProvider")
377379

378380
val reprioritizeAuthOptions = symbol("reprioritizeAuthOptions")
379381
}

runtime/auth/http-auth/api/http-auth.api

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ public final class aws/smithy/kotlin/runtime/http/auth/BearerTokenSigner : aws/s
5959
public abstract interface class aws/smithy/kotlin/runtime/http/auth/CloseableBearerTokenProvider : aws/smithy/kotlin/runtime/http/auth/BearerTokenProvider, java/io/Closeable {
6060
}
6161

62+
public final class aws/smithy/kotlin/runtime/http/auth/EnvironmentBearerTokenProvider : aws/smithy/kotlin/runtime/http/auth/BearerTokenProvider {
63+
public fun <init> (Ljava/lang/String;Laws/smithy/kotlin/runtime/util/PlatformProvider;)V
64+
public synthetic fun <init> (Ljava/lang/String;Laws/smithy/kotlin/runtime/util/PlatformProvider;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
65+
public fun resolve (Laws/smithy/kotlin/runtime/collections/Attributes;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
66+
}
67+
6268
public final class aws/smithy/kotlin/runtime/http/auth/ReprioritizeAuthOptionsKt {
6369
public static final fun reprioritizeAuthOptions (Ljava/util/List;Ljava/util/List;)Ljava/util/List;
6470
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package aws.smithy.kotlin.runtime.http.auth
6+
7+
import aws.smithy.kotlin.runtime.collections.Attributes
8+
import aws.smithy.kotlin.runtime.collections.emptyAttributes
9+
import aws.smithy.kotlin.runtime.time.Instant
10+
import aws.smithy.kotlin.runtime.util.PlatformProvider
11+
12+
/**
13+
* A [BearerTokenProvider] that extracts the bearer token from the target environment variable.
14+
*/
15+
public class EnvironmentBearerTokenProvider(
16+
private val key: String,
17+
private val platform: PlatformProvider = PlatformProvider.System,
18+
) : BearerTokenProvider {
19+
override suspend fun resolve(attributes: Attributes): BearerToken {
20+
val bearerToken = platform.getenv(key)
21+
?: error("$key environment variable is not set")
22+
23+
return object : BearerToken {
24+
override val token: String = bearerToken
25+
override val attributes: Attributes = emptyAttributes()
26+
override val expiration: Instant? = null
27+
}
28+
}
29+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package aws.smithy.kotlin.runtime.http.auth
2+
3+
import aws.smithy.kotlin.runtime.collections.emptyAttributes
4+
import aws.smithy.kotlin.runtime.util.TestPlatformProvider
5+
import kotlinx.coroutines.test.runTest
6+
import kotlin.test.Test
7+
import kotlin.test.assertEquals
8+
import kotlin.test.assertFailsWith
9+
10+
class EnvironmentBearerTokenProviderTest {
11+
@Test
12+
fun testResolveWithValidToken() = runTest {
13+
val provider = EnvironmentBearerTokenProvider(
14+
"TEST_TOKEN",
15+
TestPlatformProvider(mutableMapOf("TEST_TOKEN" to "test-bearer-token")),
16+
)
17+
18+
val token = provider.resolve()
19+
20+
assertEquals("test-bearer-token", token.token)
21+
}
22+
23+
@Test
24+
fun testResolveWithMissingToken() = runTest {
25+
val provider = EnvironmentBearerTokenProvider(
26+
"MISSING_TEST_TOKEN",
27+
TestPlatformProvider(mutableMapOf()),
28+
)
29+
30+
val exception = assertFailsWith<IllegalStateException> {
31+
provider.resolve(emptyAttributes())
32+
}
33+
assertEquals("MISSING_TEST_TOKEN environment variable is not set", exception.message)
34+
}
35+
}

0 commit comments

Comments
 (0)