forked from EWC-consortium/eudi-wallet-oid4vc-ios
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Support for DID:WEB and DID:JWK
- Loading branch information
Showing
6 changed files
with
202 additions
and
96 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
...alletOidcIos/Service/CredentialValidation/PublicKeyExtraction/ProcessEbsiJWKFromKID.swift
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,56 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by oem on 10/10/24. | ||
// | ||
|
||
import Foundation | ||
|
||
class ProcessEbsiJWKFromKID { | ||
|
||
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 [:] } | ||
let (data, response) = try await URLSession.shared.data(from: url) | ||
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 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) | ||
} | ||
} | ||
|
38 changes: 38 additions & 0 deletions
38
...alletOidcIos/Service/CredentialValidation/PublicKeyExtraction/ProcessJWKFromJwksUri.swift
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,38 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by oem on 10/10/24. | ||
// | ||
|
||
import Foundation | ||
|
||
class ProcessJWKFromJwksUri { | ||
|
||
static func processJWKFromJwksURI2(kid: String?, jwksURI: String?) async -> [String: Any] { | ||
guard let jwksURI = jwksURI else {return [:]} | ||
return await fetchJwkData(kid: kid, jwksUri: jwksURI) | ||
} | ||
|
||
static func fetchJwkData(kid: String?, jwksUri: String)async -> [String: Any] { | ||
guard let url = URL(string: jwksUri) else { | ||
return [:] | ||
} | ||
do { | ||
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 keys = jsonObject["keys"] as? [[String: Any]] else { return [:]} | ||
|
||
var jwkKey: [String: Any]? = keys.first { $0["use"] as? String == "sig" } | ||
|
||
if jwkKey == nil, let kid = kid { | ||
jwkKey = keys.first { $0["kid"] as? String == kid } | ||
} | ||
return jwkKey ?? [:] | ||
|
||
} catch { | ||
print("error") | ||
} | ||
return [:] | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...udiWalletOidcIos/Service/CredentialValidation/PublicKeyExtraction/ProcessJWKFromKID.swift
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,26 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by oem on 10/10/24. | ||
// | ||
|
||
import Foundation | ||
|
||
class ProcessJWKFromKID { | ||
static func parseDIDJWK(_ didJwk: String) -> [String: Any]? { | ||
guard didJwk.hasPrefix("did:jwk:") else { | ||
return nil | ||
} | ||
|
||
let base64UrlValue = didJwk.replacingOccurrences(of: "did:jwk:", with: "") | ||
|
||
guard let jsonString = base64UrlValue.decodeBase64(), | ||
let jsonData = jsonString.data(using: .utf8), | ||
let jwk = try? JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: Any] else { | ||
return nil | ||
} | ||
|
||
return jwk | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...WalletOidcIos/Service/CredentialValidation/PublicKeyExtraction/ProcessKeyJWKFromKID.swift
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,19 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by oem on 10/10/24. | ||
// | ||
|
||
import Foundation | ||
|
||
class ProcessKeyJWKFromKID { | ||
static func processJWKfromKid(did: String?) -> [String: Any] { | ||
guard let did = did else { return [:]} | ||
let components = did.split(separator: "#") | ||
guard let didPart = components.first else { | ||
return [:] | ||
} | ||
return DidService.shared.createJWKfromDID(did: String(didPart)) | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...WalletOidcIos/Service/CredentialValidation/PublicKeyExtraction/ProcessWebJWKFromKID.swift
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,35 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by oem on 10/10/24. | ||
// | ||
|
||
import Foundation | ||
|
||
class ProcessWebJWKFromKID { | ||
static func fetchDIDDocument(did: String) async throws -> [String: Any]? { | ||
guard did.hasPrefix("did:web:") else { return nil } | ||
|
||
let didWithoutPrefix = did.replacingOccurrences(of: "did:web:", with: "") | ||
let didParts = didWithoutPrefix.split(separator: ":") | ||
|
||
guard didParts.count > 1 else { return nil } | ||
|
||
let host = didParts[0] | ||
let path = didParts[1].split(separator: "#").first ?? "" | ||
let didDocURLString = "https://\(host)/\(path)/did.json" | ||
|
||
guard let didDocURL = URL(string: didDocURLString) else { return nil } | ||
|
||
let (data, response) = try await URLSession.shared.data(from: didDocURL) | ||
|
||
guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else { | ||
return nil | ||
} | ||
|
||
let didDoc = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] | ||
|
||
return didDoc | ||
} | ||
} |
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