Skip to content

Commit 9d6d175

Browse files
authored
feat: change SignedBodyValue enum to allow precomputed sha256 hash (#271)
1 parent 4719842 commit 9d6d175

2 files changed

Lines changed: 43 additions & 12 deletions

File tree

Source/AwsCommonRuntimeKit/auth/signing/SigningConfig.swift

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public struct SigningConfig: CStructWithUserData {
119119
return withByteCursorFromStrings(
120120
region,
121121
service,
122-
signedBodyValue.rawValue) { regionCursor, serviceCursor, signedBodyValueCursor in
122+
signedBodyValue.description) { regionCursor, serviceCursor, signedBodyValueCursor in
123123

124124
cConfig.region = regionCursor
125125
cConfig.service = serviceCursor
@@ -174,25 +174,56 @@ public enum SignedBodyHeaderType {
174174
/// Optional string to use as the canonical request's body value.
175175
/// Typically, this is the SHA-256 of the (request/chunk/event) payload, written as lowercase hex.
176176
/// If this has been precalculated, it can be set here. Special values used by certain services can also be set.
177-
public enum SignedBodyValue: String {
177+
public enum SignedBodyValue: CustomStringConvertible, Equatable {
178178
/// if empty, a public value will be calculated from the payload during signing
179-
case empty = ""
179+
case empty
180180
/// For empty sha256
181-
case emptySha256 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
181+
case emptySha256
182+
/// Use this to provide a precalculated sha256 value
183+
case precomputedSha256(String)
182184
/// Use this in the case of needing to not use the payload for signing
183-
case unsignedPayload = "UNSIGNED-PAYLOAD"
185+
case unsignedPayload
184186
/// For streaming sha256 payload
185-
case streamingSha256Payload = "STREAMING-AWS4-HMAC-SHA256-PAYLOAD"
187+
case streamingSha256Payload
186188
/// For streaming sha256 payload trailer
187-
case streamingSha256PayloadTrailer = "STREAMING-AWS4-HMAC-SHA256-PAYLOAD-TRAILER"
189+
case streamingSha256PayloadTrailer
188190
/// For streaming sigv4a sha256 payload
189-
case streamingECDSA_P256Sha256Payload = "STREAMING-AWS4-ECDSA-P256-SHA256-PAYLOAD"
191+
case streamingECDSA_P256Sha256Payload
190192
/// For streaming sigv4a sha256 payload trailer
191-
case streamingECDSA_P256Sha256PayloadTrailer = "STREAMING-AWS4-ECDSA-P256-SHA256-PAYLOAD-TRAILER"
193+
case streamingECDSA_P256Sha256PayloadTrailer
192194
/// For streaming sigv4a sha256 events
193-
case streamingSha256Events = "STREAMING-AWS4-HMAC-SHA256-EVENTS"
195+
case streamingSha256Events
194196
/// For streaming unsigned payload trailer
195-
case streamingUnSignedPayloadTrailer = "STREAMING-UNSIGNED-PAYLOAD-TRAILER"
197+
case streamingUnSignedPayloadTrailer
198+
199+
public var description: String {
200+
switch self {
201+
case .empty:
202+
return ""
203+
case .emptySha256:
204+
return "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
205+
case .precomputedSha256(let value):
206+
return value
207+
case .unsignedPayload:
208+
return "UNSIGNED-PAYLOAD"
209+
case .streamingSha256Payload:
210+
return "STREAMING-AWS4-HMAC-SHA256-PAYLOAD"
211+
case .streamingSha256PayloadTrailer:
212+
return "STREAMING-AWS4-HMAC-SHA256-PAYLOAD-TRAILER"
213+
case .streamingECDSA_P256Sha256Payload:
214+
return "STREAMING-AWS4-ECDSA-P256-SHA256-PAYLOAD"
215+
case .streamingECDSA_P256Sha256PayloadTrailer:
216+
return "STREAMING-AWS4-ECDSA-P256-SHA256-PAYLOAD-TRAILER"
217+
case .streamingSha256Events:
218+
return "STREAMING-AWS4-HMAC-SHA256-EVENTS"
219+
case .streamingUnSignedPayloadTrailer:
220+
return "STREAMING-UNSIGNED-PAYLOAD-TRAILER"
221+
}
222+
}
223+
224+
public static func == (lhs: SignedBodyValue, rhs: SignedBodyValue) -> Bool {
225+
return lhs.description == rhs.description
226+
}
196227
}
197228

198229
public enum SigningAlgorithmType {

Test/AwsCommonRuntimeKitTests/auth/SigningConfigTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class SigningConfigTests: XCBaseTestCase {
2929
XCTAssertNotNil(cSigningConfig.credentials)
3030
XCTAssertEqual(UInt64(signingConfig.expiration!), cSigningConfig.expiration_in_seconds)
3131
XCTAssertEqual(signingConfig.signedBodyHeader.rawValue, cSigningConfig.signed_body_header)
32-
XCTAssertEqual(signingConfig.signedBodyValue.rawValue, cSigningConfig.signed_body_value.toString())
32+
XCTAssertEqual(signingConfig.signedBodyValue.description, cSigningConfig.signed_body_value.toString())
3333
}
3434
}
3535
}

0 commit comments

Comments
 (0)