From f94a63dec0a66a47722b872d5d7bbb4dc5db38e4 Mon Sep 17 00:00:00 2001 From: ozaki Date: Sun, 10 Nov 2024 13:55:09 +0900 Subject: [PATCH] Fixed a bug related to parsing Disclosure. --- .../Services/OID/PresentationExchange.swift | 7 ++-- .../AuthorizationRquestTests.swift | 40 +++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/tw2023_wallet/Services/OID/PresentationExchange.swift b/tw2023_wallet/Services/OID/PresentationExchange.swift index eed4b92..3ea62a4 100644 --- a/tw2023_wallet/Services/OID/PresentationExchange.swift +++ b/tw2023_wallet/Services/OID/PresentationExchange.swift @@ -72,11 +72,12 @@ struct PresentationDefinition: Codable { func matchSdJwtVcToRequirement(sdJwt: String) -> ( InputDescriptor, [DisclosureWithOptionality] )? { - let parts = sdJwt.split(separator: "~").map(String.init) - let newList = parts.count > 2 ? Array(parts.dropFirst().dropLast()) : [] + guard let sdJwtParts = try? SDJwtUtil.divideSDJwt(sdJwt: sdJwt) else { + return nil + } // [Disclosure] - let allDisclosures = decodeDisclosureFunction(newList) + let allDisclosures = decodeDisclosureFunction(sdJwtParts.disclosures) let sourcePayload = Dictionary( uniqueKeysWithValues: allDisclosures.compactMap { disclosure in if let key = disclosure.key, let value = disclosure.value { diff --git a/tw2023_walletTests/AuthorizationRquestTests.swift b/tw2023_walletTests/AuthorizationRquestTests.swift index c2b8551..083a3ce 100644 --- a/tw2023_walletTests/AuthorizationRquestTests.swift +++ b/tw2023_walletTests/AuthorizationRquestTests.swift @@ -235,6 +235,46 @@ final class AuthorizationRquestTests: XCTestCase { } } } + + func testPresentationDefinitionMatchSdJwt() { + let configuration = URLSessionConfiguration.ephemeral + configuration.protocolClasses = [MockURLProtocol.self] + let mockSession = URLSession(configuration: configuration) + + let testURL = URL(string: "https://example.com/presentation_definition.json")! + guard + let url = Bundle.main.url( + forResource: "presentation_definition", withExtension: "json"), + let mockData = try? Data(contentsOf: url) + else { + XCTFail("Cannot read presentation_definition.json") + return + } + let response = HTTPURLResponse( + url: url, statusCode: 200, httpVersion: nil, headerFields: nil) + MockURLProtocol.mockResponses[testURL.absoluteString] = (mockData, response) + + let authorizationRequest = AuthorizationRequestPayloadImpl( + presentationDefinition: nil + ) + let requestObject = RequestObjectPayloadImpl( + presentationDefinitionUri: testURL.absoluteString + ) + + runAsyncTest { + do { + let pdOptional = try await processPresentationDefinition( + authorizationRequest, requestObject, using: mockSession) + let pd = try XCTUnwrap(pdOptional, "PresentationDefinition should not be nil.") + let sdJwt = "eyJ0eXAiOiJzZCtqd3QiLCJhbGciOiJFUzI1NiJ9.eyJfc2QiOlsiOWhnWm5VbGEyT1JhTHB3Wkp6T0pBTUZfVUd2dzVOekIwTEdmU1VaNTN6cyJdLCJfc2RfYWxnIjoiU0hBLTI1NiJ9.nzsiKRK39ijCaw0oD9nmhrB41HnZj_CiShckWZAVRW3tCDTm3vrJHyoVj4F7_2mx2aMvbT4iAekDGGtsXyhdvw~WyJmZWE3MTcwYTc3OGRiNzk1IiwiaXNfb2xkZXJfdGhhbl8xMyIsdHJ1ZV0~" + XCTAssertNotNil(pd.matchSdJwtVcToRequirement(sdJwt: sdJwt)) + XCTAssertEqual(pd.id, "12345") + } + catch { + XCTFail("Request should not fail. \(error)") + } + } + } func testPresentationDefinitionFromQueryParameter() { let configuration = URLSessionConfiguration.ephemeral