Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift 6 migration #38

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version: 5.8.1
// swift-tools-version: 6.0.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand Down
15 changes: 9 additions & 6 deletions Sources/Digest/DigestCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@
*/
import Foundation

class DigestCreator {
final class DigestCreator: Sendable {

// MARK: - Properties

var hashingAlgorithm: HashingAlgorithm

let saltProvider = DefaultSaltProvider()
let hashingAlgorithm: HashingAlgorithm
let saltProvider: SaltProvider

// MARK: - LifeCycle

init(hashingAlgorithm: HashingAlgorithm = SHA256Hashing()) {
init(
hashingAlgorithm: HashingAlgorithm = SHA256Hashing(),
saltProvider: SaltProvider = DefaultSaltProvider()
) {
self.hashingAlgorithm = hashingAlgorithm
self.saltProvider = saltProvider
}

// MARK: - Methods
Expand All @@ -47,7 +50,7 @@ class DigestCreator {

}

public enum DigestType: RawRepresentable, Hashable {
public enum DigestType: RawRepresentable, Hashable, Sendable {

public typealias RawValue = DisclosureDigest

Expand Down
2 changes: 1 addition & 1 deletion Sources/Digest/HashingAlgorithm/HashingAlgorithm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Foundation
public typealias Disclosure = String
public typealias DisclosureDigest = String

protocol HashingAlgorithm {
protocol HashingAlgorithm: Sendable {
var identifier: String { get }

func hash(disclosure: Disclosure) -> Data?
Expand Down
4 changes: 2 additions & 2 deletions Sources/Digest/HashingAlgorithm/SHA256.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
import Foundation
import CryptoKit

class SHA256Hashing: HashingAlgorithm {
final class SHA256Hashing: HashingAlgorithm {

// MARK: - Properties

var identifier: String = "sha-256"
let identifier: String = "sha-256"

// MARK: - Methods

Expand Down
4 changes: 2 additions & 2 deletions Sources/Digest/HashingAlgorithm/SHA384.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
import Foundation
import CryptoKit

class SHA384Hashing: HashingAlgorithm {
final class SHA384Hashing: HashingAlgorithm {

// MARK: - Properties

var identifier: String = "sha-384"
let identifier: String = "sha-384"

// MARK: - Methods

Expand Down
4 changes: 2 additions & 2 deletions Sources/Digest/HashingAlgorithm/SHA512.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
import Foundation
import CryptoKit

class SHA512Hashing: HashingAlgorithm {
final class SHA512Hashing: HashingAlgorithm {

// MARK: - Properties

var identifier: String = "sha-512"
let identifier: String = "sha-512"

// MARK: - Methods

Expand Down
4 changes: 2 additions & 2 deletions Sources/Digest/SaltProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import Foundation

typealias Salt = String

protocol SaltProvider {
protocol SaltProvider: Sendable {
var salt: Data { get }
var saltString: Salt { get }
}

class DefaultSaltProvider: SaltProvider {
final class DefaultSaltProvider: SaltProvider {

// MARK: - Properties

Expand Down
4 changes: 3 additions & 1 deletion Sources/Factory/SDJWTFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ class SDJWTFactory {
private func encodeObject(sdJwtObject: [String: SdElement]?) throws -> ClaimSet {
// Check if the input object is of correct format
guard let sdJwtObject else {
throw SDJWTError.nonObjectFormat(ofElement: sdJwtObject)
throw SDJWTError.nonObjectFormat(
ofElement: (try? sdJwtObject?.toJSONString()) ?? ""
)
}

// Initialize arrays to store disclosures and JSON output
Expand Down
5 changes: 3 additions & 2 deletions Sources/Fetchers/SdJwtVcIssuerMetaDataFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/
import Foundation
import SwiftyJSON
import JSONWebKey
@preconcurrency import JSONWebKey
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @dtsiflit . I think version 4.0.1 already has swift 6 support ;) so this would not be needed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much @goncalo-frade-iohk :) I've updated the PR to reflect this.


public protocol SdJwtVcIssuerMetaDataFetching {
var session: Networking { get }
func fetchIssuerMetaData(issuer: URL) async throws -> SdJwtVcIssuerMetaData?
@MainActor func fetchIssuerMetaData(issuer: URL) async throws -> SdJwtVcIssuerMetaData?
}

public class SdJwtVcIssuerMetaDataFetcher: SdJwtVcIssuerMetaDataFetching {
Expand Down Expand Up @@ -70,6 +70,7 @@ private extension SdJwtVcIssuerMetaDataFetcher {
return components.url!
}

@MainActor
func fetch<T: Decodable>(from url: URL, with session: Networking) async throws -> T {

let (data, response) = try await session.data(from: url)
Expand Down
4 changes: 2 additions & 2 deletions Sources/Issuer/JWT.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
*/
import Foundation
import JSONWebAlgorithms
import JSONWebSignature
@preconcurrency import JSONWebSignature
import SwiftyJSON

public struct JWT: JWTRepresentable {
public struct JWT: JWTRepresentable, Sendable {

// MARK: - Properties

Expand Down
5 changes: 3 additions & 2 deletions Sources/Issuer/SDJWT.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import JSONWebToken
import SwiftyJSON

public typealias KBJWT = JWT
extension JWS: @unchecked @retroactive Sendable {}

struct SDJWT {
struct SDJWT: Sendable {

// MARK: - Properties

Expand Down Expand Up @@ -67,7 +68,7 @@ struct SDJWT {
}
}

public struct SignedSDJWT {
public struct SignedSDJWT: Sendable {

// MARK: - Properties

Expand Down
4 changes: 2 additions & 2 deletions Sources/Model/SdJwtVcIssuerMetaData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* limitations under the License.
*/
import Foundation
import JSONWebKey
@preconcurrency import JSONWebKey
import SwiftyJSON

struct SdJwtVcIssuerMetadataTO: Decodable {
struct SdJwtVcIssuerMetadataTO: Decodable, Sendable {
let issuer: String
let jwksUri: String?
let jwks: JWKSet?
Expand Down
2 changes: 1 addition & 1 deletion Sources/Networking/Networking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import Foundation

extension URLSession: Networking {}

public protocol Networking {
public protocol Networking: Sendable {
func data(
from url: URL
) async throws -> (Data, URLResponse)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public enum SDJWTError: Error {
case encodingError
case discloseError
case serializationError
case nonObjectFormat(ofElement: Any)
case nonObjectFormat(ofElement: String)
case keyCreation
case algorithmMissMatch
case noneAsAlgorithm
Expand Down
2 changes: 1 addition & 1 deletion Sources/Utilities/Extensions/JSON+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import SwiftyJSON
@preconcurrency import SwiftyJSON

extension JSON {
subscript(key: Keys) -> JSON {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Utilities/TimeRange.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
import Foundation

public struct TimeRange {
public struct TimeRange: Sendable {
let startTime: Date
let endTime: Date

Expand Down
25 changes: 17 additions & 8 deletions Sources/Verifier/ClaimsVerifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
import Foundation
import SwiftyJSON

public class ClaimsVerifier: VerifierProtocol {
public final class ClaimsVerifier: VerifierProtocol {

// MARK: - Properties
var iat: Date?
var iatValidWindow: TimeRange?
let iat: Date?
let iatValidWindow: TimeRange?

var nbf: Date?
var exp: Date?
let nbf: Date?
let exp: Date?

var audClaim: JSON?
var expectedAud: String?
let audClaim: JSON?
let expectedAud: String?

let currentDate: Date

Expand All @@ -43,25 +43,34 @@ public class ClaimsVerifier: VerifierProtocol {

if let iat {
self.iat = Date(timeIntervalSince1970: TimeInterval(iat))
} else {
self.iat = nil
}

if let nbf {
self.nbf = Date(timeIntervalSince1970: TimeInterval(nbf))
} else {
self.nbf = nil
}

if let exp {
self.exp = Date(timeIntervalSince1970: TimeInterval(exp))
} else {
self.exp = nil
}

self.audClaim = JSON(parseJSON: audClaim ?? "")
self.expectedAud = expectedAud
self.currentDate = currentDate
self.iatValidWindow = iatValidWindow
}

// MARK: - Methods
@discardableResult
public func verify() throws -> Bool {
if let iat,
let iatValidWindow,
iatValidWindow.contains(date: iat) {
!iatValidWindow.contains(date: iat) {
throw SDJWTVerifierError.invalidJwt
}

Expand Down
20 changes: 11 additions & 9 deletions Sources/Verifier/DisclosuresVerifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ public struct DisclosuresVerifierOutput {
var recreatedClaims: JSON
}

public class DisclosuresVerifier: VerifierProtocol {
public final class DisclosuresVerifier: VerifierProtocol {

// MARK: - Properties

let disclosuresReceivedInSDJWT: [Disclosure]
var digestsFoundOnPayload: [DigestType] = []
let digestsFoundOnPayload: [DigestType]
let digestCreator: DigestCreator
var digestsOfDisclosuresDict: [DisclosureDigest: Disclosure]
let digestsOfDisclosuresDict: [DisclosureDigest: Disclosure]

private let sdJwt: SDJWT
private var recreatedClaims: JSON = .empty
private let recreatedClaims: JSON

// MARK: - Lifecycle

Expand All @@ -44,19 +44,21 @@ public class DisclosuresVerifier: VerifierProtocol {

self.disclosuresReceivedInSDJWT = sdJwt.disclosures

digestsOfDisclosuresDict = [:]
var dict: [DisclosureDigest: Disclosure] = [:]
for disclosure in disclosuresReceivedInSDJWT {
let hashed = digestCreator.hashAndBase64Encode(input: disclosure)
if let hashed {
self.digestsOfDisclosuresDict[hashed] = disclosure
dict[hashed] = disclosure
} else {
throw SDJWTVerifierError.failedToCreateVerifier
}
}

digestsOfDisclosuresDict = dict

let claimExtractor =
try ClaimExtractor(digestsOfDisclosuresDict: digestsOfDisclosuresDict)
.findDigests(payload: sdJwt.jwt.payload, disclosures: sdJwt.disclosures)
try ClaimExtractor(
digestsOfDisclosuresDict: digestsOfDisclosuresDict
).findDigests(payload: sdJwt.jwt.payload, disclosures: sdJwt.disclosures)

digestsFoundOnPayload = claimExtractor.digestsFoundOnPayload
recreatedClaims = claimExtractor.recreatedClaims
Expand Down
15 changes: 6 additions & 9 deletions Sources/Verifier/KeyBindingVerifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ import JSONWebKey
import JSONWebSignature
import SwiftyJSON

public class KeyBindingVerifier: VerifierProtocol {
public final class KeyBindingVerifier: VerifierProtocol {

static let kbJwt = "kb+jwt"

private var signatureVerifier: SignatureVerifier?

public init() {
}

Expand All @@ -49,7 +47,8 @@ public class KeyBindingVerifier: VerifierProtocol {
throw SDJWTVerifierError.keyBindingFailed(description: "No Nonce Provided")
}

self.signatureVerifier = try SignatureVerifier(signedJWT: challenge, publicKey: extractedKey)
let signatureVerifier = try SignatureVerifier(signedJWT: challenge, publicKey: extractedKey)
_ = try signatureVerifier.verify()

try verifyIat(iatOffset: iatOffset, iat: Date(timeIntervalSince1970: TimeInterval(timeInterval)))
try verifyAud(aud: aud, expectedAudience: expectedAudience)
Expand All @@ -69,15 +68,13 @@ public class KeyBindingVerifier: VerifierProtocol {
throw SDJWTVerifierError.keyBindingFailed(description: "No Nonce Provided")
}

self.signatureVerifier = try SignatureVerifier(signedJWT: challenge, publicKey: extractedKey)
let signatureVerifier = try SignatureVerifier(signedJWT: challenge, publicKey: extractedKey)
_ = try signatureVerifier.verify()
}

@discardableResult
public func verify() throws -> JWS {
guard let verifier = signatureVerifier else {
throw SDJWTVerifierError.keyBindingFailed(description: "Invalid signature verifier")
}
return try verifier.verify()
throw SDJWTVerifierError.keyBindingFailed(description: "Invalid signature verifier")
}
}

Expand Down
Loading