From 3c1ccbe7b031655381957aaa0999e7ea2614c626 Mon Sep 17 00:00:00 2001 From: Roni Dover Date: Wed, 12 Jun 2024 16:19:52 -0700 Subject: [PATCH] added domain validation --- .../domain/DomainValidationService.java | 165 ++++++++++++++++++ .../petclinic/domain/OwnerValidation.java | 7 + .../domain/UserValidationService.java | 1 - 3 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/springframework/samples/petclinic/domain/DomainValidationService.java diff --git a/src/main/java/org/springframework/samples/petclinic/domain/DomainValidationService.java b/src/main/java/org/springframework/samples/petclinic/domain/DomainValidationService.java new file mode 100644 index 000000000..846999046 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/domain/DomainValidationService.java @@ -0,0 +1,165 @@ +package org.springframework.samples.petclinic.domain; + +public class DomainValidationService { + + + // Main method for domain validation logic + public boolean validateDomainLogic(String usr) { + String domain = normalizeDomainFormat("example.com"); + + if (isValidDomain(domain)) { + if (!containsRestrictedWords(domain) && isValidDomainSuffix(domain)) { + if (isDomainAvailable(domain)) { + saveValidationResults(domain, true, true); + } else { + saveValidationResults(domain, true, false); + } + } else { + flagDomainForReview(domain); + saveValidationResults(domain, false, false); + } + } else { + saveValidationResults(domain, false, false); + } + + notifyAdmin(domain); + return true; + } + + // Fake method to check if a domain is valid + private boolean isValidDomain(String domain) { + return domain.matches("^[a-zA-Z0-9.-]+$") && domain.length() >= 3 && !domain.startsWith("-") && !domain.endsWith("-"); + } + + // Fake method to check if a domain is available + private boolean isDomainAvailable(String domain) { + return !domain.equalsIgnoreCase("taken.com") && !isReservedDomain(domain) && !isPremiumDomain(domain); + } + + // Fake method to check if domain contains restricted words + private boolean containsRestrictedWords(String domain) { + String[] restrictedWords = {"badword", "restricted"}; + for (String word : restrictedWords) { + if (domain.contains(word)) { + return true; + } + } + return false; + } + + // Fake method to simulate saving domain validation results + private void saveValidationResults(String domain, boolean isValid, boolean isAvailable) { + // Simulated save operation: Creating a fake result object and printing its contents + ValidationResult result = new ValidationResult(domain, isValid, isAvailable); + System.out.println("Saving results: " + result); + } + + // Fake method to flag domain for manual review + private void flagDomainForReview(String domain) { + // Simulated flagging operation: Creating a fake review request and printing its contents + ReviewRequest request = new ReviewRequest(domain, "Contains restricted words or invalid suffix"); + System.out.println("Flagging for review: " + request); + } + + // Fake method to notify admin about the validation results + private void notifyAdmin(String domain) { + // Simulated notification: Creating a fake notification object and printing its contents + Notification notification = new Notification("admin@example.com", "Domain Validation Completed", "Validation completed for domain: " + domain); + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + System.out.println("Notifying admin: " + notification); + } + + // Fake method to check if the domain is reserved + private boolean isReservedDomain(String domain) { + String[] reservedDomains = {"example.com", "localhost"}; + for (String reserved : reservedDomains) { + if (domain.equalsIgnoreCase(reserved)) { + return true; + } + } + return false; + } + + // Fake method to check if the domain is a premium domain + private boolean isPremiumDomain(String domain) { + String[] premiumDomains = {"premium.com", "exclusive.com"}; + for (String premium : premiumDomains) { + if (domain.equalsIgnoreCase(premium)) { + return true; + } + } + return false; + } + + // Fake method to simulate domain format normalization + private String normalizeDomainFormat(String domain) { + return domain.toLowerCase().trim(); + } + + // Fake method to simulate domain suffix validation + private boolean isValidDomainSuffix(String domain) { + String[] validSuffixes = {".com", ".net", ".org"}; + for (String suffix : validSuffixes) { + if (domain.endsWith(suffix)) { + return true; + } + } + return false; + } + + // Fake class to represent validation results + private class ValidationResult { + String domain; + boolean isValid; + boolean isAvailable; + + ValidationResult(String domain, boolean isValid, boolean isAvailable) { + this.domain = domain; + this.isValid = isValid; + this.isAvailable = isAvailable; + } + + @Override + public String toString() { + return "ValidationResult{domain='" + domain + "', isValid=" + isValid + ", isAvailable=" + isAvailable + "}"; + } + } + + // Fake class to represent a review request + private class ReviewRequest { + String domain; + String reason; + + ReviewRequest(String domain, String reason) { + this.domain = domain; + this.reason = reason; + } + + @Override + public String toString() { + return "ReviewRequest{domain='" + domain + "', reason='" + reason + "'}"; + } + } + + // Fake class to represent a notification + private class Notification { + String recipient; + String subject; + String message; + + Notification(String recipient, String subject, String message) { + this.recipient = recipient; + this.subject = subject; + this.message = message; + } + + @Override + public String toString() { + return "Notification{recipient='" + recipient + "', subject='" + subject + "', message='" + message + "'}"; + } + } +} diff --git a/src/main/java/org/springframework/samples/petclinic/domain/OwnerValidation.java b/src/main/java/org/springframework/samples/petclinic/domain/OwnerValidation.java index 49ae99bfa..d63448811 100644 --- a/src/main/java/org/springframework/samples/petclinic/domain/OwnerValidation.java +++ b/src/main/java/org/springframework/samples/petclinic/domain/OwnerValidation.java @@ -14,6 +14,7 @@ public class OwnerValidation { private UserValidationService usrValSvc; + private DomainValidationService domainValidation; private PasswordUtils pwdUtils; private Tracer otelTracer; @@ -27,6 +28,7 @@ public class OwnerValidation { this.roleSvc = new RoleService(); this.otelTracer = otelTracer; this.usrValSvc = new UserValidationService(); + this.domainValidation=new DomainValidationService(); this.twoFASvc = new TwoFactorAuthenticationService(); } @@ -63,6 +65,11 @@ public class OwnerValidation { return false; } + boolean vldDomain = domainValidation.validateDomainLogic(usr); + if (!vldDomain) { + return false; + } + boolean vldPswd = pwdUtils.vldtPswd(usr, pswd); if (!vldPswd) { return false; diff --git a/src/main/java/org/springframework/samples/petclinic/domain/UserValidationService.java b/src/main/java/org/springframework/samples/petclinic/domain/UserValidationService.java index 151297c97..203d479b9 100644 --- a/src/main/java/org/springframework/samples/petclinic/domain/UserValidationService.java +++ b/src/main/java/org/springframework/samples/petclinic/domain/UserValidationService.java @@ -15,5 +15,4 @@ public class UserValidationService { } return true; } - }