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