Skip to content

Commit 73eccb3

Browse files
committed
Remove caching logic, add a test
1 parent ba50bdf commit 73eccb3

File tree

3 files changed

+79
-8
lines changed

3 files changed

+79
-8
lines changed

runtime/auth/aws-signing-common/api/aws-signing-common.api

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
public final class aws/smithy/kotlin/runtime/auth/awssigning/AuthTokenGenerator {
2-
public synthetic fun <init> (Ljava/lang/String;Laws/smithy/kotlin/runtime/auth/awscredentials/CredentialsProvider;JLaws/smithy/kotlin/runtime/auth/awssigning/AwsSigner;Laws/smithy/kotlin/runtime/time/Clock;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
3-
public synthetic fun <init> (Ljava/lang/String;Laws/smithy/kotlin/runtime/auth/awscredentials/CredentialsProvider;JLaws/smithy/kotlin/runtime/auth/awssigning/AwsSigner;Laws/smithy/kotlin/runtime/time/Clock;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
2+
public fun <init> (Ljava/lang/String;Laws/smithy/kotlin/runtime/auth/awscredentials/CredentialsProvider;Laws/smithy/kotlin/runtime/auth/awssigning/AwsSigner;Laws/smithy/kotlin/runtime/time/Clock;)V
3+
public synthetic fun <init> (Ljava/lang/String;Laws/smithy/kotlin/runtime/auth/awscredentials/CredentialsProvider;Laws/smithy/kotlin/runtime/auth/awssigning/AwsSigner;Laws/smithy/kotlin/runtime/time/Clock;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
44
public final fun generateAuthToken-exY8QGI (Laws/smithy/kotlin/runtime/net/url/Url;Ljava/lang/String;JLkotlin/coroutines/Continuation;)Ljava/lang/Object;
55
public final fun getClock ()Laws/smithy/kotlin/runtime/time/Clock;
66
public final fun getCredentialsProvider ()Laws/smithy/kotlin/runtime/auth/awscredentials/CredentialsProvider;
7-
public final fun getCredentialsRefreshBuffer-UwyO8pc ()J
87
public final fun getService ()Ljava/lang/String;
98
public final fun getSigner ()Laws/smithy/kotlin/runtime/auth/awssigning/AwsSigner;
109
}

runtime/auth/aws-signing-common/common/src/aws/smithy/kotlin/runtime/auth/awssigning/AuthTokenGenerator.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,18 @@
44
*/
55
package aws.smithy.kotlin.runtime.auth.awssigning
66

7-
import aws.smithy.kotlin.runtime.auth.awscredentials.Credentials
87
import aws.smithy.kotlin.runtime.auth.awscredentials.CredentialsProvider
98
import aws.smithy.kotlin.runtime.auth.awssigning.AwsSigningConfig.Companion.invoke
109
import aws.smithy.kotlin.runtime.http.HttpMethod
1110
import aws.smithy.kotlin.runtime.http.request.HttpRequest
1211
import aws.smithy.kotlin.runtime.net.url.Url
1312
import aws.smithy.kotlin.runtime.time.Clock
14-
import aws.smithy.kotlin.runtime.util.ExpiringValue
1513
import kotlin.time.Duration
16-
import kotlin.time.Duration.Companion.minutes
17-
import kotlin.time.Duration.Companion.seconds
1814

1915
/**
2016
* Generates an authentication token, which is a SigV4-signed URL with the HTTP scheme removed.
2117
* @param service The name of the service the token is being generated for
2218
* @param credentialsProvider The [CredentialsProvider] which will provide credentials to use when generating the auth token
23-
* @param credentialsRefreshBuffer The amount of time before the resolved [Credentials] expire in which they are considered expired, defaults to 10 seconds.
2419
* @param signer The [AwsSigner] implementation to use when creating the authentication token
2520
* @param clock The [Clock] implementation to use
2621
*/
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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.auth.awssigning
6+
7+
import aws.smithy.kotlin.runtime.auth.awscredentials.Credentials
8+
import aws.smithy.kotlin.runtime.auth.awscredentials.CredentialsProvider
9+
import aws.smithy.kotlin.runtime.collections.Attributes
10+
import aws.smithy.kotlin.runtime.http.Headers
11+
import aws.smithy.kotlin.runtime.http.request.HttpRequest
12+
import aws.smithy.kotlin.runtime.http.request.toBuilder
13+
import aws.smithy.kotlin.runtime.net.Host
14+
import aws.smithy.kotlin.runtime.net.url.Url
15+
import aws.smithy.kotlin.runtime.time.Instant
16+
import aws.smithy.kotlin.runtime.time.ManualClock
17+
import kotlinx.coroutines.test.runTest
18+
import kotlin.test.Test
19+
import kotlin.test.assertContains
20+
import kotlin.test.assertTrue
21+
import kotlin.time.Duration.Companion.seconds
22+
import kotlin.time.DurationUnit
23+
24+
class AuthTokenGeneratorTest {
25+
@Test
26+
fun testGenerateAuthToken() = runTest {
27+
val credentials = Credentials("akid", "secret")
28+
29+
val credentialsProvider = object : CredentialsProvider {
30+
var credentialsResolved = false
31+
override suspend fun resolve(attributes: Attributes): Credentials {
32+
credentialsResolved = true
33+
return credentials
34+
}
35+
}
36+
37+
val clock = ManualClock(Instant.fromEpochSeconds(0))
38+
39+
val generator = AuthTokenGenerator("foo", credentialsProvider, TEST_SIGNER, clock = clock)
40+
41+
42+
val endpoint = Url { host = Host.parse("foo.bar.us-east-1.baz") }
43+
val token = generator.generateAuthToken(endpoint, "us-east-1", 333.seconds)
44+
45+
assertContains(token, "foo.bar.us-east-1.baz")
46+
assertContains(token, "X-Amz-Credential=signature") // test custom signer was invoked
47+
assertContains(token, "X-Amz-Expires=333") // expiration
48+
assertContains(token, "X-Amz-SigningDate=0") // clock
49+
50+
assertTrue(credentialsProvider.credentialsResolved)
51+
}
52+
}
53+
54+
private val TEST_SIGNER = object : AwsSigner {
55+
override suspend fun sign(
56+
request: HttpRequest,
57+
config: AwsSigningConfig,
58+
): AwsSigningResult<HttpRequest> {
59+
val builder = request.toBuilder()
60+
builder.url.parameters.decodedParameters.apply {
61+
put("X-Amz-Credential", "signature")
62+
put("X-Amz-Expires", (config.expiresAfter?.toLong(DurationUnit.SECONDS) ?: 900).toString())
63+
put("X-Amz-SigningDate", config.signingDate.epochSeconds.toString())
64+
}
65+
66+
return AwsSigningResult<HttpRequest>(builder.build(), "signature".encodeToByteArray())
67+
}
68+
69+
override suspend fun signChunk(chunkBody: ByteArray, prevSignature: ByteArray, config: AwsSigningConfig): AwsSigningResult<Unit> {
70+
throw IllegalStateException("signChunk unexpectedly invoked")
71+
}
72+
73+
override suspend fun signChunkTrailer(trailingHeaders: Headers, prevSignature: ByteArray, config: AwsSigningConfig): AwsSigningResult<Unit> {
74+
throw IllegalStateException("signChunkTrailer unexpectedly invoked")
75+
}
76+
77+
}

0 commit comments

Comments
 (0)