From f74c96cddfc4ba5b9d31e571c532c3536590f83a Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Wed, 19 Jun 2024 13:56:08 +0530 Subject: [PATCH 1/8] Checking blacklisted Domains --- lib/Service/RecoveryEmailService.php | 39 ++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/lib/Service/RecoveryEmailService.php b/lib/Service/RecoveryEmailService.php index 96a27b3..c70f8b0 100644 --- a/lib/Service/RecoveryEmailService.php +++ b/lib/Service/RecoveryEmailService.php @@ -22,6 +22,8 @@ use OCP\Mail\IEMailTemplate; use OCP\Mail\IMailer; use OCP\Security\VerificationToken\IVerificationToken; use OCP\Util; +use OCP\Files\IAppData; +use OCP\Files\NotFoundException; class RecoveryEmailService { private ILogger $logger; @@ -37,8 +39,11 @@ class RecoveryEmailService { private CurlService $curl; private array $apiConfig; protected const TOKEN_LIFETIME = 60 * 30; // 30 minutes + private IAppData $appData; + private const BLACKLISTED_DOMAINS_FOLDER_NAME = 'ecloud-accounts'; + private const BLACKLISTED_DOMAINS_FILE_NAME = 'blacklisted_domains.json'; - public function __construct(string $appName, ILogger $logger, IConfig $config, LDAPConnectionService $LDAPConnectionService, IUserManager $userManager, IMailer $mailer, IFactory $l10nFactory, IURLGenerator $urlGenerator, Defaults $themingDefaults, IVerificationToken $verificationToken, CurlService $curlService) { + public function __construct(string $appName, ILogger $logger, IConfig $config, LDAPConnectionService $LDAPConnectionService, IUserManager $userManager, IMailer $mailer, IFactory $l10nFactory, IURLGenerator $urlGenerator, Defaults $themingDefaults, IVerificationToken $verificationToken, CurlService $curlService, IAppData $appData) { $this->logger = $logger; $this->config = $config; $this->appName = $appName; @@ -50,6 +55,7 @@ class RecoveryEmailService { $this->themingDefaults = $themingDefaults; $this->verificationToken = $verificationToken; $this->curl = $curlService; + $this->appData = $appData; $commonServiceURL = $this->config->getSystemValue('common_services_url', ''); if (!empty($commonServiceURL)) { @@ -236,7 +242,10 @@ class RecoveryEmailService { */ public function isBlacklistedEmail(string $email): bool { // Get the blacklisted domains from configuration - $blacklistedDomainsInJson = $this->config->getAppValue('ecloud-accounts', 'blacklisted_domains'); + if (!$this->ensureDocumentsFolder()) { + return false; + } + $blacklistedDomainsInJson = $this->getBlacklistedDomainData(); if (empty($blacklistedDomainsInJson)) { return false; } @@ -254,6 +263,32 @@ class RecoveryEmailService { // Check if the email domain is in the blacklisted domains array return in_array($emailDomain, $blacklistedDomains); } + /** + * Retrieve the blacklisted domain data. + * + */ + public function getBlacklistedDomainData() { + $foldername = self::BLACKLISTED_DOMAINS_FOLDER_NAME; + $document = self::BLACKLISTED_DOMAINS_FILE_NAME; + return $this->appData->getFolder($foldername)->getFile((string) $document)->getContent(); + } + /** + * Ensure the specified folder exists within AppData. + * + */ + private function ensureDocumentsFolder(): bool { + $foldername = self::BLACKLISTED_DOMAINS_FOLDER_NAME; + try { + $this->appData->getFolder($foldername); + } catch (NotFoundException $e) { + $this->logger->logException('Blacklisted domains file not found!'); + return false; + } catch (\RuntimeException $e) { + $this->logger->logException($e); + return false; + } + return true; + } private function manageEmailRestriction(string $email, string $method, string $url) : void { $params = []; -- GitLab From 7accbcf0248f224210ea5288f7a0b4c453f1412e Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Wed, 19 Jun 2024 16:23:40 +0530 Subject: [PATCH 2/8] added factory --- lib/Service/RecoveryEmailService.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/Service/RecoveryEmailService.php b/lib/Service/RecoveryEmailService.php index c70f8b0..6c2e532 100644 --- a/lib/Service/RecoveryEmailService.php +++ b/lib/Service/RecoveryEmailService.php @@ -24,6 +24,7 @@ use OCP\Security\VerificationToken\IVerificationToken; use OCP\Util; use OCP\Files\IAppData; use OCP\Files\NotFoundException; +use OC\Files\AppData\Factory; class RecoveryEmailService { private ILogger $logger; @@ -40,10 +41,11 @@ class RecoveryEmailService { private array $apiConfig; protected const TOKEN_LIFETIME = 60 * 30; // 30 minutes private IAppData $appData; + private Factory $appDataFactory; private const BLACKLISTED_DOMAINS_FOLDER_NAME = 'ecloud-accounts'; private const BLACKLISTED_DOMAINS_FILE_NAME = 'blacklisted_domains.json'; - public function __construct(string $appName, ILogger $logger, IConfig $config, LDAPConnectionService $LDAPConnectionService, IUserManager $userManager, IMailer $mailer, IFactory $l10nFactory, IURLGenerator $urlGenerator, Defaults $themingDefaults, IVerificationToken $verificationToken, CurlService $curlService, IAppData $appData) { + public function __construct(string $appName, ILogger $logger, IConfig $config, LDAPConnectionService $LDAPConnectionService, IUserManager $userManager, IMailer $mailer, IFactory $l10nFactory, IURLGenerator $urlGenerator, Defaults $themingDefaults, IVerificationToken $verificationToken, CurlService $curlService, IAppData $appData, Factory $appDataFactory) { $this->logger = $logger; $this->config = $config; $this->appName = $appName; @@ -55,7 +57,7 @@ class RecoveryEmailService { $this->themingDefaults = $themingDefaults; $this->verificationToken = $verificationToken; $this->curl = $curlService; - $this->appData = $appData; + $this->appData = $appDataFactory->get('ecloud-accounts'); $commonServiceURL = $this->config->getSystemValue('common_services_url', ''); if (!empty($commonServiceURL)) { -- GitLab From 1c832f6d8ecab1db281f5dfee349bb02dd2a3aef Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Wed, 19 Jun 2024 16:27:18 +0530 Subject: [PATCH 3/8] added factory --- lib/Service/RecoveryEmailService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Service/RecoveryEmailService.php b/lib/Service/RecoveryEmailService.php index 6c2e532..cee9211 100644 --- a/lib/Service/RecoveryEmailService.php +++ b/lib/Service/RecoveryEmailService.php @@ -57,7 +57,7 @@ class RecoveryEmailService { $this->themingDefaults = $themingDefaults; $this->verificationToken = $verificationToken; $this->curl = $curlService; - $this->appData = $appDataFactory->get('ecloud-accounts'); + $this->appData = $appDataFactory->get(self::BLACKLISTED_DOMAINS_FOLDER_NAME); $commonServiceURL = $this->config->getSystemValue('common_services_url', ''); if (!empty($commonServiceURL)) { -- GitLab From 1fa1f85753fd2f41fac3752e45d454f3fa438a46 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Wed, 19 Jun 2024 16:27:56 +0530 Subject: [PATCH 4/8] removed unncessary code --- lib/Service/RecoveryEmailService.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/Service/RecoveryEmailService.php b/lib/Service/RecoveryEmailService.php index cee9211..fd4e7af 100644 --- a/lib/Service/RecoveryEmailService.php +++ b/lib/Service/RecoveryEmailService.php @@ -41,11 +41,10 @@ class RecoveryEmailService { private array $apiConfig; protected const TOKEN_LIFETIME = 60 * 30; // 30 minutes private IAppData $appData; - private Factory $appDataFactory; private const BLACKLISTED_DOMAINS_FOLDER_NAME = 'ecloud-accounts'; private const BLACKLISTED_DOMAINS_FILE_NAME = 'blacklisted_domains.json'; - public function __construct(string $appName, ILogger $logger, IConfig $config, LDAPConnectionService $LDAPConnectionService, IUserManager $userManager, IMailer $mailer, IFactory $l10nFactory, IURLGenerator $urlGenerator, Defaults $themingDefaults, IVerificationToken $verificationToken, CurlService $curlService, IAppData $appData, Factory $appDataFactory) { + public function __construct(string $appName, ILogger $logger, IConfig $config, LDAPConnectionService $LDAPConnectionService, IUserManager $userManager, IMailer $mailer, IFactory $l10nFactory, IURLGenerator $urlGenerator, Defaults $themingDefaults, IVerificationToken $verificationToken, CurlService $curlService, Factory $appDataFactory) { $this->logger = $logger; $this->config = $config; $this->appName = $appName; -- GitLab From 177ea540cddae420e5378853a90c5313410df34f Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Thu, 20 Jun 2024 07:48:10 +0530 Subject: [PATCH 5/8] added ecloudaccount code --- lib/Service/RecoveryEmailService.php | 42 ++++------------------------ 1 file changed, 6 insertions(+), 36 deletions(-) diff --git a/lib/Service/RecoveryEmailService.php b/lib/Service/RecoveryEmailService.php index fd4e7af..d0c4033 100644 --- a/lib/Service/RecoveryEmailService.php +++ b/lib/Service/RecoveryEmailService.php @@ -22,9 +22,7 @@ use OCP\Mail\IEMailTemplate; use OCP\Mail\IMailer; use OCP\Security\VerificationToken\IVerificationToken; use OCP\Util; -use OCP\Files\IAppData; -use OCP\Files\NotFoundException; -use OC\Files\AppData\Factory; +use OCA\EcloudAccounts\Service\UserService; class RecoveryEmailService { private ILogger $logger; @@ -40,11 +38,9 @@ class RecoveryEmailService { private CurlService $curl; private array $apiConfig; protected const TOKEN_LIFETIME = 60 * 30; // 30 minutes - private IAppData $appData; - private const BLACKLISTED_DOMAINS_FOLDER_NAME = 'ecloud-accounts'; - private const BLACKLISTED_DOMAINS_FILE_NAME = 'blacklisted_domains.json'; + private UserService $userService; - public function __construct(string $appName, ILogger $logger, IConfig $config, LDAPConnectionService $LDAPConnectionService, IUserManager $userManager, IMailer $mailer, IFactory $l10nFactory, IURLGenerator $urlGenerator, Defaults $themingDefaults, IVerificationToken $verificationToken, CurlService $curlService, Factory $appDataFactory) { + public function __construct(string $appName, ILogger $logger, IConfig $config, LDAPConnectionService $LDAPConnectionService, IUserManager $userManager, IMailer $mailer, IFactory $l10nFactory, IURLGenerator $urlGenerator, Defaults $themingDefaults, IVerificationToken $verificationToken, CurlService $curlService, UserService $userService) { $this->logger = $logger; $this->config = $config; $this->appName = $appName; @@ -56,7 +52,7 @@ class RecoveryEmailService { $this->themingDefaults = $themingDefaults; $this->verificationToken = $verificationToken; $this->curl = $curlService; - $this->appData = $appDataFactory->get(self::BLACKLISTED_DOMAINS_FOLDER_NAME); + $this->userService = $userService; $commonServiceURL = $this->config->getSystemValue('common_services_url', ''); if (!empty($commonServiceURL)) { @@ -243,10 +239,10 @@ class RecoveryEmailService { */ public function isBlacklistedEmail(string $email): bool { // Get the blacklisted domains from configuration - if (!$this->ensureDocumentsFolder()) { + if (!$this->userService->ensureDocumentsFolder()) { return false; } - $blacklistedDomainsInJson = $this->getBlacklistedDomainData(); + $blacklistedDomainsInJson = $this->userService->getBlacklistedDomainData(); if (empty($blacklistedDomainsInJson)) { return false; } @@ -264,32 +260,6 @@ class RecoveryEmailService { // Check if the email domain is in the blacklisted domains array return in_array($emailDomain, $blacklistedDomains); } - /** - * Retrieve the blacklisted domain data. - * - */ - public function getBlacklistedDomainData() { - $foldername = self::BLACKLISTED_DOMAINS_FOLDER_NAME; - $document = self::BLACKLISTED_DOMAINS_FILE_NAME; - return $this->appData->getFolder($foldername)->getFile((string) $document)->getContent(); - } - /** - * Ensure the specified folder exists within AppData. - * - */ - private function ensureDocumentsFolder(): bool { - $foldername = self::BLACKLISTED_DOMAINS_FOLDER_NAME; - try { - $this->appData->getFolder($foldername); - } catch (NotFoundException $e) { - $this->logger->logException('Blacklisted domains file not found!'); - return false; - } catch (\RuntimeException $e) { - $this->logger->logException($e); - return false; - } - return true; - } private function manageEmailRestriction(string $email, string $method, string $url) : void { $params = []; -- GitLab From 33301c074b234885ad122d034254ce756344f253 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Thu, 20 Jun 2024 07:59:03 +0530 Subject: [PATCH 6/8] used isbalcklisted service ditectly --- lib/Service/RecoveryEmailService.php | 32 +--------------------------- 1 file changed, 1 insertion(+), 31 deletions(-) diff --git a/lib/Service/RecoveryEmailService.php b/lib/Service/RecoveryEmailService.php index d0c4033..a209f47 100644 --- a/lib/Service/RecoveryEmailService.php +++ b/lib/Service/RecoveryEmailService.php @@ -99,7 +99,7 @@ class RecoveryEmailService { $this->logger->info("User ID $username's requested recovery email address is disallowed."); throw new MurenaDomainDisallowedException(); } - if ($this->isBlacklistedEmail($recoveryEmail)) { + if ($this->userService->isBlacklistedEmail($recoveryEmail)) { $this->logger->info("User ID $username's requested recovery email address domain is blacklisted. Please provide another recovery address."); throw new BlacklistedEmailException(); } @@ -231,36 +231,6 @@ class RecoveryEmailService { $this->deleteUnverifiedRecoveryEmail($userId); } } - /** - * Check if an email domain is blacklisted against a JSON list of disposable email domains. - * - * @param string $email The email address to check. - * @return bool True if the email domain is blacklisted, false otherwise. - */ - public function isBlacklistedEmail(string $email): bool { - // Get the blacklisted domains from configuration - if (!$this->userService->ensureDocumentsFolder()) { - return false; - } - $blacklistedDomainsInJson = $this->userService->getBlacklistedDomainData(); - if (empty($blacklistedDomainsInJson)) { - return false; - } - $blacklistedDomains = json_decode($blacklistedDomainsInJson, true); - - if (empty($blacklistedDomains)) { - return false; - } - // Split the email address into parts using explode - $emailParts = explode('@', $email); - - // Extract the domain part - $emailDomain = strtolower(end($emailParts)); - - // Check if the email domain is in the blacklisted domains array - return in_array($emailDomain, $blacklistedDomains); - } - private function manageEmailRestriction(string $email, string $method, string $url) : void { $params = []; -- GitLab From ea92e6e8794c7d1d60b2e7eabd7fc9b6f85f0a0f Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Thu, 20 Jun 2024 14:00:23 +0530 Subject: [PATCH 7/8] blacklisted --- lib/Service/RecoveryEmailService.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/Service/RecoveryEmailService.php b/lib/Service/RecoveryEmailService.php index a209f47..455ea00 100644 --- a/lib/Service/RecoveryEmailService.php +++ b/lib/Service/RecoveryEmailService.php @@ -22,7 +22,7 @@ use OCP\Mail\IEMailTemplate; use OCP\Mail\IMailer; use OCP\Security\VerificationToken\IVerificationToken; use OCP\Util; -use OCA\EcloudAccounts\Service\UserService; +use OCA\EcloudAccounts\Service\BlackListService; class RecoveryEmailService { private ILogger $logger; @@ -38,9 +38,9 @@ class RecoveryEmailService { private CurlService $curl; private array $apiConfig; protected const TOKEN_LIFETIME = 60 * 30; // 30 minutes - private UserService $userService; + private BlackListService $blackListService; - public function __construct(string $appName, ILogger $logger, IConfig $config, LDAPConnectionService $LDAPConnectionService, IUserManager $userManager, IMailer $mailer, IFactory $l10nFactory, IURLGenerator $urlGenerator, Defaults $themingDefaults, IVerificationToken $verificationToken, CurlService $curlService, UserService $userService) { + public function __construct(string $appName, ILogger $logger, IConfig $config, LDAPConnectionService $LDAPConnectionService, IUserManager $userManager, IMailer $mailer, IFactory $l10nFactory, IURLGenerator $urlGenerator, Defaults $themingDefaults, IVerificationToken $verificationToken, CurlService $curlService, BlackListService $blackListService) { $this->logger = $logger; $this->config = $config; $this->appName = $appName; @@ -52,7 +52,7 @@ class RecoveryEmailService { $this->themingDefaults = $themingDefaults; $this->verificationToken = $verificationToken; $this->curl = $curlService; - $this->userService = $userService; + $this->blackListService = $blackListService; $commonServiceURL = $this->config->getSystemValue('common_services_url', ''); if (!empty($commonServiceURL)) { @@ -99,7 +99,7 @@ class RecoveryEmailService { $this->logger->info("User ID $username's requested recovery email address is disallowed."); throw new MurenaDomainDisallowedException(); } - if ($this->userService->isBlacklistedEmail($recoveryEmail)) { + if ($this->blackListService->isBlacklistedEmail($recoveryEmail)) { $this->logger->info("User ID $username's requested recovery email address domain is blacklisted. Please provide another recovery address."); throw new BlacklistedEmailException(); } -- GitLab From 20ecf5fd573c35234aa44d7492bee459a4eb5adb Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 21 Jun 2024 07:46:34 +0530 Subject: [PATCH 8/8] bump --- appinfo/info.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appinfo/info.xml b/appinfo/info.xml index 39b37f5..bf39775 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -5,7 +5,7 @@ Email Recovery Email Recovery App - 6.0.2 + 6.1.0 agpl MURENA SAS EmailRecovery -- GitLab