Skip to content

Commit

Permalink
Merge pull request #5 from eu-digital-identity-wallet/develop
Browse files Browse the repository at this point in the history
Develop: limit main actor usage, reader cert variables
  • Loading branch information
psima authored Dec 4, 2023
2 parents d3fc74f + 724755a commit b153a48
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class OpenId4VpService: PresentationService {
let rsaPublicKey = try? KeyController.generateRSAPublicKey(from: rsaPrivateKey) else { return nil }
guard let rsaJWK = try? RSAPublicKey(publicKey: rsaPublicKey, additionalParameters: ["use": "sig", "kid": UUID().uuidString, "alg": "RS256"]) else { return nil }
guard let keySet = try? WebKeySet(jwk: rsaJWK) else { return nil }
var res = WalletOpenId4VPConfiguration(subjectSyntaxTypesSupported: [], preferredSubjectSyntaxType: .jwkThumbprint, decentralizedIdentifier: try! DecentralizedIdentifier(rawValue: "did:example:123"), idTokenTTL: 10 * 60, presentationDefinitionUriSupported: true, signingKey: privateKey, signingKeySet: keySet, supportedClientIdSchemes: [.preregistered(clients: [verifierMetaData.clientId: verifierMetaData])], vpFormatsSupported: [])
let res = WalletOpenId4VPConfiguration(subjectSyntaxTypesSupported: [], preferredSubjectSyntaxType: .jwkThumbprint, decentralizedIdentifier: try! DecentralizedIdentifier(rawValue: "did:example:123"), idTokenTTL: 10 * 60, presentationDefinitionUriSupported: true, signingKey: privateKey, signingKeySet: keySet, supportedClientIdSchemes: [.preregistered(clients: [verifierMetaData.clientId: verifierMetaData])], vpFormatsSupported: [])
return res
}

Expand Down
41 changes: 21 additions & 20 deletions Sources/eudi-lib-ios-wallet-kit/Services/PresentationSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class PresentationSession: ObservableObject {
@Published public var readerCertIssuer: String?
/// Reader certificate validation message (only for BLE transfer wih verifier using reader authentication)
@Published public var readerCertValidationMessage: String?
/// Reader certificate issuer is valid (only for BLE transfer wih verifier using reader authentication)
@Published public var readerCertIssuerValid: Bool?
/// Error message when the ``status`` is in the error state.
@Published public var uiError: WalletError?
/// Request items selected by the user to be sent to verifier.
Expand All @@ -50,7 +52,7 @@ public class PresentationSession: ObservableObject {
///
/// The ``disclosedDocuments`` property will be set. Additionally ``readerCertIssuer`` and ``readerCertValidationMessage`` may be set for the BLE proximity flow
/// - Parameter request: Keys are defined in the ``UserRequestKeys``
public func decodeRequest(_ request: [String: Any]) {
func decodeRequest(_ request: [String: Any]) {
// show the items as checkboxes
guard let validRequestItems = request[UserRequestKeys.valid_items_requested.rawValue] as? RequestItems else { return }
var tmp = validRequestItems.toDocElementViewModels(valid: true)
Expand All @@ -60,16 +62,17 @@ public class PresentationSession: ObservableObject {
disclosedDocuments = tmp
if let readerAuthority = request[UserRequestKeys.reader_certificate_issuer.rawValue] as? String {
readerCertIssuer = readerAuthority
readerCertValidationMessage = request[UserRequestKeys.reader_certificate_validation_message.rawValue] as? String ?? ""
readerCertIssuerValid = request[UserRequestKeys.reader_auth_validated.rawValue] as? Bool
readerCertValidationMessage = request[UserRequestKeys.reader_certificate_validation_message.rawValue] as? String
}
status = .requestReceived
}

static func makeError(str: String) -> NSError {
logger.error(Logger.Message(unicodeScalarLiteral: str))
return NSError(domain: "\(PresentationSession.self)", code: 0, userInfo: [NSLocalizedDescriptionKey: str])
}

@MainActor
/// Start QR engagement to be presented to verifier
///
/// On success ``deviceEngagement`` published variable will be set with the result and ``status`` will be ``.qrEngagementReady``
Expand All @@ -78,16 +81,20 @@ public class PresentationSession: ObservableObject {
do {
let data = try await presentationService.startQrEngagement()
if let data, data.count > 0 {
deviceEngagement = data
status = .qrEngagementReady
await MainActor.run {
deviceEngagement = data
status = .qrEngagementReady
}
}
} catch {
status = .error
uiError = WalletError(description: error.localizedDescription, code: (error as NSError).code, userInfo: (error as NSError).userInfo)
}
} catch { await setError(error) }
}

@MainActor
func setError(_ error: Error) {
status = .error
uiError = WalletError(description: error.localizedDescription, code: (error as NSError).code, userInfo: (error as NSError).userInfo)
}

/// Receive request from verifer
///
/// The request is futher decoded internally. See also ``decodeRequest(_:)``
Expand All @@ -97,36 +104,30 @@ public class PresentationSession: ObservableObject {
public func receiveRequest() async -> [String: Any]? {
do {
let request = try await presentationService.receiveRequest()
decodeRequest(request)
status = .requestReceived
await decodeRequest(request)
return request
} catch {
uiError = WalletError(description: error.localizedDescription, code: (error as NSError).code, userInfo: (error as NSError).userInfo)
status = .error
await setError(error)
return nil
}
}

@MainActor
/// Send response to verifier
/// - Parameters:
/// - userAccepted: Whether user confirmed to send the response
/// - itemsToSend: Data to send organized into a hierarcy of doc.types and namespaces
/// - onCancel: Action to perform if the user cancels the biometric authentication
public func sendResponse(userAccepted: Bool, itemsToSend: RequestItems, onCancel: (() -> Void)?) async {
do {
status = .userSelected
await MainActor.run {status = .userSelected }
let action = { [ weak self] in _ = try await self?.presentationService.sendResponse(userAccepted: userAccepted, itemsToSend: itemsToSend) }
if EudiWallet.standard.userAuthenticationRequired {
try await EudiWallet.authorizedAction(dismiss: { onCancel?()}, action: action )
} else {
try await action()
}
status = .responseSent
} catch {
status = .error
uiError = WalletError(description: error.localizedDescription, code: (error as NSError).code, userInfo: (error as NSError).userInfo)
}
await MainActor.run {status = .responseSent }
} catch { await setError(error) }
}


Expand Down

0 comments on commit b153a48

Please sign in to comment.