From 685bd48a599e7b6d0136c07efbb00105aed7802c Mon Sep 17 00:00:00 2001 From: Amir Abbas Mousavian Date: Fri, 9 Aug 2024 09:57:25 +0330 Subject: [PATCH] fix: Swift 5 building issue chore: Support Swift 6.0 chore: Update dependencies --- Package.swift | 11 ++-- Package@swift-6.0.swift | 62 +++++++++++++++++++ Sources/JWSETKit/Base/RawType.swift | 10 --- .../Certificate/SecCertificate.swift | 21 +------ .../Cryptography/Compression/Compressor.swift | 4 +- .../JWSETKit/Cryptography/EC/Ed25519.swift | 8 +-- Sources/JWSETKit/Cryptography/KeyParser.swift | 6 +- .../JWSETKit/Entities/JOSE/JOSEHeader.swift | 2 +- 8 files changed, 79 insertions(+), 45 deletions(-) create mode 100644 Package@swift-6.0.swift diff --git a/Package.swift b/Package.swift index 79fa54b..2710e52 100644 --- a/Package.swift +++ b/Package.swift @@ -25,9 +25,9 @@ let package = Package( ], dependencies: [ .package(url: "https://github.com/Flight-School/AnyCodable", .upToNextMajor(from: "0.6.7")), - .package(url: "https://github.com/apple/swift-asn1.git", .upToNextMajor(from: "1.1.0")), - .package(url: "https://github.com/apple/swift-crypto.git", .upToNextMajor(from: "3.4.0")), - .package(url: "https://github.com/apple/swift-certificates", .upToNextMajor(from: "1.4.0")), + .package(url: "https://github.com/apple/swift-asn1.git", .upToNextMajor(from: "1.2.0")), + .package(url: "https://github.com/apple/swift-crypto.git", .upToNextMajor(from: "3.5.0")), + .package(url: "https://github.com/apple/swift-certificates", .upToNextMajor(from: "1.5.0")), .package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", .upToNextMajor(from: "1.8.2")), .package(url: "https://github.com/tsolomko/SWCompression.git", .upToNextMajor(from: "4.8.6")), ], @@ -39,7 +39,7 @@ let package = Package( .product(name: "SwiftASN1", package: "swift-asn1"), .product(name: "X509", package: "swift-certificates"), // Linux support - .product(name: "Crypto", package: "swift-crypto", condition: .when(platforms: .nonDarwin)), + .product(name: "Crypto", package: "swift-crypto"), .product(name: "_CryptoExtras", package: "swift-crypto", condition: .when(platforms: .nonDarwin)), .product(name: "CryptoSwift", package: "CryptoSwift", condition: .when(platforms: .nonDarwin)), .product(name: "SWCompression", package: "SWCompression", condition: .when(platforms: .nonDarwin)), @@ -52,8 +52,7 @@ let package = Package( name: "JWSETKitTests", dependencies: ["JWSETKit"] ), - ], - swiftLanguageVersions: [.v5] + ] ) for target in package.targets { diff --git a/Package@swift-6.0.swift b/Package@swift-6.0.swift new file mode 100644 index 0000000..c4ccf0c --- /dev/null +++ b/Package@swift-6.0.swift @@ -0,0 +1,62 @@ +// swift-tools-version: 6.0 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +extension [Platform] { + static let darwin: [Platform] = [.macOS, .macCatalyst, .iOS, .tvOS, .watchOS, .custom("visionos")] + static let nonDarwin: [Platform] = [.linux, .android, .windows, .wasi, .openbsd] +} + +let package = Package( + name: "JWSETKit", + defaultLocalization: "en", + platforms: [ + .iOS(.v14), + .macOS(.v11), + .tvOS(.v14), + .macCatalyst(.v14), + ], + products: [ + .library( + name: "JWSETKit", + targets: ["JWSETKit"] + ), + ], + dependencies: [ + .package(url: "https://github.com/Flight-School/AnyCodable", .upToNextMajor(from: "0.6.7")), + .package(url: "https://github.com/apple/swift-asn1.git", .upToNextMajor(from: "1.2.0")), + .package(url: "https://github.com/apple/swift-crypto.git", .upToNextMajor(from: "3.5.0")), + .package(url: "https://github.com/apple/swift-certificates", .upToNextMajor(from: "1.5.0")), + .package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", .upToNextMajor(from: "1.8.2")), + .package(url: "https://github.com/tsolomko/SWCompression.git", .upToNextMajor(from: "4.8.6")), + ], + targets: [ + .target( + name: "JWSETKit", + dependencies: [ + "AnyCodable", + .product(name: "SwiftASN1", package: "swift-asn1"), + .product(name: "X509", package: "swift-certificates"), + // Linux support + .product(name: "Crypto", package: "swift-crypto"), + .product(name: "_CryptoExtras", package: "swift-crypto", condition: .when(platforms: .nonDarwin)), + .product(name: "CryptoSwift", package: "CryptoSwift", condition: .when(platforms: .nonDarwin)), + .product(name: "SWCompression", package: "SWCompression", condition: .when(platforms: .nonDarwin)), + ], + resources: [ + .process("PrivacyInfo.xcprivacy"), + ] + ), + .testTarget( + name: "JWSETKitTests", + dependencies: ["JWSETKit"] + ), + ] +) + +for target in package.targets { + target.swiftSettings = [ + .enableUpcomingFeature("ExistentialAny"), + ] +} diff --git a/Sources/JWSETKit/Base/RawType.swift b/Sources/JWSETKit/Base/RawType.swift index fc29fd2..6777e52 100644 --- a/Sources/JWSETKit/Base/RawType.swift +++ b/Sources/JWSETKit/Base/RawType.swift @@ -15,14 +15,4 @@ extension StringRepresentable { public init(stringLiteral value: StringLiteralType) { self.init(rawValue: "\(value)") } - - public init(from decoder: any Decoder) throws { - let container = try decoder.singleValueContainer() - try self.init(rawValue: container.decode(String.self)) - } - - public func encode(to encoder: any Encoder) throws { - var container = encoder.singleValueContainer() - try container.encode(rawValue) - } } diff --git a/Sources/JWSETKit/Cryptography/Certificate/SecCertificate.swift b/Sources/JWSETKit/Cryptography/Certificate/SecCertificate.swift index 3ecce16..b93cc13 100644 --- a/Sources/JWSETKit/Cryptography/Certificate/SecCertificate.swift +++ b/Sources/JWSETKit/Cryptography/Certificate/SecCertificate.swift @@ -28,7 +28,7 @@ extension SecCertificate: JSONWebValidatingKey { public static func create(storage: JSONWebValueStorage) throws -> Self { let key = AnyJSONWebKey(storage: storage) - guard let certificate = key.certificateChain.first, let result = try certificate.secCertificate() as? Self else { + guard let certificate = key.certificateChain.first, let result = try SecCertificate.makeWithCertificate(certificate) as? Self else { throw JSONWebKeyError.keyNotFound } return result @@ -87,7 +87,7 @@ extension SecTrust: JSONWebValidatingKey { public static func create(storage: JSONWebValueStorage) throws -> Self { let key = AnyJSONWebKey(storage: storage) - let certificates = try key.certificateChain.map { try $0.secCertificate() } + let certificates = try key.certificateChain.map(SecCertificate.makeWithCertificate) var result: SecTrust? SecTrustCreateWithCertificates(certificates as CFArray, SecPolicyCreateBasicX509(), &result) guard let result = result as? Self else { @@ -107,23 +107,6 @@ extension SecTrust: Expirable { } } -extension Certificate { - /// Casts `X509.Certificate` into `SecCertificate`. - /// - /// - Returns: A new `SecCertificate` instance. - public func secCertificate() throws -> SecCertificate { - try SecCertificateCreateWithData(kCFAllocatorDefault, derRepresentation as CFData)! - } - - /// Casts `SecCertificate` into `X509.Certificate`. - /// - /// - Parameter secCertificate: `SecCertificate` instance to be casted. - public init(_ secCertificate: SecCertificate) throws { - let der = SecCertificateCopyData(secCertificate) as Data - try self.init(derEncoded: der) - } -} - public func == (lhs: Certificate, rhs: SecCertificate) -> Bool { lhs == (try? Certificate(rhs)) } diff --git a/Sources/JWSETKit/Cryptography/Compression/Compressor.swift b/Sources/JWSETKit/Cryptography/Compression/Compressor.swift index b4328c0..4e9c342 100644 --- a/Sources/JWSETKit/Cryptography/Compression/Compressor.swift +++ b/Sources/JWSETKit/Cryptography/Compression/Compressor.swift @@ -10,7 +10,7 @@ import Foundation import SWCompression extension JSONWebCompressionAlgorithm { - var swCompressor: any CompressionAlgorithm.Type { + var swCompressor: some CompressionAlgorithm.Type { get throws { switch self { case .deflate: @@ -21,7 +21,7 @@ extension JSONWebCompressionAlgorithm { } } - var swDecompressor: any DecompressionAlgorithm.Type { + var swDecompressor: some DecompressionAlgorithm.Type { get throws { switch self { case .deflate: diff --git a/Sources/JWSETKit/Cryptography/EC/Ed25519.swift b/Sources/JWSETKit/Cryptography/EC/Ed25519.swift index b525a74..d163a01 100644 --- a/Sources/JWSETKit/Cryptography/EC/Ed25519.swift +++ b/Sources/JWSETKit/Cryptography/EC/Ed25519.swift @@ -8,7 +8,7 @@ import Foundation import Crypto -extension Crypto.Curve25519.Signing.PublicKey: @retroactive Hashable, Swift.Codable {} +extension Crypto.Curve25519.Signing.PublicKey: Swift.Hashable, Swift.Codable {} extension Curve25519.Signing.PublicKey: CryptoECPublicKey { static var curve: JSONWebKeyCurve { .ed25519 } @@ -30,7 +30,7 @@ extension Curve25519.Signing.PublicKey: CryptoECPublicKey { } } -extension Crypto.Curve25519.KeyAgreement.PublicKey: @retroactive Hashable, Swift.Codable {} +extension Crypto.Curve25519.KeyAgreement.PublicKey: Swift.Hashable, Swift.Codable {} extension Curve25519.KeyAgreement.PublicKey: CryptoECPublicKey { static var curve: JSONWebKeyCurve { .x25519 } @@ -64,7 +64,7 @@ extension Curve25519.Signing.PublicKey: CryptoEdKeyPortable {} extension Curve25519.KeyAgreement.PublicKey: CryptoEdKeyPortable {} -extension Crypto.Curve25519.Signing.PrivateKey: @retroactive Hashable, Swift.Codable {} +extension Crypto.Curve25519.Signing.PrivateKey: Swift.Hashable, Swift.Codable {} extension Curve25519.Signing.PrivateKey: JSONWebSigningKey, CryptoECPrivateKey { public init(algorithm _: some JSONWebAlgorithm) throws { @@ -76,7 +76,7 @@ extension Curve25519.Signing.PrivateKey: JSONWebSigningKey, CryptoECPrivateKey { } } -extension Crypto.Curve25519.KeyAgreement.PrivateKey: @retroactive Hashable, Swift.Codable {} +extension Crypto.Curve25519.KeyAgreement.PrivateKey: Swift.Hashable, Swift.Codable {} extension Curve25519.KeyAgreement.PrivateKey: CryptoECPrivateKey { public init(algorithm _: some JSONWebAlgorithm) throws { diff --git a/Sources/JWSETKit/Cryptography/KeyParser.swift b/Sources/JWSETKit/Cryptography/KeyParser.swift index ae664d4..6d727b4 100644 --- a/Sources/JWSETKit/Cryptography/KeyParser.swift +++ b/Sources/JWSETKit/Cryptography/KeyParser.swift @@ -211,7 +211,7 @@ extension AnyJSONWebKey { } extension [any JSONWebKey] { - func bestMatch(for algorithm: any JSONWebAlgorithm, id: String? = nil) -> Self.Element? { + func bestMatch(for algorithm: some JSONWebAlgorithm, id: String? = nil) -> Self.Element? { guard let keyType = algorithm.keyType else { return nil } let candidates = filter { $0.keyType == keyType && $0.curve == algorithm.curve @@ -225,13 +225,13 @@ extension [any JSONWebKey] { } extension [any JSONWebSigningKey] { - func bestMatch(for algorithm: any JSONWebAlgorithm, id: String? = nil) -> Self.Element? { + func bestMatch(for algorithm: some JSONWebAlgorithm, id: String? = nil) -> Self.Element? { (self as [any JSONWebKey]).bestMatch(for: algorithm, id: id) as? Self.Element } } extension [any JSONWebValidatingKey] { - func bestMatch(for algorithm: any JSONWebAlgorithm, id: String? = nil) -> Self.Element? { + func bestMatch(for algorithm: some JSONWebAlgorithm, id: String? = nil) -> Self.Element? { (self as [any JSONWebKey]).bestMatch(for: algorithm, id: id) as? Self.Element } } diff --git a/Sources/JWSETKit/Entities/JOSE/JOSEHeader.swift b/Sources/JWSETKit/Entities/JOSE/JOSEHeader.swift index 79a17ab..f6d1f40 100644 --- a/Sources/JWSETKit/Entities/JOSE/JOSEHeader.swift +++ b/Sources/JWSETKit/Entities/JOSE/JOSEHeader.swift @@ -32,7 +32,7 @@ public struct JOSEHeader: JSONWebContainer { /// - algorithm: Contains JWA to deremine signing/encryption algorithm. /// - type: Payload type, usually `"JWT"` for JSON Web Token. /// - keyId: Key ID that generated signature. - public init(algorithm: any JSONWebAlgorithm, type: JSONWebContentType, keyId: String? = nil) { + public init(algorithm: some JSONWebAlgorithm, type: JSONWebContentType, keyId: String? = nil) { self.storage = .init() self.algorithm = algorithm self.type = type