diff --git a/crates/web5_cli/src/dids/create.rs b/crates/web5_cli/src/dids/create.rs index efe03608..b46c7352 100644 --- a/crates/web5_cli/src/dids/create.rs +++ b/crates/web5_cli/src/dids/create.rs @@ -1,6 +1,6 @@ use clap::Subcommand; -use url::Url; use std::sync::Arc; +use url::Url; use web5::{ crypto::dsa::ed25519::{Ed25519Generator, Ed25519Signer}, dids::{ @@ -79,37 +79,41 @@ impl Commands { let valid_url = if domain.starts_with("http://") || domain.starts_with("https://") { let url = Url::parse(domain).expect("Invalid URL"); - + // Ensure "http://" is only allowed for localhost or 127.0.0.1 - if url.scheme() == "http" && !(url.host_str() == Some("localhost") || url.host_str() == Some("127.0.0.1")) { + if url.scheme() == "http" + && !(url.host_str() == Some("localhost") + || url.host_str() == Some("127.0.0.1")) + { panic!("Only https is allowed except for localhost or 127.0.0.1 with http"); } - + // Get the trimmed URL string without the scheme let trimmed_url = url[url::Position::BeforeHost..].to_string(); - + // Remove the scheme let normalized = if trimmed_url.starts_with("//") { &trimmed_url[2..] } else { &trimmed_url }; - + normalized.to_string() } else { Url::parse(&format!("https://{}", domain)).expect("Invalid URL"); domain.clone() }; - - let normalized = if valid_url.ends_with("/did.json") { - valid_url.trim_end_matches("/did.json").to_string() - } else if valid_url.ends_with("/.well-known") { - valid_url.trim_end_matches("/.well-known").to_string() - } else if valid_url.ends_with("/") { - valid_url.trim_end_matches("/").to_string() - } else { - valid_url.clone() - }; + + let mut normalized = valid_url.clone(); + if normalized.ends_with("/") { + normalized = normalized.trim_end_matches("/").to_string() + } + if normalized.ends_with("/did.json") { + normalized = normalized.trim_end_matches("/did.json").to_string() + } + if normalized.ends_with("/.well-known") { + normalized = normalized.trim_end_matches("/.well-known").to_string() + } let encoded_domain = normalized.replace(":", "%3A"); let encoded_domain = encoded_domain.replace("/", ":");