|
| 1 | +// |
| 2 | +// Copyright Amazon.com Inc. or its affiliates. |
| 3 | +// All Rights Reserved. |
| 4 | +// |
| 5 | +// SPDX-License-Identifier: Apache-2.0 |
| 6 | +// |
| 7 | + |
| 8 | +import class AwsCommonRuntimeKit.HTTPRequestBase |
| 9 | +import class AwsCommonRuntimeKit.Signer |
| 10 | +import class SmithyHTTPAPI.HTTPRequest |
| 11 | +import class SmithyHTTPAPI.HTTPRequestBuilder |
| 12 | +import enum AwsCommonRuntimeKit.CommonRunTimeError |
| 13 | +import enum Smithy.ClientError |
| 14 | +import enum SmithyHTTPAuthAPI.AWSSignedBodyHeader |
| 15 | +import enum SmithyHTTPAuthAPI.AWSSignedBodyValue |
| 16 | +import enum SmithyHTTPAuthAPI.AWSSignatureType |
| 17 | +import enum SmithyHTTPAuthAPI.SigningAlgorithm |
| 18 | +import enum SmithyHTTPAuthAPI.SigningPropertyKeys |
| 19 | +import protocol SmithyIdentity.AWSCredentialIdentityResolver |
| 20 | +import protocol SmithyIdentityAPI.Identity |
| 21 | +import protocol SmithyHTTPAuthAPI.Signer |
| 22 | +import struct AwsCommonRuntimeKit.SigningConfig |
| 23 | +import struct Smithy.AttributeKey |
| 24 | +import struct Smithy.Attributes |
| 25 | +import struct Smithy.SwiftLogger |
| 26 | +import struct SmithyIdentity.AWSCredentialIdentity |
| 27 | +import struct SmithyHTTPAuthAPI.SigningFlags |
| 28 | +import struct Foundation.Date |
| 29 | +import struct Foundation.TimeInterval |
| 30 | +import struct Foundation.URL |
| 31 | +import SmithyHTTPClient |
| 32 | + |
| 33 | +public class SigV4Signer: SmithyHTTPAuthAPI.Signer { |
| 34 | + public init() {} |
| 35 | + |
| 36 | + public func signRequest<IdentityT>( |
| 37 | + requestBuilder: SmithyHTTPAPI.HTTPRequestBuilder, |
| 38 | + identity: IdentityT, |
| 39 | + signingProperties: Smithy.Attributes |
| 40 | + ) async throws -> SmithyHTTPAPI.HTTPRequestBuilder where IdentityT: SmithyIdentityAPI.Identity { |
| 41 | + guard let isBidirectionalStreamingEnabled = signingProperties.get( |
| 42 | + key: SigningPropertyKeys.bidirectionalStreaming |
| 43 | + ) else { |
| 44 | + throw Smithy.ClientError.authError( |
| 45 | + "Signing properties passed to the AWSSigV4Signer must contain T/F flag for bidirectional streaming." |
| 46 | + ) |
| 47 | + } |
| 48 | + |
| 49 | + guard let identity = identity as? AWSCredentialIdentity else { |
| 50 | + throw Smithy.ClientError.authError( |
| 51 | + "Identity passed to the AWSSigV4Signer must be of type Credentials." |
| 52 | + ) |
| 53 | + } |
| 54 | + |
| 55 | + var signingConfig = try constructSigningConfig(identity: identity, signingProperties: signingProperties) |
| 56 | + |
| 57 | + let unsignedRequest = requestBuilder.build() |
| 58 | + let crtUnsignedRequest: HTTPRequestBase = isBidirectionalStreamingEnabled ? |
| 59 | + try unsignedRequest.toHttp2Request() : |
| 60 | + try unsignedRequest.toHttpRequest() |
| 61 | + |
| 62 | + let crtSigningConfig = try signingConfig.toCRTType() |
| 63 | + |
| 64 | + let crtSignedRequest = try await Signer.signRequest( |
| 65 | + request: crtUnsignedRequest, |
| 66 | + config: crtSigningConfig |
| 67 | + ) |
| 68 | + |
| 69 | + let sdkSignedRequest = requestBuilder.update(from: crtSignedRequest, originalRequest: unsignedRequest) |
| 70 | + |
| 71 | + // Return signed request |
| 72 | + return sdkSignedRequest |
| 73 | + } |
| 74 | + |
| 75 | + private func constructSigningConfig( |
| 76 | + identity: AWSCredentialIdentity, |
| 77 | + signingProperties: Smithy.Attributes |
| 78 | + ) throws -> AWSSigningConfig { |
| 79 | + guard let unsignedBody = signingProperties.get(key: SigningPropertyKeys.unsignedBody) else { |
| 80 | + throw Smithy.ClientError.authError( |
| 81 | + "Signing properties passed to the AWSSigV4Signer must contain T/F flag for unsigned body." |
| 82 | + ) |
| 83 | + } |
| 84 | + guard let signingName = signingProperties.get(key: SigningPropertyKeys.signingName) else { |
| 85 | + throw Smithy.ClientError.authError( |
| 86 | + "Signing properties passed to the AWSSigV4Signer must contain signing name." |
| 87 | + ) |
| 88 | + } |
| 89 | + guard let signingRegion = signingProperties.get(key: SigningPropertyKeys.signingRegion) else { |
| 90 | + throw Smithy.ClientError.authError( |
| 91 | + "Signing properties passed to the AWSSigV4Signer must contain signing region." |
| 92 | + ) |
| 93 | + } |
| 94 | + guard let signingAlgorithm = signingProperties.get(key: SigningPropertyKeys.signingAlgorithm) else { |
| 95 | + throw Smithy.ClientError.authError( |
| 96 | + "Signing properties passed to the AWSSigV4Signer must contain signing algorithm." |
| 97 | + ) |
| 98 | + } |
| 99 | + |
| 100 | + let expiration: TimeInterval = signingProperties.get(key: SigningPropertyKeys.expiration) ?? 0 |
| 101 | + let signedBodyHeader: AWSSignedBodyHeader = |
| 102 | + signingProperties.get(key: SigningPropertyKeys.signedBodyHeader) ?? .none |
| 103 | + |
| 104 | + // Determine signed body value |
| 105 | + let checksumIsPresent = signingProperties.get(key: SigningPropertyKeys.checksum) != nil |
| 106 | + let isChunkedEligibleStream = signingProperties.get(key: SigningPropertyKeys.isChunkedEligibleStream) ?? false |
| 107 | + let preComputedSha256 = signingProperties.get(key: AttributeKey<String>(name: "SignedBodyValue")) |
| 108 | + |
| 109 | + let signedBodyValue: AWSSignedBodyValue = determineSignedBodyValue( |
| 110 | + checksumIsPresent: checksumIsPresent, |
| 111 | + isChunkedEligbleStream: isChunkedEligibleStream, |
| 112 | + isUnsignedBody: unsignedBody, |
| 113 | + preComputedSha256: preComputedSha256 |
| 114 | + ) |
| 115 | + |
| 116 | + let flags: SigningFlags = SigningFlags( |
| 117 | + useDoubleURIEncode: signingProperties.get(key: SigningPropertyKeys.useDoubleURIEncode) ?? true, |
| 118 | + shouldNormalizeURIPath: signingProperties.get(key: SigningPropertyKeys.shouldNormalizeURIPath) ?? true, |
| 119 | + omitSessionToken: signingProperties.get(key: SigningPropertyKeys.omitSessionToken) ?? false |
| 120 | + ) |
| 121 | + let signatureType: AWSSignatureType = |
| 122 | + signingProperties.get(key: SigningPropertyKeys.signatureType) ?? .requestHeaders |
| 123 | + |
| 124 | + return AWSSigningConfig( |
| 125 | + credentials: identity, |
| 126 | + expiration: expiration, |
| 127 | + signedBodyHeader: signedBodyHeader, |
| 128 | + signedBodyValue: signedBodyValue, |
| 129 | + flags: flags, |
| 130 | + date: Date(), |
| 131 | + service: signingName, |
| 132 | + region: signingRegion, |
| 133 | + signatureType: signatureType, |
| 134 | + signingAlgorithm: signingAlgorithm |
| 135 | + ) |
| 136 | + } |
| 137 | + |
| 138 | + private func determineSignedBodyValue( |
| 139 | + checksumIsPresent: Bool, |
| 140 | + isChunkedEligbleStream: Bool, |
| 141 | + isUnsignedBody: Bool, |
| 142 | + preComputedSha256: String? |
| 143 | + ) -> AWSSignedBodyValue { |
| 144 | + if !isChunkedEligbleStream { |
| 145 | + // Normal Payloads, Event Streams, etc. |
| 146 | + if isUnsignedBody { |
| 147 | + return .unsignedPayload |
| 148 | + } else if let sha256 = preComputedSha256 { |
| 149 | + return .precomputed(sha256) |
| 150 | + } else { |
| 151 | + return .empty |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + // streaming + eligible for chunked transfer |
| 156 | + if !checksumIsPresent { |
| 157 | + return isUnsignedBody ? .unsignedPayload : .streamingSha256Payload |
| 158 | + } else { |
| 159 | + // checksum is present |
| 160 | + return isUnsignedBody ? .streamingUnsignedPayloadTrailer : .streamingSha256PayloadTrailer |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments