|
| 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 | + val endpoint = Url { host = Host.parse("foo.bar.us-east-1.baz") } |
| 42 | + val token = generator.generateAuthToken(endpoint, "us-east-1", 333.seconds) |
| 43 | + |
| 44 | + assertContains(token, "foo.bar.us-east-1.baz") |
| 45 | + assertContains(token, "X-Amz-Credential=signature") // test custom signer was invoked |
| 46 | + assertContains(token, "X-Amz-Expires=333") // expiration |
| 47 | + assertContains(token, "X-Amz-SigningDate=0") // clock |
| 48 | + |
| 49 | + assertTrue(credentialsProvider.credentialsResolved) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +private val TEST_SIGNER = object : AwsSigner { |
| 54 | + override suspend fun sign( |
| 55 | + request: HttpRequest, |
| 56 | + config: AwsSigningConfig, |
| 57 | + ): AwsSigningResult<HttpRequest> { |
| 58 | + val builder = request.toBuilder() |
| 59 | + builder.url.parameters.decodedParameters.apply { |
| 60 | + put("X-Amz-Credential", "signature") |
| 61 | + put("X-Amz-Expires", (config.expiresAfter?.toLong(DurationUnit.SECONDS) ?: 900).toString()) |
| 62 | + put("X-Amz-SigningDate", config.signingDate.epochSeconds.toString()) |
| 63 | + } |
| 64 | + |
| 65 | + return AwsSigningResult<HttpRequest>(builder.build(), "signature".encodeToByteArray()) |
| 66 | + } |
| 67 | + |
| 68 | + override suspend fun signChunk(chunkBody: ByteArray, prevSignature: ByteArray, config: AwsSigningConfig): AwsSigningResult<Unit> = throw IllegalStateException("signChunk unexpectedly invoked") |
| 69 | + |
| 70 | + override suspend fun signChunkTrailer(trailingHeaders: Headers, prevSignature: ByteArray, config: AwsSigningConfig): AwsSigningResult<Unit> = throw IllegalStateException("signChunkTrailer unexpectedly invoked") |
| 71 | +} |
0 commit comments