Skip to content

Commit cd03ec5

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

File tree

8 files changed

+115
-92
lines changed

8 files changed

+115
-92
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,14 +374,15 @@ object RuntimeTypes {
374374
val BearerTokenAuthScheme = symbol("BearerTokenAuthScheme")
375375
val BearerTokenProviderConfig = symbol("BearerTokenProviderConfig")
376376
val BearerTokenProvider = symbol("BearerTokenProvider")
377+
378+
val reprioritizeAuthOptions = symbol("reprioritizeAuthOptions")
377379
}
378380

379381
object HttpAuthAws : RuntimeTypePackage(KotlinDependency.HTTP_AUTH_AWS) {
380382
val AwsHttpSigner = symbol("AwsHttpSigner")
381383
val SigV4AuthScheme = symbol("SigV4AuthScheme")
382384
val SigV4AsymmetricAuthScheme = symbol("SigV4AsymmetricAuthScheme")
383385
val mergeAuthOptions = symbol("mergeAuthOptions")
384-
val reprioritizeAuthOptions = symbol("reprioritizeAuthOptions")
385386
val sigV4 = symbol("sigV4")
386387
val sigV4A = symbol("sigV4A")
387388
}

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

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

134-
write("return authSchemePreference?.let { #T(it, authOptions) } ?: authOptions", RuntimeTypes.Auth.HttpAuthAws.reprioritizeAuthOptions)
134+
write("return authSchemePreference?.let { #T(it, authOptions) } ?: authOptions", RuntimeTypes.Auth.HttpAuth.reprioritizeAuthOptions)
135135
}
136136

137137
// render any helper methods

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ 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;
4039
}
4140

4241
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: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,3 @@ public fun mergeAuthOptions(modeled: List<AuthOption>, endpointOptions: List<Aut
3838

3939
return merged
4040
}
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: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -55,75 +55,4 @@ class EndpointAuthTest {
5555
assertEquals(expectedOption.attributes, actualOption.attributes)
5656
}
5757
}
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-
}
12958
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,7 @@ 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/ReprioritizeAuthOptionsKt {
63+
public static final fun reprioritizeAuthOptions (Ljava/util/List;Ljava/util/List;)Ljava/util/List;
64+
}
65+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package aws.smithy.kotlin.runtime.http.auth
7+
8+
import aws.smithy.kotlin.runtime.InternalApi
9+
import aws.smithy.kotlin.runtime.auth.AuthOption
10+
import aws.smithy.kotlin.runtime.auth.AuthSchemeId
11+
12+
/**
13+
* Re-prioritize a resolved list of auth options based on a user's preference list
14+
*/
15+
@InternalApi
16+
public fun reprioritizeAuthOptions(authSchemePreference: List<AuthSchemeId>, authOptions: List<AuthOption>): List<AuthOption> {
17+
// add preferred candidates first
18+
val preferredAuthOptions = authSchemePreference.mapNotNull { preferredSchemeId ->
19+
val preferredSchemeName = preferredSchemeId.id.substringAfter('#')
20+
authOptions.singleOrNull {
21+
it.schemeId.id.substringAfter('#') == preferredSchemeName
22+
}
23+
}
24+
25+
val nonPreferredAuthOptions = authOptions.filterNot { it in preferredAuthOptions }
26+
27+
return preferredAuthOptions + nonPreferredAuthOptions
28+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import aws.smithy.kotlin.runtime.auth.AuthOption
2+
import aws.smithy.kotlin.runtime.auth.AuthSchemeId
3+
import aws.smithy.kotlin.runtime.http.auth.reprioritizeAuthOptions
4+
import kotlinx.coroutines.test.runTest
5+
import kotlin.test.Test
6+
import kotlin.test.assertEquals
7+
8+
class ReprioritizeAuthOptionsTest {
9+
@Test
10+
fun testReprioritizeAuthOptions() = runTest {
11+
val preference = listOf(AuthSchemeId.AwsSigV4Asymmetric)
12+
val authOptions = listOf(
13+
AuthOption(AuthSchemeId.AwsSigV4),
14+
AuthOption(AuthSchemeId.AwsSigV4Asymmetric),
15+
)
16+
17+
val expected = listOf(
18+
AuthOption(AuthSchemeId.AwsSigV4Asymmetric),
19+
AuthOption(AuthSchemeId.AwsSigV4),
20+
)
21+
22+
assertEquals(expected, reprioritizeAuthOptions(preference, authOptions))
23+
}
24+
25+
@Test
26+
fun testReprioritizeAuthOptionsNoOp() = runTest {
27+
val preference = listOf(AuthSchemeId.HttpBearer)
28+
val authOptions = listOf(
29+
AuthOption(AuthSchemeId.AwsSigV4),
30+
AuthOption(AuthSchemeId.AwsSigV4Asymmetric),
31+
)
32+
33+
val expected = listOf(
34+
AuthOption(AuthSchemeId.AwsSigV4),
35+
AuthOption(AuthSchemeId.AwsSigV4Asymmetric),
36+
)
37+
38+
assertEquals(expected, reprioritizeAuthOptions(preference, authOptions))
39+
}
40+
41+
@Test
42+
fun testReprioritizeAuthOptionsAllPreferred() = runTest {
43+
val preference = listOf(AuthSchemeId.HttpDigest, AuthSchemeId.HttpBasic)
44+
val authOptions = listOf(
45+
AuthOption(AuthSchemeId.HttpBasic),
46+
AuthOption(AuthSchemeId.HttpDigest),
47+
)
48+
49+
val expected = listOf(
50+
AuthOption(AuthSchemeId.HttpDigest),
51+
AuthOption(AuthSchemeId.HttpBasic),
52+
)
53+
54+
assertEquals(expected, reprioritizeAuthOptions(preference, authOptions))
55+
}
56+
57+
@Test
58+
fun testReprioritizeAuthOptionsNoPreference() = runTest {
59+
val preference = emptyList<AuthSchemeId>()
60+
val authOptions = listOf(
61+
AuthOption(AuthSchemeId.AwsSigV4),
62+
AuthOption(AuthSchemeId.HttpBearer),
63+
)
64+
65+
val expected = authOptions
66+
67+
assertEquals(expected, reprioritizeAuthOptions(preference, authOptions))
68+
}
69+
70+
@Test
71+
fun testReprioritizeAuthOptionsEmptyAuthOptions() = runTest {
72+
val preference = listOf(AuthSchemeId.AwsSigV4)
73+
val authOptions = emptyList<AuthOption>()
74+
75+
val expected = emptyList<AuthOption>()
76+
77+
assertEquals(expected, reprioritizeAuthOptions(preference, authOptions))
78+
}
79+
80+
}

0 commit comments

Comments
 (0)