From ce193fcff3c749cb7ab3208e806f6a6ab009f5dc Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Fri, 3 Jan 2025 12:12:59 +0530 Subject: [PATCH 1/3] check popular domain --- lib/Service/BlackListService.php | 62 ++++++++++++++++++++++++++++ lib/Service/RecoveryEmailService.php | 4 ++ 2 files changed, 66 insertions(+) diff --git a/lib/Service/BlackListService.php b/lib/Service/BlackListService.php index 020c58f..f9a4a43 100644 --- a/lib/Service/BlackListService.php +++ b/lib/Service/BlackListService.php @@ -17,6 +17,7 @@ class BlackListService { private $appName; private const BLACKLISTED_DOMAINS_FILE_NAME = 'blacklisted_domains.json'; private const BLACKLISTED_DOMAINS_URL = 'https://raw.githubusercontent.com/disposable/disposable-email-domains/master/domains.json'; + private const POPULAR_DOMAINS_FILE_NAME = 'domains.json'; public function __construct(string $appName, ILogger $logger, IFactory $l10nFactory, IAppData $appData) { @@ -43,6 +44,7 @@ class BlackListService { $emailDomain = strtolower(end($emailParts)); return in_array($emailDomain, $blacklistedDomains); } + /** * Update the blacklisted domains data by fetching it from a URL and saving it locally. * @@ -120,4 +122,64 @@ class BlackListService { } return true; } + + /** + * Check if an domain is popular domain a JSON list of popular domains. + * + * @param string $email The email address to check. + * @return bool True if the email domain is popular, false otherwise. + */ + public function isPopularDomain(string $email): bool { + if (!$this->ensureDocumentsFolder()) { + return false; + } + $popularlistedDomains = $this->getPopularlistedDomainData(); + if (empty($popularlistedDomains)) { + return false; + } + $emailParts = explode('@', $email); + $emailDomain = strtolower(end($emailParts)); + return in_array($emailDomain, $popularlistedDomains); + } + + /** + * Retrieve the Popular domain file path + * + * @return ISimpleFile + */ + private function getPopularDomainsFile(): ISimpleFile { + try { + $currentFolder = $this->appData->getFolder('/'); + } catch (NotFoundException $e) { + $currentFolder = $this->appData->newFolder('/'); + } + $filename = self::POPULAR_DOMAINS_FILE_NAME; + if ($currentFolder->fileExists($filename)) { + return $currentFolder->getFile($filename); + } + return $currentFolder->newFile($filename); + } + + /** + * Retrieve the Popular domain data. + * + * @return array The array of popular domains. + */ + public function getPopularlistedDomainData(): array { + $document = self::POPULAR_DOMAINS_FILE_NAME; + $file = $this->getPopularDomainsFile(); + try { + $popularlistedDomainsInJson = $file->getContent(); + if (empty($popularlistedDomainsInJson)) { + return []; + } + return json_decode($popularlistedDomainsInJson, true, 512, JSON_THROW_ON_ERROR); + } catch (NotFoundException $e) { + $this->logger->warning('Popular domains file ' . $document . ' not found!'); + return []; + } catch (\Throwable $e) { + $this->logger->warning('Error decoding Popular domains file ' . $document . ': ' . $e->getMessage()); + return []; + } + } } diff --git a/lib/Service/RecoveryEmailService.php b/lib/Service/RecoveryEmailService.php index 0fe1844..c3c35d4 100644 --- a/lib/Service/RecoveryEmailService.php +++ b/lib/Service/RecoveryEmailService.php @@ -135,6 +135,10 @@ class RecoveryEmailService { $this->logger->info("User ID $username's requested recovery email address domain is blacklisted. Please provide another recovery address."); throw new BlacklistedEmailException($l->t('The domain of this email address is blacklisted. Please provide another recovery address.')); } + if ($this->blackListService->isNotPopularDomain($recoveryEmail)) { + $this->logger->info("User ID $username's requested recovery email address domain is blacklisted. Please provide another recovery address."); + } + } return true; } -- GitLab From ba0979ff30e717768b6f557d8f60f1a7ff58016c Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Fri, 3 Jan 2025 12:23:33 +0530 Subject: [PATCH 2/3] lint fix --- lib/Service/RecoveryEmailService.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/Service/RecoveryEmailService.php b/lib/Service/RecoveryEmailService.php index c3c35d4..7731ea9 100644 --- a/lib/Service/RecoveryEmailService.php +++ b/lib/Service/RecoveryEmailService.php @@ -136,9 +136,8 @@ class RecoveryEmailService { throw new BlacklistedEmailException($l->t('The domain of this email address is blacklisted. Please provide another recovery address.')); } if ($this->blackListService->isNotPopularDomain($recoveryEmail)) { - $this->logger->info("User ID $username's requested recovery email address domain is blacklisted. Please provide another recovery address."); - } - + $this->logger->info("User ID $username's requested recovery email address domain is popular domain"); + } } return true; } -- GitLab From 27b13fd49ce86efa3abfe70de101df2876e8ebf5 Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Fri, 3 Jan 2025 13:26:11 +0530 Subject: [PATCH 3/3] check pipeline --- lib/Service/RecoveryEmailService.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/Service/RecoveryEmailService.php b/lib/Service/RecoveryEmailService.php index 7731ea9..ded7ae2 100644 --- a/lib/Service/RecoveryEmailService.php +++ b/lib/Service/RecoveryEmailService.php @@ -138,6 +138,7 @@ class RecoveryEmailService { if ($this->blackListService->isNotPopularDomain($recoveryEmail)) { $this->logger->info("User ID $username's requested recovery email address domain is popular domain"); } + echo "test"; } return true; } -- GitLab