Skip to content

Commit

Permalink
Fix normalization logic
Browse files Browse the repository at this point in the history
  • Loading branch information
KendallWeihe committed Jul 16, 2024
1 parent 86cbc0d commit 906f251
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions crates/web5_cli/src/dids/create.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -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("/", ":");
Expand Down

0 comments on commit 906f251

Please sign in to comment.