Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#313: Trigger the rdap lookup on load #365

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions _data/assetPaths.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"admin.js": "/assets/js/admin-77FHK54G.js",
"admin.map": "/assets/js/admin-77FHK54G.js.map",
"app.js": "/assets/js/app-XWR645AF.js",
"app.map": "/assets/js/app-XWR645AF.js.map",
"styles.css": "/assets/styles/styles-MYAQ7E3B.css",
"styles.map": "/assets/styles/styles-MYAQ7E3B.css.map"
"app.js": "/assets/js/app-T4AK7MTZ.js",
"app.map": "/assets/js/app-T4AK7MTZ.js.map",
"styles.css": "/assets/styles/styles-M7MBCPQG.css",
"styles.map": "/assets/styles/styles-M7MBCPQG.css.map"
}
90 changes: 65 additions & 25 deletions _includes/whois-search.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,60 @@
<script>
document.addEventListener("DOMContentLoaded", function () {
// Autofill domain from URL if present
let domain = getQueryParam("domain");
if (domain) {
const inputField = document.getElementById("whois-lookup-input");
const submitButton = document.querySelector(".usa-search--domain__submit");

if (inputField && submitButton) {
const validation = validateDomain(domain, true);

if (validation.isValid) {
inputField.value = validation.domain;
submitButton.click();
} else {
console.warn("Invalid domain:", validation.errorMessage);
}
}
}
});

// Function to get query parameters from URL
function getQueryParam(param) {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get(param);
}

// Function to validate domain name
function validateDomain(domain, mustIncludeTopLevelDomain=false) {
if (!domain) return { isValid: false, errorMessage: "Domain is required" };

// Number of parts to domain separated by .
const num_domain_parts = domain.split('.').length

// Domain has more than one part above the TLD which RDAP does not support
if (num_domain_parts > 2) {
return { isValid: false, errorMessage: "Domain name must have exactly one part above the TLD." };
} else if (num_domain_parts == 2 && !domain.endsWith('.gov')) {
return { isValid: false, errorMessage: "You can only search domain names that end in .gov" };
}

// On page load, we want this extra check. On form interaction however, we want to
// give the user the option to enter or omit the TLD
if (mustIncludeTopLevelDomain && !domain.endsWith(".gov")) {
return { isValid: false, errorMessage: "You can only search domain names that end in .gov" };
}

// A domain name is alphanumeric or hyphen, up to 63 characters, doesn't
// begin or end with a hyphen, followed by a TLD of 2-6 alphabetic characters
const DOMAIN_REGEX = /^(?!-)[A-Za-z0-9-]{1,63}(?<!-)(\.gov)?$/;
if (!DOMAIN_REGEX.test(domain)) {
return { isValid: false, errorMessage: "Enter a domain using only letters, numbers, or hyphens." };
}

return { isValid: true, domain };
}

// RDAP data that is excluded in search results
const excludedFields = {
"contact": ["version"],
Expand Down Expand Up @@ -130,44 +186,28 @@ <h4>${contact.charAt(0).toUpperCase() + contact.slice(1)}</h4>

const rdapBaseUrl = 'https://manage.get.gov/api/v1/rdap/?domain=';

let domain = document.getElementById(searchId).value
let domain = document.getElementById(searchId).value.trim();

if (domain === null || domain.length === 0) {
render_result(searchResultsId, ``);
return;
}

/* Validate domain name is valid.
A domain name is alphanumeric or hyphen, up to 63 characters, doesn't
begin or end with a hyphen, followed by a TLD of 2-6 alphabetic characters */
DOMAIN_REGEX = /^(?!-)[A-Za-z0-9-]{1,63}(?<!-)(\.gov)?$/;

if (!DOMAIN_REGEX.test(domain)) {
let errorMessageText = "Enter a domain using only letters, numbers, or hyphens."
// Number of parts to domain separated by .
const num_domain_parts = domain.split('.').length

// Domain has more than one part above the TLD which RDAP does not support
if (num_domain_parts > 2) {
errorMessageText = "Domain name must have exactly one part above the TLD."
}
else if (num_domain_parts == 2 && !domain.endsWith('.gov')) {
errorMessageText = "You can only search domain names that end in .gov"
}

let errorMessage = `
// Validate domain using the new function
const validation = validateDomain(domain);
if (!validation.isValid) {
render_result(searchResultsId, `
<div class="usa-alert usa-alert--error usa-alert--slim" role="alert">
<div class="usa-alert__body display-flex flex-column flex-align-center">
<p class="usa-alert__text">
${errorMessageText}
</p>
<p class="usa-alert__text">${validation.errorMessage}</p>
</div>
</div>
`
render_result(searchResultsId, errorMessage);
`);
return;
}

domain = validation.domain;

if (!domain.endsWith('.gov')) {
domain = `${domain}.gov`;
}
Expand Down
Loading