Skip to content

Commit 310c653

Browse files
committed
Break out to reprioritizeAuthOptions function to make testing easier
1 parent faa91de commit 310c653

File tree

5 files changed

+94
-17
lines changed

5 files changed

+94
-17
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ object RuntimeTypes {
381381
val SigV4AuthScheme = symbol("SigV4AuthScheme")
382382
val SigV4AsymmetricAuthScheme = symbol("SigV4AsymmetricAuthScheme")
383383
val mergeAuthOptions = symbol("mergeAuthOptions")
384+
val reprioritizeAuthOptions = symbol("reprioritizeAuthOptions")
384385
val sigV4 = symbol("sigV4")
385386
val sigV4A = symbol("sigV4A")
386387
}

codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeProviderGenerator.kt

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -131,23 +131,7 @@ open class AuthSchemeProviderGenerator {
131131
}
132132
write("")
133133

134-
// reprioritize auth options based on user's preference
135-
withInlineBlock("return authSchemePreference?.let {", "} ") {
136-
// add preferred candidates first
137-
withBlock("val preferredAuthOptions = it.mapNotNull { preferredSchemeId ->", "}") {
138-
writeWithNoFormatting("val preferredSchemeName = preferredSchemeId.id.substringAfter('#')")
139-
withBlock("authOptions.singleOrNull {", "}") {
140-
writeWithNoFormatting("it.schemeId.id.substringAfter('#') == preferredSchemeName")
141-
}
142-
}
143-
144-
// add any remaining candidates that weren't in the preference list
145-
write("val nonPreferredAuthOptions = authOptions.filterNot { it in preferredAuthOptions }")
146-
write("")
147-
148-
write("preferredAuthOptions + nonPreferredAuthOptions")
149-
}
150-
write("?: authOptions")
134+
write("return authSchemePreference?.let { #T(it, authOptions) } ?: authOptions", RuntimeTypes.Auth.HttpAuthAws.reprioritizeAuthOptions)
151135
}
152136

