Skip to content

Commit

Permalink
Support %-encoded characters in DID method id (#1303)
Browse files Browse the repository at this point in the history
  • Loading branch information
UMR1352 committed Mar 27, 2024
1 parent e68538f commit 0352b84
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 24 deletions.
2 changes: 1 addition & 1 deletion identity_did/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repository.workspace = true
description = "Agnostic implementation of the Decentralized Identifiers (DID) standard."

[dependencies]
did_url = { version = "0.1", default-features = false, features = ["std", "serde"] }
did_url_parser = { version = "0.2.0", features = ["std", "serde"] }
form_urlencoded = { version = "1.2.0", default-features = false, features = ["alloc"] }
identity_core = { version = "=1.1.1", path = "../identity_core" }
serde.workspace = true
Expand Down
31 changes: 19 additions & 12 deletions identity_did/src/did.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use core::fmt::Formatter;
use core::str::FromStr;
use std::hash::Hash;

use did_url::DID as BaseDIDUrl;
use did_url_parser::DID as BaseDIDUrl;

use identity_core::common::KeyComparable;

Expand Down Expand Up @@ -111,14 +111,7 @@ impl CoreDID {
///
/// Returns `Err` if the input is not a valid [`DID`].
pub fn parse(input: impl AsRef<str>) -> Result<Self, Error> {
let base_did_url: BaseDIDUrl = BaseDIDUrl::parse(input).map_err(Error::from)?;
Self::try_from_base_did(base_did_url)
}

/// Try convert a [`BaseDIDUrl`] into a [`CoreDID`].
fn try_from_base_did(base_did_url: BaseDIDUrl) -> Result<Self, Error> {
Self::check_validity(&base_did_url)?;
Ok(Self(base_did_url))
BaseDIDUrl::parse(input).map(Self).map_err(Error::from)
}

/// Set the method name of the [`DID`].
Expand All @@ -145,9 +138,23 @@ impl CoreDID {

/// Validates whether a string is a valid [`DID`] method-id.
pub fn valid_method_id(value: &str) -> Result<(), Error> {
if !value.chars().all(is_char_method_id) {
return Err(Error::InvalidMethodId);
// if !value.chars().all(is_char_method_id) {
// return Err(Error::InvalidMethodId);
// }
let mut chars = value.chars();
while let Some(c) = chars.next() {
match c {
'%' => {
let digits = chars.clone().take(2).collect::<String>();
u8::from_str_radix(&digits, 16).map_err(|_| Error::InvalidMethodId)?;
chars.next();
chars.next();
}
c if is_char_method_id(c) => (),
_ => return Err(Error::InvalidMethodId),
}
}

Ok(())
}

Expand Down Expand Up @@ -185,7 +192,7 @@ impl TryFrom<BaseDIDUrl> for CoreDID {
type Error = Error;

fn try_from(base_did_url: BaseDIDUrl) -> Result<Self, Self::Error> {
Self::try_from_base_did(base_did_url)
Ok(Self(base_did_url))
}
}

Expand Down
2 changes: 1 addition & 1 deletion identity_did/src/did_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::cmp::Ordering;
use std::hash::Hash;
use std::hash::Hasher;

use did_url::DID as BaseDIDUrl;
use did_url_parser::DID as BaseDIDUrl;

use identity_core::common::KeyComparable;
use identity_core::common::Url;
Expand Down
16 changes: 8 additions & 8 deletions identity_did/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ pub enum Error {
Other(&'static str),
}

impl From<did_url::Error> for Error {
fn from(error: did_url::Error) -> Self {
impl From<did_url_parser::Error> for Error {
fn from(error: did_url_parser::Error) -> Self {
match error {
did_url::Error::InvalidFragment => Self::InvalidFragment,
did_url::Error::InvalidMethodId => Self::InvalidMethodId,
did_url::Error::InvalidMethodName => Self::InvalidMethodName,
did_url::Error::InvalidPath => Self::InvalidPath,
did_url::Error::InvalidQuery => Self::InvalidQuery,
did_url::Error::InvalidScheme => Self::InvalidScheme,
did_url_parser::Error::InvalidFragment => Self::InvalidFragment,
did_url_parser::Error::InvalidMethodId => Self::InvalidMethodId,
did_url_parser::Error::InvalidMethodName => Self::InvalidMethodName,
did_url_parser::Error::InvalidPath => Self::InvalidPath,
did_url_parser::Error::InvalidQuery => Self::InvalidQuery,
did_url_parser::Error::InvalidScheme => Self::InvalidScheme,
error => Self::Other(error.as_str()),
}
}
Expand Down
2 changes: 1 addition & 1 deletion identity_did/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ mod error;

pub use crate::did_url::DIDUrl;
pub use crate::did_url::RelativeDIDUrl;
pub use ::did_url::DID as BaseDIDUrl;
pub use ::did_url_parser::DID as BaseDIDUrl;
pub use did::CoreDID;
pub use did::DID;
pub use error::Error;
2 changes: 1 addition & 1 deletion identity_document/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ rust-version.workspace = true
description = "Method-agnostic implementation of the Decentralized Identifiers (DID) standard."

[dependencies]
did_url = { version = "0.1", default-features = false, features = ["std", "serde"] }
did_url_parser = { version = "0.2.0", features = ["std", "serde"] }
identity_core = { version = "=1.1.1", path = "../identity_core" }
identity_did = { version = "=1.1.1", path = "../identity_did" }
identity_verification = { version = "=1.1.1", path = "../identity_verification", default-features = false }
Expand Down

0 comments on commit 0352b84

Please sign in to comment.