diff --git a/Sources/eudiWalletOidcIos/Service/CredentialValidation/SignatureValidator.swift b/Sources/eudiWalletOidcIos/Service/CredentialValidation/SignatureValidator.swift index 55c2695..d1a5cd6 100644 --- a/Sources/eudiWalletOidcIos/Service/CredentialValidation/SignatureValidator.swift +++ b/Sources/eudiWalletOidcIos/Service/CredentialValidation/SignatureValidator.swift @@ -45,21 +45,46 @@ class SignatureValidator { static func processJWKforEBSI(did: String?) async -> [String: Any]{ guard let did = did else { return [:]} let ebsiEndPoint = "https://api-conformance.ebsi.eu/did-registry/v5/identifiers/\(did)" + let pilotEndpoint = "https://api-pilot.ebsi.eu/did-registry/v5/identifiers/\(did)" + do { - guard let url = URL(string: ebsiEndPoint) else { return [:]} + guard let url = URL(string: ebsiEndPoint) else { return [:] } let (data, response) = try await URLSession.shared.data(from: url) - guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else { return [:]} - guard let jsonObject = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any], let verificationMethods = jsonObject["verificationMethod"] as? [[String: Any]] else { return [:]} - for data in verificationMethods { - if let publicKeyJwk = data["publicKeyJwk"] as? [String: Any], let crv = publicKeyJwk["crv"] as? String, crv == "P-256" { - return publicKeyJwk - } + guard let httpResponse = response as? HTTPURLResponse else { return [:] } + + if httpResponse.statusCode == 200 { + // Process the response from the first URL + return try processPublicKeyFromJWKList(data) + } else { + // Call the fallback URL if the status is not 200 + return try await fetchJWKListFromUrl(pilotEndpoint) } } catch { - print("error") + print("Error fetching from primary URL: \(error)") } return [:] } + + private static func processPublicKeyFromJWKList(_ data: Data) throws -> [String: Any] { + guard let jsonObject = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any], + let verificationMethods = jsonObject["verificationMethod"] as? [[String: Any]] else { return [:] } + + for method in verificationMethods { + if let publicKeyJwk = method["publicKeyJwk"] as? [String: Any], + let crv = publicKeyJwk["crv"] as? String, crv == "P-256" { + return publicKeyJwk + } + } + return [:] + } + + private static func fetchJWKListFromUrl(_ fallbackURL: String) async throws -> [String: Any] { + guard let url = URL(string: fallbackURL) else { return [:] } + let (data, response) = try await URLSession.shared.data(from: url) + guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else { return [:] } + + return try processPublicKeyFromJWKList(data) + } static func processJWKFromJwksURI2(kid: String?, jwksURI: String?) async -> [String: Any] { guard let jwksURI = jwksURI else {return [:]} diff --git a/Sources/eudiWalletOidcIos/Service/IssueServiceProtocol.swift b/Sources/eudiWalletOidcIos/Service/IssueServiceProtocol.swift index d7c1bbb..f720ea5 100644 --- a/Sources/eudiWalletOidcIos/Service/IssueServiceProtocol.swift +++ b/Sources/eudiWalletOidcIos/Service/IssueServiceProtocol.swift @@ -52,13 +52,11 @@ protocol IssueServiceProtocol { - credentialEndpointUrlString: The URL string of the credential endpoint. - c_nonce: The nonce value for the credential request. - accessToken: The access token for authentication. - - Returns: A `CredentialResponse` object if the request is successful, otherwise `nil`. */ func processCredentialRequest(did: String, secureKey: SecureKeyData, nonce: String, credentialOffer: CredentialOffer, issuerConfig: IssuerWellKnownConfiguration, accessToken: String, format: String) async -> CredentialResponse? // Processes a deferred credential request to obtain the credential response in deffered manner. - /** - Parameters - acceptanceToken - token which we got from credential request - deferredCredentialEndPoint - end point to call the deferred credential @@ -76,6 +74,6 @@ protocol IssueServiceProtocol { func getCryptoFromIssuerConfig(issuerConfig: IssuerWellKnownConfiguration?, type: String?) -> [String]? - func etCredentialDisplayFromIssuerConfig(issuerConfig: IssuerWellKnownConfiguration?, type: String?) -> Display? + func getCredentialDisplayFromIssuerConfig(issuerConfig: IssuerWellKnownConfiguration?, type: String?) -> Display? }