Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 22 additions & 5 deletions kinode/src/http/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -1194,10 +1194,13 @@ <h3>Logging in...</h3>
}

const firstPathItem = window.location.pathname.split('/')[1];
if (firstPathItem === '') {
document.getElementById("node-and-domain").innerText = "${node} ";
} else {
const expectedSecureSubdomain = generateSecureSubdomain(firstPathItem);
const maybeSecureSubdomain = window.location.host.split('.')[0];
const isSecureSubdomain = expectedSecureSubdomain === maybeSecureSubdomain;
if (isSecureSubdomain) {
document.getElementById("node-and-domain").innerText = "${node}: authenticate for secure subdomain app " + firstPathItem;
} else {
document.getElementById("node-and-domain").innerText = "${node} ";
}

async function login(password) {
Expand All @@ -1210,7 +1213,10 @@ <h3>Logging in...</h3>
const result = await fetch("/login", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ password_hash: hashHex, subdomain: firstPathItem }),
body: JSON.stringify({
password_hash: hashHex,
subdomain: isSecureSubdomain ? firstPathItem : '',
}),
});

if (result.status == 200) {
Expand All @@ -1226,6 +1232,17 @@ <h3>Logging in...</h3>
}
}

function generateSecureSubdomain(processString) {
const parts = processString.split(':');
const package = parts[1];
const publisher = parts[2];
const subdomain = [package, publisher].join("-")
.split("")
.map(c => c.match(/[a-zA-Z0-9]/) ? c : '-')
.join("");
return subdomain;
}

document.addEventListener("DOMContentLoaded", () => {
const form = document.getElementById("signup-form");
form.addEventListener("submit", (e) => {
Expand All @@ -1238,4 +1255,4 @@ <h3>Logging in...</h3>
</script>
</body>

</html>
</html>
39 changes: 16 additions & 23 deletions kinode/src/http/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,19 +546,30 @@ async fn http_handler(
.into_response());
}
if request_subdomain != subdomain {
let query_string = if !query_params.is_empty() {
let params: Vec<String> = query_params
.iter()
.map(|(key, value)| format!("{}={}", key, value))
.collect();
format!("?{}", params.join("&"))
} else {
String::new()
};

return Ok(warp::http::Response::builder()
.status(StatusCode::TEMPORARY_REDIRECT)
.header(
"Location",
format!(
"{}://{}.{}{}",
"{}://{}.{}{}{}",
match headers.get("X-Forwarded-Proto") {
Some(proto) => proto.to_str().unwrap_or("http"),
None => "http",
},
subdomain,
host,
original_path,
query_string,
),
)
.body(vec![])
Expand All @@ -584,28 +595,10 @@ async fn http_handler(
&jwt_secret_bytes,
) {
// redirect to login page so they can get an auth token
if original_path == "" {
return Ok(warp::http::Response::builder()
.status(StatusCode::OK)
.body(login_html.to_string())
.into_response());
} else {
return Ok(warp::http::Response::builder()
.status(StatusCode::TEMPORARY_REDIRECT)
.header(
"Location",
format!(
"{}://{}",
match headers.get("X-Forwarded-Proto") {
Some(proto) => proto.to_str().unwrap_or("http"),
None => "http",
},
host,
),
)
.body(vec![])
.into_response());
}
return Ok(warp::http::Response::builder()
.status(StatusCode::OK)
.body(login_html.to_string())
.into_response());
}
}
}
Expand Down