From 9bacb2e79b215e5f77a0ba7541ee8fc17d28544c Mon Sep 17 00:00:00 2001 From: Yasir Date: Tue, 12 Nov 2024 13:37:08 +0300 Subject: [PATCH] Refactor StatusList2021 to use String for credential index --- examples/1_advanced/8_status_list_2021.rs | 2 +- .../src/revocation/status_list_2021/credential.rs | 5 +++-- .../src/revocation/status_list_2021/entry.rs | 13 ++++++------- .../jwt_credential_validator_utils.rs | 2 +- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/examples/1_advanced/8_status_list_2021.rs b/examples/1_advanced/8_status_list_2021.rs index 0a70690e91..73da986343 100644 --- a/examples/1_advanced/8_status_list_2021.rs +++ b/examples/1_advanced/8_status_list_2021.rs @@ -100,7 +100,7 @@ async fn main() -> anyhow::Result<()> { // Create an unsigned `UniversityDegree` credential for Alice. // The issuer also chooses a unique `StatusList2021` index to be able to revoke it later. - let credential_index: usize = 420; + let credential_index: &str = "420"; let status: Status = StatusList2021Entry::new( status_list_credential.id().cloned().unwrap(), status_list_credential.purpose(), diff --git a/identity_credential/src/revocation/status_list_2021/credential.rs b/identity_credential/src/revocation/status_list_2021/credential.rs index ed96643cb7..9bd398b4c1 100644 --- a/identity_credential/src/revocation/status_list_2021/credential.rs +++ b/identity_credential/src/revocation/status_list_2021/credential.rs @@ -128,7 +128,7 @@ impl StatusList2021Credential { pub fn set_credential_status( &mut self, credential: &mut Credential, - index: usize, + index: &str, revoked_or_suspended: bool, ) -> Result { let id = self @@ -136,8 +136,9 @@ impl StatusList2021Credential { .cloned() .ok_or(StatusList2021CredentialError::Unreferenceable)?; let entry = StatusList2021Entry::new(id, self.purpose(), index, None); + let index_int = index.parse::().expect("should be integer serialized as string"); - self.set_entry(index, revoked_or_suspended)?; + self.set_entry(index_int, revoked_or_suspended)?; credential.credential_status = Some(entry.clone().into()); Ok(entry) diff --git a/identity_credential/src/revocation/status_list_2021/entry.rs b/identity_credential/src/revocation/status_list_2021/entry.rs index 7eecf2f28e..3cf98501a1 100644 --- a/identity_credential/src/revocation/status_list_2021/entry.rs +++ b/identity_credential/src/revocation/status_list_2021/entry.rs @@ -45,8 +45,7 @@ pub struct StatusList2021Entry { #[serde(rename = "type", deserialize_with = "deserialize_status_entry_type")] type_: String, status_purpose: StatusPurpose, - #[serde(deserialize_with = "serde_aux::prelude::deserialize_number_from_string")] - status_list_index: usize, + status_list_index: String, status_list_credential: Url, } @@ -67,7 +66,7 @@ impl From for Status { impl StatusList2021Entry { /// Creates a new [`StatusList2021Entry`]. - pub fn new(status_list: Url, purpose: StatusPurpose, index: usize, id: Option) -> Self { + pub fn new(status_list: Url, purpose: StatusPurpose, index: impl Into, id: Option) -> Self { let id = id.unwrap_or_else(|| { let mut id = status_list.clone(); id.set_fragment(None); @@ -79,7 +78,7 @@ impl StatusList2021Entry { type_: CREDENTIAL_STATUS_TYPE.to_owned(), status_purpose: purpose, status_list_credential: status_list, - status_list_index: index, + status_list_index: index.into(), } } @@ -94,8 +93,8 @@ impl StatusList2021Entry { } /// Returns the index of this entry. - pub const fn index(&self) -> usize { - self.status_list_index + pub fn index(&self) -> &str { + &self.status_list_index } /// Returns the referenced [`StatusList2021Credential`]'s [`Url`]. @@ -124,7 +123,7 @@ mod tests { let status = StatusList2021Entry::new( Url::parse("https://example.com/credentials/status/3").unwrap(), StatusPurpose::Revocation, - 94567, + "94567", Url::parse("https://example.com/credentials/status/3#94567").ok(), ); assert_eq!(status, deserialized); diff --git a/identity_credential/src/validator/jwt_credential_validation/jwt_credential_validator_utils.rs b/identity_credential/src/validator/jwt_credential_validation/jwt_credential_validator_utils.rs index d454122c15..c6d989af64 100644 --- a/identity_credential/src/validator/jwt_credential_validation/jwt_credential_validator_utils.rs +++ b/identity_credential/src/validator/jwt_credential_validation/jwt_credential_validator_utils.rs @@ -109,7 +109,7 @@ impl JwtCredentialValidatorUtils { && status.purpose() == status_list_credential.purpose() { let entry_status = status_list_credential - .entry(status.index()) + .entry(status.index().parse().expect("Failed to parse index")) .map_err(|e| JwtValidationError::InvalidStatus(crate::Error::InvalidStatus(e.to_string())))?; match entry_status { CredentialStatus::Revoked => Err(JwtValidationError::Revoked),