Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 3dd8e73

Browse files
authored
Add information to MDL reader handle_response method (#112)
This updates the MDL reader handle_response method to return the device and issuer authentication status check instead of triggering an error and finishing the flow.
1 parent 3f9af34 commit 3dd8e73

File tree

2 files changed

+128
-16
lines changed

2 files changed

+128
-16
lines changed

MobileSdkRs/Sources/MobileSdkRs/mobile_sdk_rs.swift

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7420,15 +7420,39 @@ public struct MdlReaderResponseData {
74207420
* Contains the namespaces for the mDL directly, without top-level doc types
74217421
*/
74227422
public var verifiedResponse: [String: [String: MDocItem]]
7423+
/**
7424+
* Outcome of issuer authentication.
7425+
*/
7426+
public var issuerAuthentication: AuthenticationStatus
7427+
/**
7428+
* Outcome of device authentication.
7429+
*/
7430+
public var deviceAuthentication: AuthenticationStatus
7431+
/**
7432+
* Errors that occurred during response processing.
7433+
*/
7434+
public var errors: String?
74237435

74247436
// Default memberwise initializers are never public by default, so we
74257437
// declare one manually.
74267438
public init(state: MdlSessionManager,
74277439
/**
74287440
* Contains the namespaces for the mDL directly, without top-level doc types
7429-
*/verifiedResponse: [String: [String: MDocItem]]) {
7441+
*/verifiedResponse: [String: [String: MDocItem]],
7442+
/**
7443+
* Outcome of issuer authentication.
7444+
*/issuerAuthentication: AuthenticationStatus,
7445+
/**
7446+
* Outcome of device authentication.
7447+
*/deviceAuthentication: AuthenticationStatus,
7448+
/**
7449+
* Errors that occurred during response processing.
7450+
*/errors: String?) {
74307451
self.state = state
74317452
self.verifiedResponse = verifiedResponse
7453+
self.issuerAuthentication = issuerAuthentication
7454+
self.deviceAuthentication = deviceAuthentication
7455+
self.errors = errors
74327456
}
74337457
}
74347458

@@ -7439,13 +7463,19 @@ public struct FfiConverterTypeMDLReaderResponseData: FfiConverterRustBuffer {
74397463
return
74407464
try MdlReaderResponseData(
74417465
state: FfiConverterTypeMDLSessionManager.read(from: &buf),
7442-
verifiedResponse: FfiConverterDictionaryStringDictionaryStringTypeMDocItem.read(from: &buf)
7466+
verifiedResponse: FfiConverterDictionaryStringDictionaryStringTypeMDocItem.read(from: &buf),
7467+
issuerAuthentication: FfiConverterTypeAuthenticationStatus.read(from: &buf),
7468+
deviceAuthentication: FfiConverterTypeAuthenticationStatus.read(from: &buf),
7469+
errors: FfiConverterOptionString.read(from: &buf)
74437470
)
74447471
}
74457472

74467473
public static func write(_ value: MdlReaderResponseData, into buf: inout [UInt8]) {
74477474
FfiConverterTypeMDLSessionManager.write(value.state, into: &buf)
74487475
FfiConverterDictionaryStringDictionaryStringTypeMDocItem.write(value.verifiedResponse, into: &buf)
7476+
FfiConverterTypeAuthenticationStatus.write(value.issuerAuthentication, into: &buf)
7477+
FfiConverterTypeAuthenticationStatus.write(value.deviceAuthentication, into: &buf)
7478+
FfiConverterOptionString.write(value.errors, into: &buf)
74497479
}
74507480
}
74517481

@@ -7720,6 +7750,68 @@ public func FfiConverterTypeStatusMessage_lower(_ value: StatusMessage) -> RustB
77207750
return FfiConverterTypeStatusMessage.lower(value)
77217751
}
77227752

