summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorArthur Schiwon <blizzz@arthur-schiwon.de>2021-08-20 19:59:08 +0200
committerArthur Schiwon <blizzz@arthur-schiwon.de>2021-09-09 14:03:35 +0200
commitaacaad2a3f56893c6be463ec7a21c868322654ee (patch)
tree0e995c930501cae44ba49ff707ddc896c6048093 /lib
parent19cc757531959a14df40a79d550c82b39e4bc5a2 (diff)
downloadnextcloud-server-aacaad2a3f56893c6be463ec7a21c868322654ee.tar.gz
nextcloud-server-aacaad2a3f56893c6be463ec7a21c868322654ee.zip
implement verification for additional mails
- mails added by (sub)admins are automatically verified - provisioning_api controller as verification endpoint - IAccountProperty gets a locallyVerified property - IPropertyCollection gets a method to fetch an IAccountProperty by value - an remove equivalent was already present - AccountManager always initiates mail verification on update if necessary - add core success template for arbitrary title and message Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
Diffstat (limited to 'lib')
-rw-r--r--lib/private/Accounts/AccountManager.php131
-rw-r--r--lib/private/Accounts/AccountProperty.php21
-rw-r--r--lib/private/Accounts/AccountPropertyCollection.php9
-rw-r--r--lib/private/Server.php4
-rw-r--r--lib/public/Accounts/IAccountProperty.php20
-rw-r--r--lib/public/Accounts/IAccountPropertyCollection.php9
6 files changed, 183 insertions, 11 deletions
diff --git a/lib/private/Accounts/AccountManager.php b/lib/private/Accounts/AccountManager.php
index 9fc5accfa08..a3f971df6a1 100644
--- a/lib/private/Accounts/AccountManager.php
+++ b/lib/private/Accounts/AccountManager.php
@@ -32,6 +32,7 @@
*/
namespace OC\Accounts;
+use Exception;
use InvalidArgumentException;
use libphonenumber\NumberParseException;
use libphonenumber\PhoneNumber;
@@ -45,9 +46,17 @@ use OCP\Accounts\IAccountPropertyCollection;
use OCP\Accounts\PropertyDoesNotExistException;
use OCP\BackgroundJob\IJobList;
use OCP\DB\QueryBuilder\IQueryBuilder;
+use OCP\Defaults;
use OCP\IConfig;
use OCP\IDBConnection;
+use OCP\IL10N;
+use OCP\IURLGenerator;
use OCP\IUser;
+use OCP\L10N\IFactory;
+use OCP\Mail\IMailer;
+use OCP\Security\ICrypto;
+use OCP\Security\VerificationToken\IVerificationToken;
+use OCP\Util;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
@@ -88,17 +97,46 @@ class AccountManager implements IAccountManager {
/** @var LoggerInterface */
private $logger;
-
- public function __construct(IDBConnection $connection,
- IConfig $config,
- EventDispatcherInterface $eventDispatcher,
- IJobList $jobList,
- LoggerInterface $logger) {
+ /** @var IVerificationToken */
+ private $verificationToken;
+ /** @var IMailer */
+ private $mailer;
+ /** @var Defaults */
+ private $defaults;
+ /** @var IL10N */
+ private $l10n;
+ /** @var IURLGenerator */
+ private $urlGenerator;
+ /** @var ICrypto */
+ private $crypto;
+ /** @var IFactory */
+ private $l10nfactory;
+
+ public function __construct(
+ IDBConnection $connection,
+ IConfig $config,
+ EventDispatcherInterface $eventDispatcher,
+ IJobList $jobList,
+ LoggerInterface $logger,
+ IVerificationToken $verificationToken,
+ IMailer $mailer,
+ Defaults $defaults,
+ IFactory $factory,
+ IURLGenerator $urlGenerator,
+ ICrypto $crypto
+ ) {
$this->connection = $connection;
$this->config = $config;
$this->eventDispatcher = $eventDispatcher;
$this->jobList = $jobList;
$this->logger = $logger;
+ $this->verificationToken = $verificationToken;
+ $this->mailer = $mailer;
+ $this->defaults = $defaults;
+ $this->urlGenerator = $urlGenerator;
+ $this->crypto = $crypto;
+ // DIing IL10N results in a dependency loop
+ $this->l10nfactory = $factory;
}
/**
@@ -337,7 +375,6 @@ class AccountManager implements IAccountManager {
/**
* check if we need to ask the server for email verification, if yes we create a cronjob
- *
*/
protected function checkEmailVerification(IAccount $updatedAccount, array $oldData): void {
try {
@@ -358,11 +395,73 @@ class AccountManager implements IAccountManager {
]
);
+ $property->setVerified(self::VERIFICATION_IN_PROGRESS);
+ }
+ }
+
+ protected function checkLocalEmailVerification(IAccount $updatedAccount, array $oldData): void {
+ $mailCollection = $updatedAccount->getPropertyCollection(self::COLLECTION_EMAIL);
+ foreach ($mailCollection->getProperties() as $property) {
+ if ($property->getLocallyVerified() !== self::NOT_VERIFIED) {
+ continue;
+ }
+ if ($this->sendEmailVerificationEmail($updatedAccount->getUser(), $property->getValue())) {
+ $property->setLocallyVerified(self::VERIFICATION_IN_PROGRESS);
+ }
+ }
+ }
+
+ protected function sendEmailVerificationEmail(IUser $user, string $email): bool {
+ $ref = \substr(hash('sha256', $email), 0, 8);
+ $key = $this->crypto->encrypt($email);
+ $token = $this->verificationToken->create($user, 'verifyMail' . $ref, $email);
+ $link = $this->urlGenerator->linkToRouteAbsolute('provisioning_api.Verification.verifyMail',
+ [
+ 'userId' => $user->getUID(),
+ 'token' => $token,
+ 'key' => $key
+ ]);
+ $emailTemplate = $this->mailer->createEMailTemplate('core.EmailVerification', [
+ 'link' => $link,
+ ]);
- $property->setVerified(self::VERIFICATION_IN_PROGRESS);
+ if (!$this->l10n) {
+ $this->l10n = $this->l10nfactory->get('core');
}
+
+ $emailTemplate->setSubject($this->l10n->t('%s email verification', [$this->defaults->getName()]));
+ $emailTemplate->addHeader();
+ $emailTemplate->addHeading($this->l10n->t('Email verification'));
+
+ $emailTemplate->addBodyText(
+ htmlspecialchars($this->l10n->t('Click the following button to confirm your email.')),
+ $this->l10n->t('Click the following link to confirm your email.')
+ );
+
+ $emailTemplate->addBodyButton(
+ htmlspecialchars($this->l10n->t('Confirm your email')),
+ $link,
+ false
+ );
+ $emailTemplate->addFooter();
+
+ try {
+ $message = $this->mailer->createMessage();
+ $message->setTo([$email => $user->getDisplayName()]);
+ $message->setFrom([Util::getDefaultEmailAddress('verification-noreply') => $this->defaults->getName()]);
+ $message->useTemplate($emailTemplate);
+ $this->mailer->send($message);
+ } catch (Exception $e) {
+ // Log the exception and continue
+ $this->logger->info('Failed to send verification mail', [
+ 'app' => 'core',
+ 'exception' => $e
+ ]);
+ return false;
+ }
+ return true;
}
/**
@@ -406,7 +505,6 @@ class AccountManager implements IAccountManager {
}
}
-
/**
* add new user to accounts table
*
@@ -435,6 +533,12 @@ class AccountManager implements IAccountManager {
foreach ($data as $dataRow) {
$propertyName = $dataRow['name'];
unset($dataRow['name']);
+
+ if (isset($dataRow['locallyVerified']) && $dataRow['locallyVerified'] === self::NOT_VERIFIED) {
+ // do not write default value, save DB space
+ unset($dataRow['locallyVerified']);
+ }
+
if (!$this->isCollection($propertyName)) {
$preparedData[$propertyName] = $dataRow;
continue;
@@ -511,7 +615,6 @@ class AccountManager implements IAccountManager {
continue;
}
-
$query->setParameter('name', $property['name'])
->setParameter('value', $property['value'] ?? '');
$query->executeStatement();
@@ -587,6 +690,7 @@ class AccountManager implements IAccountManager {
$data['verified'] ?? self::NOT_VERIFIED,
''
);
+ $p->setLocallyVerified($data['locallyVerified'] ?? self::NOT_VERIFIED);
$collection->addProperty($p);
return $collection;
@@ -599,6 +703,10 @@ class AccountManager implements IAccountManager {
$account->setPropertyCollection($this->arrayDataToCollection($account, $accountData));
} else {
$account->setProperty($accountData['name'], $accountData['value'] ?? '', $accountData['scope'] ?? self::SCOPE_LOCAL, $accountData['verified'] ?? self::NOT_VERIFIED);
+ if (isset($accountData['locallyVerified'])) {
+ $property = $account->getProperty($accountData['name']);
+ $property->setLocallyVerified($accountData['locallyVerified']);
+ }
}
}
return $account;
@@ -640,14 +748,17 @@ class AccountManager implements IAccountManager {
$oldData = $this->getUser($account->getUser(), false);
$this->updateVerificationStatus($account, $oldData);
$this->checkEmailVerification($account, $oldData);
+ $this->checkLocalEmailVerification($account, $oldData);
$data = [];
foreach ($account->getAllProperties() as $property) {
+ /** @var IAccountProperty $property */
$data[] = [
'name' => $property->getName(),
'value' => $property->getValue(),
'scope' => $property->getScope(),
'verified' => $property->getVerified(),
+ 'locallyVerified' => $property->getLocallyVerified(),
];
}
diff --git a/lib/private/Accounts/AccountProperty.php b/lib/private/Accounts/AccountProperty.php
index 1a21baf9698..0e6356e9e92 100644
--- a/lib/private/Accounts/AccountProperty.php
+++ b/lib/private/Accounts/AccountProperty.php
@@ -27,6 +27,7 @@ declare(strict_types=1);
*/
namespace OC\Accounts;
+use InvalidArgumentException;
use OCP\Accounts\IAccountManager;
use OCP\Accounts\IAccountProperty;
@@ -42,6 +43,8 @@ class AccountProperty implements IAccountProperty {
private $verified;
/** @var string */
private $verificationData;
+ /** @var string */
+ private $locallyVerified = IAccountManager::NOT_VERIFIED;
public function __construct(string $name, string $value, string $scope, string $verified, string $verificationData) {
$this->name = $name;
@@ -90,7 +93,7 @@ class AccountProperty implements IAccountProperty {
IAccountManager::SCOPE_PRIVATE,
IAccountManager::SCOPE_PUBLISHED
])) {
- throw new \InvalidArgumentException('Invalid scope');
+ throw new InvalidArgumentException('Invalid scope');
}
$this->scope = $newScope;
return $this;
@@ -178,4 +181,20 @@ class AccountProperty implements IAccountProperty {
public function getVerificationData(): string {
return $this->verificationData;
}
+
+ public function setLocallyVerified(string $verified): IAccountProperty {
+ if (!in_array($verified, [
+ IAccountManager::NOT_VERIFIED,
+ IAccountManager::VERIFICATION_IN_PROGRESS,
+ IAccountManager::VERIFIED,
+ ])) {
+ throw new InvalidArgumentException('Provided verification value is invalid');
+ }
+ $this->locallyVerified = $verified;
+ return $this;
+ }
+
+ public function getLocallyVerified(): string {
+ return $this->locallyVerified;
+ }
}
diff --git a/lib/private/Accounts/AccountPropertyCollection.php b/lib/private/Accounts/AccountPropertyCollection.php
index eb92536a6a0..3aed76d8746 100644
--- a/lib/private/Accounts/AccountPropertyCollection.php
+++ b/lib/private/Accounts/AccountPropertyCollection.php
@@ -84,6 +84,15 @@ class AccountPropertyCollection implements IAccountPropertyCollection {
return $this;
}
+ public function getPropertyByValue(string $value): ?IAccountProperty {
+ foreach ($this->properties as $i => $property) {
+ if ($property->getValue() === $value) {
+ return $property;
+ }
+ }
+ return null;
+ }
+
public function removePropertyByValue(string $value): IAccountPropertyCollection {
foreach ($this->properties as $i => $property) {
if ($property->getValue() === $value) {
diff --git a/lib/private/Server.php b/lib/private/Server.php
index 0320eda2b91..6b6a1402a04 100644
--- a/lib/private/Server.php
+++ b/lib/private/Server.php
@@ -135,6 +135,7 @@ use OC\Security\CSRF\TokenStorage\SessionStorage;
use OC\Security\Hasher;
use OC\Security\SecureRandom;
use OC\Security\TrustedDomainHelper;
+use OC\Security\VerificationToken\VerificationToken;
use OC\Session\CryptoWrapper;
use OC\Share20\ProviderFactory;
use OC\Share20\ShareHelper;
@@ -224,6 +225,7 @@ use OCP\Security\ICredentialsManager;
use OCP\Security\ICrypto;
use OCP\Security\IHasher;
use OCP\Security\ISecureRandom;
+use OCP\Security\VerificationToken\IVerificationToken;
use OCP\Share\IShareHelper;
use OCP\SystemTag\ISystemTagManager;
use OCP\SystemTag\ISystemTagObjectMapper;
@@ -795,6 +797,8 @@ class Server extends ServerContainer implements IServerContainer {
/** @deprecated 19.0.0 */
$this->registerDeprecatedAlias('SecureRandom', \OCP\Security\ISecureRandom::class);
+ $this->registerAlias(IVerificationToken::class, VerificationToken::class);
+
$this->registerAlias(ICrypto::class, Crypto::class);
/** @deprecated 19.0.0 */
$this->registerDeprecatedAlias('Crypto', ICrypto::class);
diff --git a/lib/public/Accounts/IAccountProperty.php b/lib/public/Accounts/IAccountProperty.php
index 20505f299dd..94866fc4807 100644
--- a/lib/public/Accounts/IAccountProperty.php
+++ b/lib/public/Accounts/IAccountProperty.php
@@ -115,4 +115,24 @@ interface IAccountProperty extends \JsonSerializable {
* @since 22.0.0
*/
public function getVerificationData(): string;
+
+ /**
+ * Set the instance-based verification status of a property
+ *
+ * @since 23.0.0
+ *
+ * @param string $verified must be one of the verification constants of IAccountManager
+ * @return IAccountProperty
+ * @throws InvalidArgumentException
+ */
+ public function setLocallyVerified(string $verified): IAccountProperty;
+
+ /**
+ * Get the instance-based verification status of a property
+ *
+ * @since 23.0.0
+ *
+ * @return string
+ */
+ public function getLocallyVerified(): string;
}
diff --git a/lib/public/Accounts/IAccountPropertyCollection.php b/lib/public/Accounts/IAccountPropertyCollection.php
index 779fb1299b4..0d4c416cbaa 100644
--- a/lib/public/Accounts/IAccountPropertyCollection.php
+++ b/lib/public/Accounts/IAccountPropertyCollection.php
@@ -89,4 +89,13 @@ interface IAccountPropertyCollection extends JsonSerializable {
* @since 22.0.0
*/
public function removePropertyByValue(string $value): IAccountPropertyCollection;
+
+ /**
+ * retrieves a property identified by its value. null, if none was found.
+ *
+ * Returns only the first property if there are more with the same value.
+ *
+ * @since 23.0.0
+ */
+ public function getPropertyByValue(string $value): ?IAccountProperty;
}