-
Notifications
You must be signed in to change notification settings - Fork 31
feat: add AuthTokenGenerator
#1212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
179c1c0
6e3c0fd
ba50bdf
73eccb3
4aae969
52dbd28
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package aws.smithy.kotlin.runtime.auth.awssigning | ||
|
|
||
| import aws.smithy.kotlin.runtime.auth.awscredentials.Credentials | ||
| import aws.smithy.kotlin.runtime.auth.awscredentials.CredentialsProvider | ||
| import aws.smithy.kotlin.runtime.auth.awssigning.AwsSigningConfig.Companion.invoke | ||
| import aws.smithy.kotlin.runtime.http.HttpMethod | ||
| import aws.smithy.kotlin.runtime.http.request.HttpRequest | ||
| import aws.smithy.kotlin.runtime.net.url.Url | ||
| import aws.smithy.kotlin.runtime.time.Clock | ||
| import aws.smithy.kotlin.runtime.util.ExpiringValue | ||
| import kotlin.time.Duration | ||
| import kotlin.time.Duration.Companion.minutes | ||
| import kotlin.time.Duration.Companion.seconds | ||
|
|
||
| // The default expiration value to use for [Credentials] when none is provided. | ||
| private val DEFAULT_CREDENTIALS_EXPIRATION = 10.minutes | ||
|
|
||
| /** | ||
| * Generates an authentication token, which is a SigV4-signed URL with the HTTP scheme removed. | ||
| * @param service The name of the service the token is being generated for | ||
| * @param credentialsProvider The [CredentialsProvider] which will provide credentials to use when generating the auth token | ||
| * @param credentialsRefreshBuffer The amount of time before the resolved [Credentials] expire in which they are considered expired, defaults to 10 seconds. | ||
| * @param signer The [AwsSigner] implementation to use when creating the authentication token | ||
| * @param clock The [Clock] implementation to use | ||
| */ | ||
| public class AuthTokenGenerator( | ||
| public val service: String, | ||
| public val credentialsProvider: CredentialsProvider, | ||
| public val credentialsRefreshBuffer: Duration = 10.seconds, | ||
| public val signer: AwsSigner, | ||
| public val clock: Clock = Clock.System, | ||
| ) { | ||
| private lateinit var credentials: ExpiringValue<Credentials> | ||
|
|
||
| private fun Url.trimScheme(): String = toString().removePrefix(scheme.protocolName).removePrefix("://") | ||
|
|
||
| public suspend fun generateAuthToken(endpoint: Url, region: String, expiration: Duration): String { | ||
| if (!::credentials.isInitialized || (credentials.expiresAt - clock.now()).absoluteValue <= credentialsRefreshBuffer) { | ||
| val resolved = credentialsProvider.resolve() | ||
| credentials = ExpiringValue(resolved, resolved.expiration ?: (clock.now() + DEFAULT_CREDENTIALS_EXPIRATION)) | ||
| } | ||
|
||
|
|
||
| val req = HttpRequest(HttpMethod.GET, endpoint) | ||
|
|
||
| val creds = credentials.value | ||
| val serv = service | ||
|
|
||
| val config = AwsSigningConfig { | ||
| credentials = creds | ||
| this.region = region | ||
| service = serv | ||
|
||
| signingDate = clock.now() | ||
| expiresAfter = expiration | ||
| signatureType = AwsSignatureType.HTTP_REQUEST_VIA_QUERY_PARAMS | ||
| } | ||
|
|
||
| return signer.sign(req, config).output.url.trimScheme() | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.