Skip to content

Commit

Permalink
refactor!: rename HttpAuthScheme -> AuthScheme and update auth ids
Browse files Browse the repository at this point in the history
  • Loading branch information
aajtodd committed Aug 16, 2023
1 parent 520d176 commit fb0ad17
Show file tree
Hide file tree
Showing 17 changed files with 45 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ object RuntimeTypes {
object Identity : RuntimeTypePackage(KotlinDependency.IDENTITY_API) {
val AuthSchemeId = symbol("AuthSchemeId", "auth")
val AuthSchemeProvider = symbol("AuthSchemeProvider", "auth")
val AuthSchemeOption = symbol("AuthSchemeOption", "auth")
val AuthOption = symbol("AuthOption", "auth")

val IdentityProvider = symbol("IdentityProvider", "identity")
val IdentityProviderConfig = symbol("IdentityProviderConfig", "identity")
Expand All @@ -303,7 +303,7 @@ object RuntimeTypes {
val AnonymousIdentity = symbol("AnonymousIdentity")
val AnonymousIdentityProvider = symbol("AnonymousIdentityProvider")
val HttpAuthConfig = symbol("HttpAuthConfig")
val HttpAuthScheme = symbol("HttpAuthScheme")
val AuthScheme = symbol("AuthScheme")

val BearerTokenAuthScheme = symbol("BearerTokenAuthScheme")
val BearerTokenProviderConfig = symbol("BearerTokenProviderConfig")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import software.amazon.smithy.kotlin.codegen.model.buildSymbol
import software.amazon.smithy.kotlin.codegen.rendering.protocol.ProtocolGenerator
import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.model.shapes.ShapeId
import software.amazon.smithy.model.traits.OptionalAuthTrait

/**
* Register support for the `smithy.api#optionalAuth` auth scheme.
Expand All @@ -25,7 +24,7 @@ class AnonymousAuthSchemeIntegration : KotlinIntegration {
}

class AnonymousAuthSchemeHandler : AuthSchemeHandler {
override val authSchemeId: ShapeId = OptionalAuthTrait.ID
override val authSchemeId: ShapeId = ShapeId.from("smithy.api#noAuth")
override val authSchemeIdSymbol: Symbol = buildSymbol {
name = "AuthSchemeId.Anonymous"
val ref = RuntimeTypes.Auth.Identity.AuthSchemeId
Expand All @@ -43,7 +42,7 @@ class AnonymousAuthSchemeHandler : AuthSchemeHandler {
op: OperationShape?,
writer: KotlinWriter,
) {
writer.write("#T(#T.Anonymous)", RuntimeTypes.Auth.Identity.AuthSchemeOption, RuntimeTypes.Auth.Identity.AuthSchemeId)
writer.write("#T(#T.Anonymous)", RuntimeTypes.Auth.Identity.AuthOption, RuntimeTypes.Auth.Identity.AuthSchemeId)
}

override fun instantiateAuthSchemeExpr(ctx: ProtocolGenerator.GenerationContext, writer: KotlinWriter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class AuthSchemeProviderAdapterGenerator {
"override suspend fun resolve(request: #T): List<#T> {",
"}",
RuntimeTypes.HttpClient.Operation.SdkHttpRequest,
RuntimeTypes.Auth.Identity.AuthSchemeOption,
RuntimeTypes.Auth.Identity.AuthOption,
) {
withBlock("val params = #T {", "}", AuthSchemeParametersGenerator.getSymbol(ctx.settings)) {
addImport(RuntimeTypes.Core.Utils.get)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ open class AuthSchemeProviderGenerator {
"private val operationOverrides = mapOf<#T, List<#T>>(",
")",
KotlinTypes.String,
RuntimeTypes.Auth.Identity.AuthSchemeOption,
RuntimeTypes.Auth.Identity.AuthOption,
) {
operationsWithOverrides.forEach { op ->
val authHandlersForOperation = authIndex.effectiveAuthHandlersForOperation(ctx, op)
Expand All @@ -82,7 +82,7 @@ open class AuthSchemeProviderGenerator {
withBlock(
"private val serviceDefaults = listOf<#T>(",
")",
RuntimeTypes.Auth.Identity.AuthSchemeOption,
RuntimeTypes.Auth.Identity.AuthOption,
) {
val defaultHandlers = authIndex.effectiveAuthHandlersForService(ctx)

Expand All @@ -98,7 +98,7 @@ open class AuthSchemeProviderGenerator {
"override suspend fun resolveAuthScheme(params: #T): List<#T> {",
"}",
paramsSymbol,
RuntimeTypes.Auth.Identity.AuthSchemeOption,
RuntimeTypes.Auth.Identity.AuthOption,
) {
withBlock("return operationOverrides.getOrElse(params.operationName) {", "}") {
write("serviceDefaults")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class BearerTokenAuthSchemeHandler : AuthSchemeHandler {
op: OperationShape?,
writer: KotlinWriter,
) {
writer.write("#T(#T.HttpBearer)", RuntimeTypes.Auth.Identity.AuthSchemeOption, RuntimeTypes.Auth.Identity.AuthSchemeId)
writer.write("#T(#T.HttpBearer)", RuntimeTypes.Auth.Identity.AuthOption, RuntimeTypes.Auth.Identity.AuthSchemeId)
}

override fun instantiateAuthSchemeExpr(ctx: ProtocolGenerator.GenerationContext, writer: KotlinWriter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ abstract class HttpProtocolClientGenerator(
writer.withBlock(
"private val configuredAuthSchemes = with(config.authSchemes.associateBy(#T::schemeId).toMutableMap()){",
"}",
RuntimeTypes.Auth.HttpAuth.HttpAuthScheme,
RuntimeTypes.Auth.HttpAuth.AuthScheme,
) {
val authIndex = AuthIndex()
val allAuthHandlers = authIndex.authHandlersForService(ctx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ object RuntimeConfigProperty {

val HttpAuthSchemes = ConfigProperty {
name = "authSchemes"
symbol = KotlinTypes.Collections.list(RuntimeTypes.Auth.HttpAuth.HttpAuthScheme, default = "emptyList()")
symbol = KotlinTypes.Collections.list(RuntimeTypes.Auth.HttpAuth.AuthScheme, default = "emptyList()")
baseClass = RuntimeTypes.Auth.HttpAuth.HttpAuthConfig
useNestedBuilderBaseClass()
documentation = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import aws.smithy.kotlin.runtime.identity.IdentityProviderConfig
/**
* A configured authentication scheme for HTTP protocol
*/
public interface HttpAuthScheme {
public interface AuthScheme {
/**
* The unique authentication scheme ID
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ package aws.smithy.kotlin.runtime.http.auth
*/
public interface HttpAuthConfig {
/**
* New or overridden [HttpAuthScheme]'s configured for this client. By default, the set
* New or overridden [AuthScheme]'s configured for this client. By default, the set
* of auth schemes configured comes from the service model. An auth scheme configured explicitly takes
* precedence over the defaults and can be used to customize identity resolution and signing for specific
* authentication schemes.
*/
public val authSchemes: List<HttpAuthScheme>
public val authSchemes: List<AuthScheme>

public interface Builder {
/**
* Register new or override default [HttpAuthScheme]'s configured for this client. By default, the set
* Register new or override default [AuthScheme]'s configured for this client. By default, the set
* of auth schemes configured comes from the service model. An auth scheme configured explicitly takes
* precedence over the defaults and can be used to customize identity resolution and signing for specific
* authentication schemes.
*/
public var authSchemes: List<HttpAuthScheme>
public var authSchemes: List<AuthScheme>
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package aws.smithy.kotlin.runtime.http.auth

import aws.smithy.kotlin.runtime.InternalApi
import aws.smithy.kotlin.runtime.auth.AuthOption
import aws.smithy.kotlin.runtime.auth.AuthSchemeId
import aws.smithy.kotlin.runtime.auth.AuthSchemeOption
import aws.smithy.kotlin.runtime.auth.awssigning.AwsSigner
Expand All @@ -20,7 +21,7 @@ import aws.smithy.kotlin.runtime.util.emptyAttributes
@InternalApi
public class SigV4AuthScheme(
config: AwsHttpSigner.Config,
) : HttpAuthScheme {
) : AuthScheme {
public constructor(awsSigner: AwsSigner, serviceName: String) : this(
AwsHttpSigner.Config().apply {
signer = awsSigner
Expand All @@ -33,12 +34,12 @@ public class SigV4AuthScheme(
}

/**
* Create a new [AuthSchemeOption] for the [SigV4AuthScheme]
* Create a new [AuthOption] for the [SigV4AuthScheme]
* @param unsignedPayload set the signing attribute to indicate the signer should use unsigned payload.
* @return auth scheme option representing the [SigV4AuthScheme]
*/
@InternalApi
public fun sigv4(unsignedPayload: Boolean = false): AuthSchemeOption {
public fun sigv4(unsignedPayload: Boolean = false): AuthOption {
val attrs = if (unsignedPayload) {
attributesOf {
AwsSigningAttributes.HashSpecification to HashSpecification.UnsignedPayload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public object AnonymousIdentityProvider : IdentityProvider {
/**
* A no-op auth scheme
*/
public object AnonymousAuthScheme : HttpAuthScheme {
public object AnonymousAuthScheme : AuthScheme {
override val schemeId: AuthSchemeId = AuthSchemeId.Anonymous
override val signer: HttpSigner = AnonymousHttpSigner
override fun identityProvider(identityProviderConfig: IdentityProviderConfig): IdentityProvider = AnonymousIdentityProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import aws.smithy.kotlin.runtime.auth.AuthSchemeId
/**
* HTTP auth scheme for HTTP Bearer authentication as defined in [RFC 6750](https://tools.ietf.org/html/rfc6750.html)
*/
public class BearerTokenAuthScheme : HttpAuthScheme {
public class BearerTokenAuthScheme : AuthScheme {
override val schemeId: AuthSchemeId = AuthSchemeId.HttpBearer
override val signer: HttpSigner = BearerTokenSigner()
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,22 @@ import aws.smithy.kotlin.runtime.util.emptyAttributes
* A tuple of [AuthSchemeId] and typed properties. AuthSchemeOption represents a candidate
* authentication scheme.
*/
public data class AuthSchemeOption(
public interface AuthOption {
/**
* The ID of the authentication scheme
*/
public val schemeId: AuthSchemeId,
public val schemeId: AuthSchemeId

/**
* Identity or signer attributes to use with this resolved authentication scheme
*/
public val attributes: Attributes = emptyAttributes(),
)
public val attributes: Attributes
}

public fun AuthOption(id: AuthSchemeId, attributes: Attributes = emptyAttributes()): AuthOption =
AuthOptionImpl(id, attributes)

private data class AuthOptionImpl(
override val schemeId: AuthSchemeId,
override val attributes: Attributes,
) : AuthOption
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface AuthSchemeProvider<in T> {
/**
* Resolve the candidate set of authentication schemes for an operation
* @param params The input context for the resolver function
* @return a list of candidate [AuthSchemeOption] that can be used for an operation
* @return a list of candidate [AuthOption] that can be used for an operation
*/
public suspend fun resolveAuthScheme(params: T): List<AuthSchemeOption>
public suspend fun resolveAuthScheme(params: T): List<AuthOption>
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
package aws.smithy.kotlin.runtime.http.operation

import aws.smithy.kotlin.runtime.InternalApi
import aws.smithy.kotlin.runtime.auth.AuthOption
import aws.smithy.kotlin.runtime.auth.AuthSchemeId
import aws.smithy.kotlin.runtime.auth.AuthSchemeOption
import aws.smithy.kotlin.runtime.auth.AuthSchemeProvider
import aws.smithy.kotlin.runtime.http.auth.AnonymousAuthScheme
import aws.smithy.kotlin.runtime.http.auth.AnonymousIdentityProvider
import aws.smithy.kotlin.runtime.http.auth.HttpAuthScheme
import aws.smithy.kotlin.runtime.http.auth.AuthScheme
import aws.smithy.kotlin.runtime.identity.IdentityProviderConfig
import aws.smithy.kotlin.runtime.identity.asIdentityProviderConfig

Expand All @@ -29,7 +30,7 @@ private val AnonymousAuthConfig = OperationAuthConfig.from(
@InternalApi
public data class OperationAuthConfig(
val authSchemeResolver: AuthSchemeResolver,
val configuredAuthSchemes: Map<AuthSchemeId, HttpAuthScheme>,
val configuredAuthSchemes: Map<AuthSchemeId, AuthScheme>,
val identityProviderConfig: IdentityProviderConfig,
) {
@InternalApi
Expand All @@ -42,10 +43,10 @@ public data class OperationAuthConfig(
*/
public fun from(
identityProviderConfig: IdentityProviderConfig,
vararg authSchemes: HttpAuthScheme,
vararg authSchemes: AuthScheme,
): OperationAuthConfig {
val resolver = AuthSchemeResolver { authSchemes.map { AuthSchemeOption(it.schemeId) } }
return OperationAuthConfig(resolver, authSchemes.associateBy(HttpAuthScheme::schemeId), identityProviderConfig)
return OperationAuthConfig(resolver, authSchemes.associateBy(AuthScheme::schemeId), identityProviderConfig)
}
}
}
Expand All @@ -60,5 +61,5 @@ public fun interface AuthSchemeResolver {
* Resolve the candidate authentication schemes for an operation
* @return a prioritized list of candidate auth schemes to use for the current operation
*/
public suspend fun resolve(request: SdkHttpRequest): List<AuthSchemeOption>
public suspend fun resolve(request: SdkHttpRequest): List<AuthOption>
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class HttpAuthHandlerTest {
interceptorExec.readBeforeExecution(Unit)

val idpConfig = AnonymousIdentityProvider.asIdentityProviderConfig()
val scheme = object : HttpAuthScheme {
val scheme = object : AuthScheme {
override val schemeId: AuthSchemeId = AuthSchemeId.Anonymous
override fun identityProvider(identityProviderConfig: IdentityProviderConfig): IdentityProvider = object : IdentityProvider {
override suspend fun resolve(attributes: Attributes): Identity {
Expand All @@ -69,7 +69,7 @@ class HttpAuthHandlerTest {
listOf(AuthSchemeOption(AuthSchemeId.Anonymous, attrs))
}

val schemes = listOf(scheme).associateBy(HttpAuthScheme::schemeId)
val schemes = listOf(scheme).associateBy(AuthScheme::schemeId)
val authConfig = OperationAuthConfig(resolver, schemes, idpConfig)
val op = AuthHandler<Unit, Unit>(inner, interceptorExec, authConfig)
val request = SdkHttpRequest(ctx, HttpRequestBuilder())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class SdkOperationExecutionTest {
assertFalse(request.headers.contains("receive-header"))
}
}
val authScheme = object : HttpAuthScheme {
val authScheme = object : AuthScheme {
override val schemeId: AuthSchemeId = AuthSchemeId.Anonymous
override val signer: HttpSigner = httpSigner
}
Expand Down

0 comments on commit fb0ad17

Please sign in to comment.