153137
// render any helper methods

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public final class aws/smithy/kotlin/runtime/http/auth/AwsHttpSigner$Config {
3636

3737
public final class aws/smithy/kotlin/runtime/http/auth/EndpointAuthKt {
3838
public static final fun mergeAuthOptions (Ljava/util/List;Ljava/util/List;)Ljava/util/List;
39+
public static final fun reprioritizeAuthOptions (Ljava/util/List;Ljava/util/List;)Ljava/util/List;
3940
}
4041

4142
public final class aws/smithy/kotlin/runtime/http/auth/SigV4AsymmetricAuthScheme : aws/smithy/kotlin/runtime/http/auth/AuthScheme {

runtime/auth/http-auth-aws/common/src/aws/smithy/kotlin/runtime/http/auth/EndpointAuth.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package aws.smithy.kotlin.runtime.http.auth
66

77
import aws.smithy.kotlin.runtime.InternalApi
88
import aws.smithy.kotlin.runtime.auth.AuthOption
9+
import aws.smithy.kotlin.runtime.auth.AuthSchemeId
910
import aws.smithy.kotlin.runtime.collections.merge
1011
import aws.smithy.kotlin.runtime.collections.toMutableAttributes
1112

@@ -37,3 +38,21 @@ public fun mergeAuthOptions(modeled: List<AuthOption>, endpointOptions: List<Aut
3738

3839
return merged
3940
}
41+
42+
/**
43+
* Re-prioritize a resolved list of auth options based on a user's preference list
44+
*/
45+
@InternalApi
46+
public fun reprioritizeAuthOptions(authSchemePreference: List<AuthSchemeId>, authOptions: List<AuthOption>): List<AuthOption> {
47+
// add preferred candidates first
48+
val preferredAuthOptions = authSchemePreference.mapNotNull { preferredSchemeId ->
49+
val preferredSchemeName = preferredSchemeId.id.substringAfter('#')
50+
authOptions.singleOrNull {
51+
it.schemeId.id.substringAfter('#') == preferredSchemeName
52+
}
53+
}
54+
55+
val nonPreferredAuthOptions = authOptions.filterNot { it in preferredAuthOptions }
56+
57+
return preferredAuthOptions + nonPreferredAuthOptions
58+
}

runtime/auth/http-auth-aws/common/test/aws/smithy/kotlin/runtime/http/auth/EndpointAuthTest.kt

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package aws.smithy.kotlin.runtime.http.auth
77
import aws.smithy.kotlin.runtime.auth.AuthOption
88
import aws.smithy.kotlin.runtime.auth.AuthSchemeId
99
import aws.smithy.kotlin.runtime.collections.attributesOf
10+
import kotlinx.coroutines.test.runTest
1011
import kotlin.test.Test
1112
import kotlin.test.assertEquals
1213

@@ -54,4 +55,75 @@ class EndpointAuthTest {
5455
assertEquals(expectedOption.attributes, actualOption.attributes)
5556
}
5657
}
58+
59+
@Test
60+
fun testReprioritizeAuthOptions() = runTest {
61+
val preference = listOf(AuthSchemeId.AwsSigV4Asymmetric)
62+
val authOptions = listOf(
63+
AuthOption(AuthSchemeId.AwsSigV4),
64+
AuthOption(AuthSchemeId.AwsSigV4Asymmetric),
65+
)
66+
67+
val expected = listOf(
68+
AuthOption(AuthSchemeId.AwsSigV4Asymmetric),
69+
AuthOption(AuthSchemeId.AwsSigV4),
70+
)
71+
72+
assertEquals(expected, reprioritizeAuthOptions(preference, authOptions))
73+
}
74+
75+
@Test
76+
fun testReprioritizeAuthOptionsNoOp() = runTest {
77+
val preference = listOf(AuthSchemeId.HttpBearer)
78+
val authOptions = listOf(
79+
AuthOption(AuthSchemeId.AwsSigV4),
80+
AuthOption(AuthSchemeId.AwsSigV4Asymmetric),
81+
)
82+
83+
val expected = listOf(
84+
AuthOption(AuthSchemeId.AwsSigV4),
85+
AuthOption(AuthSchemeId.AwsSigV4Asymmetric),
86+
)
87+
88+
assertEquals(expected, reprioritizeAuthOptions(preference, authOptions))
89+
}
90+
91+
@Test
92+
fun testReprioritizeAuthOptionsAllPreferred() = runTest {
93+
val preference = listOf(AuthSchemeId.HttpDigest, AuthSchemeId.HttpBasic)
94+
val authOptions = listOf(
95+
AuthOption(AuthSchemeId.HttpBasic),
96+
AuthOption(AuthSchemeId.HttpDigest),
97+
)
98+
99+
val expected = listOf(
100+
AuthOption(AuthSchemeId.HttpDigest),
101+
AuthOption(AuthSchemeId.HttpBasic),
102+
)
103+
104+
assertEquals(expected, reprioritizeAuthOptions(preference, authOptions))
105+
}
106+
107+
@Test
108+
fun testReprioritizeAuthOptionsNoPreference() = runTest {
109+
val preference = emptyList<AuthSchemeId>()
110+
val authOptions = listOf(
111+
AuthOption(AuthSchemeId.AwsSigV4),
112+
AuthOption(AuthSchemeId.HttpBearer),
113+
)
114+
115+
val expected = authOptions
116+
117+
assertEquals(expected, reprioritizeAuthOptions(preference, authOptions))
118+
}
119+
120+
@Test
121+
fun testReprioritizeAuthOptionsEmptyAuthOptions() = runTest {
122+
val preference = listOf(AuthSchemeId.AwsSigV4)
123+
val authOptions = emptyList<AuthOption>()
124+
125+
val expected = emptyList<AuthOption>()
126+
127+
assertEquals(expected, reprioritizeAuthOptions(preference, authOptions))
128+
}
57129
}

0 commit comments

Comments
 (0)