7753+
// Note that we don't yet support `indirect` for enums.
7754+
// See https://github.com/mozilla/uniffi-rs/issues/396 for further discussion.
7755+
7756+
public enum AuthenticationStatus {
7757+
7758+
case valid
7759+
case invalid
7760+
case unchecked
7761+
}
7762+
7763+
7764+
public struct FfiConverterTypeAuthenticationStatus: FfiConverterRustBuffer {
7765+
typealias SwiftType = AuthenticationStatus
7766+
7767+
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> AuthenticationStatus {
7768+
let variant: Int32 = try readInt(&buf)
7769+
switch variant {
7770+
7771+
case 1: return .valid
7772+
7773+
case 2: return .invalid
7774+
7775+
case 3: return .unchecked
7776+
7777+
default: throw UniffiInternalError.unexpectedEnumCase
7778+
}
7779+
}
7780+
7781+
public static func write(_ value: AuthenticationStatus, into buf: inout [UInt8]) {
7782+
switch value {
7783+
7784+
7785+
case .valid:
7786+
writeInt(&buf, Int32(1))
7787+
7788+
7789+
case .invalid:
7790+
writeInt(&buf, Int32(2))
7791+
7792+
7793+
case .unchecked:
7794+
writeInt(&buf, Int32(3))
7795+
7796+
}
7797+
}
7798+
}
7799+
7800+
7801+
public func FfiConverterTypeAuthenticationStatus_lift(_ buf: RustBuffer) throws -> AuthenticationStatus {
7802+
return try FfiConverterTypeAuthenticationStatus.lift(buf)
7803+
}
7804+
7805+
public func FfiConverterTypeAuthenticationStatus_lower(_ value: AuthenticationStatus) -> RustBuffer {
7806+
return FfiConverterTypeAuthenticationStatus.lower(value)
7807+
}
7808+
7809+
7810+
7811+
extension AuthenticationStatus: Equatable, Hashable {}
7812+
7813+
7814+
77237815
// Note that we don't yet support `indirect` for enums.
77247816
// See https://github.com/mozilla/uniffi-rs/issues/396 for further discussion.
77257817
/**

src/mdl/reader.rs

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use isomdl::{
1212
trust_anchor::{PemTrustAnchor, TrustAnchorRegistry},
1313
},
1414
},
15-
presentation::{authentication::AuthenticationStatus, reader},
15+
presentation::{authentication::AuthenticationStatus as IsoMdlAuthenticationStatus, reader},
1616
};
1717
use uuid::Uuid;
1818

@@ -151,11 +151,34 @@ impl From<serde_json::Value> for MDocItem {
151151
}
152152
}
153153
}
154+
155+
#[derive(Debug, Clone, PartialEq, uniffi::Enum)]
156+
pub enum AuthenticationStatus {
157+
Valid,
158+
Invalid,
159+
Unchecked,
160+
}
161+
162+
impl From<IsoMdlAuthenticationStatus> for AuthenticationStatus {
163+
fn from(internal: IsoMdlAuthenticationStatus) -> Self {
164+
match internal {
165+
IsoMdlAuthenticationStatus::Valid => AuthenticationStatus::Valid,
166+
IsoMdlAuthenticationStatus::Invalid => AuthenticationStatus::Invalid,
167+
IsoMdlAuthenticationStatus::Unchecked => AuthenticationStatus::Unchecked,
168+
}
169+
}
170+
}
154171
#[derive(uniffi::Record, Debug)]
155172
pub struct MDLReaderResponseData {
156173
state: Arc<MDLSessionManager>,
157174
/// Contains the namespaces for the mDL directly, without top-level doc types
158175
verified_response: HashMap<String, HashMap<String, MDocItem>>,
176+
/// Outcome of issuer authentication.
177+
pub issuer_authentication: AuthenticationStatus,
178+
/// Outcome of device authentication.
179+
pub device_authentication: AuthenticationStatus,
180+
/// Errors that occurred during response processing.
181+
pub errors: Option<String>,
159182
}
160183

161184
#[uniffi::export]
@@ -165,23 +188,17 @@ pub fn handle_response(
165188
) -> Result<MDLReaderResponseData, MDLReaderResponseError> {
166189
let mut state = state.0.clone();
167190
let validated_response = state.handle_response(&response);
168-
if !validated_response.errors.is_empty() {
169-
return Err(MDLReaderResponseError::Generic {
170-
value: serde_json::to_string(&validated_response.errors).map_err(|e| {
191+
let errors = if !validated_response.errors.is_empty() {
192+
Some(
193+
serde_json::to_string(&validated_response.errors).map_err(|e| {
171194
MDLReaderResponseError::Generic {
172195
value: format!("Could not serialze errors: {e:?}"),
173196
}
174197
})?,
175-
});
176-
}
177-
// These checks should be unreachable as they always have a corresponding entry in `errors`
178-
if let AuthenticationStatus::Invalid = validated_response.issuer_authentication {
179-
return Err(MDLReaderResponseError::InvalidIssuerAuthentication);
180-
}
181-
if let AuthenticationStatus::Invalid = validated_response.device_authentication {
182-
return Err(MDLReaderResponseError::InvalidDeviceAuthentication);
183-
}
184-
// We get the namespaces directly without the top-level doc types
198+
)
199+
} else {
200+
None
201+
};
185202
let verified_response: Result<_, _> = validated_response
186203
.response
187204
.into_iter()
@@ -205,5 +222,8 @@ pub fn handle_response(
205222
Ok(MDLReaderResponseData {
206223
state: Arc::new(MDLSessionManager(state)),
207224
verified_response,
225+
issuer_authentication: AuthenticationStatus::from(validated_response.issuer_authentication),
226+
device_authentication: AuthenticationStatus::from(validated_response.device_authentication),
227+
errors,
208228
})
209229
}

0 commit comments

Comments
 (0)