Skip to content

Commit

Permalink
fix: Swift 5 building issue
Browse files Browse the repository at this point in the history
chore: Support Swift 6.0
chore: Update dependencies
  • Loading branch information
amosavian committed Aug 9, 2024
1 parent 3ab31a3 commit 685bd48
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 45 deletions.
11 changes: 5 additions & 6 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
],
Expand All @@ -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)),
Expand All @@ -52,8 +52,7 @@ let package = Package(
name: "JWSETKitTests",
dependencies: ["JWSETKit"]
),
],
swiftLanguageVersions: [.v5]
]
)

for target in package.targets {
Expand Down
62 changes: 62 additions & 0 deletions Package@swift-6.0.swift
Original file line number Diff line number Diff line change
@@ -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"),
]
}
10 changes: 0 additions & 10 deletions Sources/JWSETKit/Base/RawType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
21 changes: 2 additions & 19 deletions Sources/JWSETKit/Cryptography/Certificate/SecCertificate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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))
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/JWSETKit/Cryptography/Compression/Compressor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -21,7 +21,7 @@ extension JSONWebCompressionAlgorithm {
}
}

var swDecompressor: any DecompressionAlgorithm.Type {
var swDecompressor: some DecompressionAlgorithm.Type {
get throws {
switch self {
case .deflate:
Expand Down
8 changes: 4 additions & 4 deletions Sources/JWSETKit/Cryptography/EC/Ed25519.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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 }
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions Sources/JWSETKit/Cryptography/KeyParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}
2 changes: 1 addition & 1 deletion Sources/JWSETKit/Entities/JOSE/JOSEHeader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 685bd48

Please sign in to comment.