-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
222 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// | ||
// Copyright RevenueCat Inc. All Rights Reserved. | ||
// | ||
// Licensed under the MIT License (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://opensource.org/licenses/MIT | ||
// | ||
// HTTPRequest+Signing.swift | ||
// | ||
// Created by Nacho Soto on 11/16/23. | ||
|
||
import CryptoKit | ||
import Foundation | ||
|
||
extension HTTPRequest { | ||
|
||
static func headerParametersForSignatureHeader(headers: Headers) -> String? { | ||
guard #available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.2, *) else { | ||
// Signature verification is not available. | ||
return nil | ||
} | ||
|
||
let headersToSign = Self.headersToSign.map(\.rawValue) | ||
|
||
if !headersToSign.isEmpty, let hash = Self.postParameterHash(headers) { | ||
return Self.signatureHashHeader(keys: headersToSign, hash: hash) | ||
} else { | ||
return nil | ||
} | ||
} | ||
|
||
/// - Returns: `nil` if none of the requested headers are found | ||
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.2, *) | ||
private static func postParameterHash(_ headers: Headers) -> String? { | ||
let headersToSign = Self.headersToSign.map(\.rawValue) | ||
let values = headers | ||
.filter { headersToSign.contains($0.key) } | ||
.map(\.value) | ||
|
||
guard !values.isEmpty else { return nil } | ||
|
||
return Self.signingParameterHash(values) | ||
} | ||
|
||
} | ||
|
||
extension HTTPRequest { | ||
|
||
static func signatureHashHeader( | ||
keys: [String], | ||
hash: String | ||
) -> String { | ||
return [ | ||
keys.joined(separator: ","), | ||
postParameterHashingAlgorithmName, | ||
hash | ||
].joined(separator: ":") | ||
} | ||
|
||
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.2, *) | ||
static func signingParameterHash(_ values: [String]) -> String { | ||
var sha256 = SHA256() | ||
|
||
for (index, value) in values.enumerated() { | ||
if index > 0 { | ||
sha256.update(data: fieldSeparator) | ||
} | ||
|
||
sha256.update(data: value.asData) | ||
} | ||
|
||
return sha256.toString() | ||
} | ||
|
||
} | ||
|
||
// MARK: - Private | ||
|
||
private extension HTTPRequest { | ||
|
||
/// Ordered list of header keys that will be included in the signature. | ||
static let headersToSign: [HTTPClient.RequestHeader] = [ | ||
.sandbox | ||
] | ||
|
||
} | ||
|
||
private let postParameterHashingAlgorithmName = "sha256" | ||
private let fieldSeparator = Data(bytes: [0x00], count: 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...ps/PurchaseTesterSwiftUI/PurchaseTester.xcworkspace/xcshareddata/swiftpm/Package.resolved
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"pins" : [ | ||
{ | ||
"identity" : "purchases-ios", | ||
"kind" : "remoteSourceControl", | ||
"location" : "https://github.com/RevenueCat/purchases-ios", | ||
"state" : { | ||
"branch" : "main", | ||
"revision" : "b57a1bed83ba0a7a8e20d41c937cc43c9cdc8149" | ||
} | ||
} | ||
], | ||
"version" : 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.