-
Notifications
You must be signed in to change notification settings - Fork 26
/
Presigner.kt
66 lines (58 loc) · 2.71 KB
/
Presigner.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* 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.InternalApi
import aws.smithy.kotlin.runtime.auth.AuthSchemeId
import aws.smithy.kotlin.runtime.auth.awscredentials.CredentialsProvider
import aws.smithy.kotlin.runtime.client.endpoints.authOptions
import aws.smithy.kotlin.runtime.collections.emptyAttributes
import aws.smithy.kotlin.runtime.http.*
import aws.smithy.kotlin.runtime.http.operation.EndpointResolver
import aws.smithy.kotlin.runtime.http.operation.ResolveEndpointRequest
import aws.smithy.kotlin.runtime.http.operation.setResolvedEndpoint
import aws.smithy.kotlin.runtime.http.request.HttpRequest
import aws.smithy.kotlin.runtime.http.request.HttpRequestBuilder
import aws.smithy.kotlin.runtime.net.url.Url
import aws.smithy.kotlin.runtime.operation.ExecutionContext
@InternalApi
public suspend fun presignRequest(
unsignedRequestBuilder: HttpRequestBuilder,
ctx: ExecutionContext,
credentialsProvider: CredentialsProvider,
endpointResolver: EndpointResolver,
signer: AwsSigner,
signingConfig: AwsSigningConfig.Builder.() -> Unit,
): HttpRequest {
unsignedRequestBuilder.body = HttpBody.Empty
val credentials = credentialsProvider.resolve(ctx.attributes)
val eprRequest = ResolveEndpointRequest(ctx, unsignedRequestBuilder.build(), credentials)
val endpoint = endpointResolver.resolve(eprRequest)
setResolvedEndpoint(unsignedRequestBuilder, ctx, endpoint)
val signingContext = endpoint.authOptions.firstOrNull { it.schemeId == AuthSchemeId.AwsSigV4 }?.attributes ?: emptyAttributes()
val config = AwsSigningConfig {
region = signingContext.getOrNull(AwsSigningAttributes.SigningRegion)
service = signingContext.getOrNull(AwsSigningAttributes.SigningService)
this.credentials = credentials
signedBodyHeader = AwsSignedBodyHeader.X_AMZ_CONTENT_SHA256
signatureType = AwsSignatureType.HTTP_REQUEST_VIA_QUERY_PARAMS
hashSpecification = HashSpecification.UnsignedPayload // By default (should override for awsQuery)
// Apply service/caller-specific overrides
signingConfig()
}
val result = signer.sign(unsignedRequestBuilder.build(), config)
val signedRequest = result.output
return HttpRequest(
method = signedRequest.method,
url = Url {
scheme = endpoint.uri.scheme
host = endpoint.uri.host
port = endpoint.uri.port
path.copyFrom(signedRequest.url.path)
parameters.copyFrom(signedRequest.url.parameters)
},
headers = signedRequest.headers,
body = HttpBody.Empty,
)
}