Skip to content

Commit

Permalink
Refactor StatusList2021 to use String for credential index
Browse files Browse the repository at this point in the history
  • Loading branch information
itsyaasir committed Nov 12, 2024
1 parent 6f83696 commit 9bacb2e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion examples/1_advanced/8_status_list_2021.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,17 @@ impl StatusList2021Credential {
pub fn set_credential_status(
&mut self,
credential: &mut Credential,
index: usize,
index: &str,
revoked_or_suspended: bool,
) -> Result<StatusList2021Entry, StatusList2021CredentialError> {
let id = self
.id()
.cloned()
.ok_or(StatusList2021CredentialError::Unreferenceable)?;
let entry = StatusList2021Entry::new(id, self.purpose(), index, None);
let index_int = index.parse::<usize>().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)
Expand Down
13 changes: 6 additions & 7 deletions identity_credential/src/revocation/status_list_2021/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand All @@ -67,7 +66,7 @@ impl From<StatusList2021Entry> for Status {

impl StatusList2021Entry {
/// Creates a new [`StatusList2021Entry`].
pub fn new(status_list: Url, purpose: StatusPurpose, index: usize, id: Option<Url>) -> Self {
pub fn new(status_list: Url, purpose: StatusPurpose, index: impl Into<String>, id: Option<Url>) -> Self {
let id = id.unwrap_or_else(|| {
let mut id = status_list.clone();
id.set_fragment(None);
Expand All @@ -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(),
}
}

Expand All @@ -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`].
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 9bacb2e

Please sign in to comment.