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

[Injimob 812] rename openId4vp to openID4VP in all files #7

Merged
merged 4 commits into from
Sep 27, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,63 +6,63 @@ public class OpenID4VP {
var authorizationRequest: AuthorizationRequest?
private var presentationDefinitionId: String?
private var responseUri: String?

public init(traceabilityId: String, networkManager: NetworkManaging? = nil) {
self.traceabilityId = traceabilityId
self.networkManager = networkManager ?? NetworkManager.shared
}

public func setPresentationDefinitionId(_ id: String) {
self.presentationDefinitionId = id
}

public func setResponseUri(_ responseUri: String){
self.responseUri = responseUri
}

public func authenticateVerifier(encodedAuthorizationRequest: String, trustedVerifierJSON: [Verifier]) async throws -> AuthenticationResponse {

Logger.setLogTag(className:String(describing: type(of: self)), traceabilityId: traceabilityId)

do {
authorizationRequest = try AuthorizationRequest.getAuthorizationRequest(encodedAuthorizationRequest: encodedAuthorizationRequest, setResponseUri: setResponseUri)

return try AuthenticationResponse.getAuthenticationResponse(authorizationRequest!, trustedVerifierJSON, setPresentationDefinitionId: setPresentationDefinitionId)

} catch(let exception) {
await sendErrorToResponseUri(error: exception, uri: responseUri ?? "")
throw exception
}
}

public func constructVerifiablePresentationToken(credentialsMap: [String: [String]]) async throws -> String? {

return try AuthorizationResponse.constructVpForSigning(credentialsMap)
}

public func shareVerifiablePresentation(vpResponseMetadata: VPResponseMetadata) async throws -> String? {

do {
return try await AuthorizationResponse.shareVp(vpResponseMetadata: vpResponseMetadata,nonce: authorizationRequest!.nonce, responseUri: authorizationRequest!.responseUri,presentationDefinitionId: presentationDefinitionId!, networkManager: networkManager)
} catch(let exception) {
await sendErrorToResponseUri(error: exception, uri: responseUri ?? "")
throw exception
}
}

private func sendErrorToResponseUri(error: Error, uri: String) async {

Logger.getLogTag(className: String(describing: type(of: self)))

guard let url = URL(string: uri) else { return }

let errorInfo = """
{
"error": \(error),
"traceabilityId": \(traceabilityId)
}
"""

do {
let response = try await networkManager.sendHTTPPostRequest(requestBody: errorInfo, url: url)
print("\(String(describing: response))")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import XCTest
class OpenID4VPTests: XCTestCase {
var openID4VP: OpenID4VP!
var mockNetworkManager: MockNetworkManager!

let authorizationRequest = AuthorizationRequest(
clientId: "client_id",
presentationDefinition: "presentationDefinition",
Expand All @@ -15,7 +15,7 @@ class OpenID4VPTests: XCTestCase {
state: "state",
responseUri: "https://example.com"
)

let jws = "wemcn3234ns"
let signatureAlgoType = "RsaSignature2018"
let publicKey = "MIICCgKCAgEA0IEd3E5CvLAbGvr/ysYT2TLE7WDrPBHGk8pwGqVvlrrFtZJ9wT8E"
Expand All @@ -24,9 +24,9 @@ class OpenID4VPTests: XCTestCase {
DescriptorMap(id: "bank_input", format: .ldp_vc, path: "$.verifiableCredential[0]"),
DescriptorMap(id: "bank_input", format: .ldp_vc, path: "$.verifiableCredential[1]")
]

let vpToken = VpTokenForSigning(verifiableCredential: ["VC1", "VC2"],holder: "")

override func setUp() {
super.setUp()
mockNetworkManager = MockNetworkManager()
Expand All @@ -39,13 +39,13 @@ class OpenID4VPTests: XCTestCase {
AuthorizationResponse.descriptorMap = descriptorMap
AuthorizationResponse.vpTokenForSigning = vpToken
}

override func tearDown() {
openID4VP = nil
mockNetworkManager = nil
super.tearDown()
}

let testVerifierList: [[String: Any]] = [
[
"client_id": "https://injiverify.dev2.mosip.net",
Expand All @@ -62,19 +62,19 @@ class OpenID4VPTests: XCTestCase {
]
]
]

let testValidEncodedVpRequest = "OPENID4VP://authorize?Y2xpZW50X2lkPWh0dHBzOi8vaW5qaXZlcmlmeS5kZXYyLm1v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eU1POEE9PSZzdGF0ZT0rbVJRZTFkNnBCb0pxRjZBYjI4a2xnPT0mcmVzcG9uc2VfdXJpPS92ZXJpZmllci92cC1yZXNwb25zZSBIVFRQLzEuMQ=="

let testInvalidPresentationDefinitionVpRequest = "OPENID4VP://authorize?Y2xpZW50X2lkPWh0dHBzOi8vaW5qaXZlcmlmeS5kZXYyLm1vc2lwLm5ldCZwcmVzZW50YXRpb25fZGVmaW5pdGlvbj17ImlucHV0X2Rlc2NyaXB0b3JzIjpbXX0mcmVzcG9uc2VfdHlwZT12cF90b2tlbiZyZXNwb25zZV9tb2RlPWRpcmVjdF9wb3N0Jm5vbmNlPVZiUlJCL0xUeExpWG1WTlp1eU1POEE9PSZzdGF0ZT0rbVJRZTFkNnBCb0pxRjZBYjI4a2xnPT0mcmVzcG9uc2VfdXJpPS92ZXJpZmllci92cC1yZXNwb25zZSBIVFRQLzEuMQ=="

let invalidVpRequest = "OPENID4VP://authorize?Y2xpZW50X2lkPWh0dHBzOi8vaW5qaXZlcmlmeS5kZXYyLm1vc2lwLm5ldCZwcmVzZW50YXRpb25fZGVmaW5pdGlvbj17ImlucHV0X2Rlc2NyaXB0b3JzIjpbXX0mcmVzcG9uc2VfdHlwZT12cF90b2tlbiZyZXNwb25zZV9tb2RlPWRpcmVjdF9wb3N0Jm5vbmNlPVZiUlJCL0xUeExpWG1WTlp1eU1POEE9PSZzdGF0ZT0rbVJRZTFkNnBCb0pxRjZBYjI4a2xnPT0mcmVzcG9uc2VfdXJpPS92ZXJpZmllci92cC1yZXNwb25zZSBIVFRQLzEuMQ=="

func testReturnDataForValidRequest() async {

let verifiers = createVerifiers(from: testVerifierList)

let decoded: Any?

do {
decoded = try await openID4VP.authenticateVerifier(encodedAuthorizationRequest: testValidEncodedVpRequest, trustedVerifierJSON: verifiers)
} catch {
Expand All @@ -83,75 +83,75 @@ class OpenID4VPTests: XCTestCase {
XCTAssertTrue(decoded is AuthenticationResponse, "decodedResponse should be an instance of AuthenticationResponse")
XCTAssertTrue(decoded != nil, "decodedResponse should not be null")
}

func testMissingPresentationDefinitionFields() async {
let verifiers = createVerifiers(from: testVerifierList)

let error = await Task {
try await openID4VP.authenticateVerifier(encodedAuthorizationRequest: testInvalidPresentationDefinitionVpRequest, trustedVerifierJSON: verifiers)
}.result

switch error {
case .failure(let thrownError):
XCTAssertEqual(thrownError as? AuthorizationRequestException, AuthorizationRequestException.invalidPresentationDefinition)
case .success: break
}
}

func testMissingRequiredFieldsInRequest() async {

let verifiers = createVerifiers(from: testVerifierList)

let error = await Task {
try await openID4VP.authenticateVerifier(encodedAuthorizationRequest: invalidVpRequest, trustedVerifierJSON: verifiers)
}.result

switch error {
case .failure(let thrownError):
XCTAssertEqual(thrownError as? AuthorizationRequestException, AuthorizationRequestException.invalidPresentationDefinition)
case .success: break
}
}

func testUUIDGeneration() {

let vpToken = UUIDGenerator.generateUUID()
let presentationSubmissionId = UUIDGenerator.generateUUID()
let presentationSubmission = PresentationSubmission(definition_id: "", descriptor_map: AuthorizationResponse.descriptorMap!)

XCTAssertNotNil(vpToken,presentationSubmissionId)
XCTAssertNotNil(presentationSubmission.id)
}

func testShareVerifiablePresentation() async{
let credentialsMap: [String: [String]] = ["bank_input":["VC1","VC2"]]
let received: String?

do {
received = try await openID4VP.constructVerifiablePresentationToken(credentialsMap: credentialsMap)
}catch{
received = nil
}
XCTAssertNotNil(received, "The response should not be nil for valid credentials map")
}

func testSendVpSuccess() async throws {

let vcResponseMetaData = VPResponseMetadata(jws: jws, signatureAlgorithm: signatureAlgoType, publicKey: publicKey, domain: domain)

let response = try await openID4VP.shareVerifiablePresentation(vpResponseMetadata: vcResponseMetaData)

XCTAssertEqual(response, "Success: Request completed successfully.")
}

func testSendVpFailure() async {

let errorMessage = "Network Request failed with error response: response"
mockNetworkManager.error = NetworkRequestException.networkRequestFailed(message: errorMessage)

let vcResponseMetaData = VPResponseMetadata(jws: jws, signatureAlgorithm: signatureAlgoType, publicKey: publicKey, domain: domain)


do {
let _ = try await openID4VP.shareVerifiablePresentation(vpResponseMetadata: vcResponseMetaData)
} catch let error as NetworkRequestException {
Expand Down
Loading