diff options
Diffstat (limited to 'core/Db')
-rw-r--r-- | core/Db/LoginFlowV2.php | 70 | ||||
-rw-r--r-- | core/Db/LoginFlowV2Mapper.php | 90 | ||||
-rw-r--r-- | core/Db/ProfileConfig.php | 106 | ||||
-rw-r--r-- | core/Db/ProfileConfigMapper.php | 34 |
4 files changed, 300 insertions, 0 deletions
diff --git a/core/Db/LoginFlowV2.php b/core/Db/LoginFlowV2.php new file mode 100644 index 00000000000..1671e5990e0 --- /dev/null +++ b/core/Db/LoginFlowV2.php @@ -0,0 +1,70 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OC\Core\Db; + +use OCP\AppFramework\Db\Entity; +use OCP\DB\Types; + +/** + * @method int getTimestamp() + * @method void setTimestamp(int $timestamp) + * @method int getStarted() + * @method void setStarted(int $started) + * @method string getPollToken() + * @method void setPollToken(string $token) + * @method string getLoginToken() + * @method void setLoginToken(string $token) + * @method string getPublicKey() + * @method void setPublicKey(string $key) + * @method string getPrivateKey() + * @method void setPrivateKey(string $key) + * @method string getClientName() + * @method void setClientName(string $clientName) + * @method string getLoginName() + * @method void setLoginName(string $loginName) + * @method string getServer() + * @method void setServer(string $server) + * @method string getAppPassword() + * @method void setAppPassword(string $appPassword) + */ +class LoginFlowV2 extends Entity { + /** @var int */ + protected $timestamp; + /** @var int */ + protected $started; + /** @var string */ + protected $pollToken; + /** @var string */ + protected $loginToken; + /** @var string */ + protected $publicKey; + /** @var string */ + protected $privateKey; + /** @var string */ + protected $clientName; + /** @var string */ + protected $loginName; + /** @var string */ + protected $server; + /** @var string */ + protected $appPassword; + + public function __construct() { + $this->addType('timestamp', Types::INTEGER); + $this->addType('started', Types::INTEGER); + $this->addType('pollToken', 'string'); + $this->addType('loginToken', 'string'); + $this->addType('publicKey', 'string'); + $this->addType('privateKey', 'string'); + $this->addType('clientName', 'string'); + $this->addType('loginName', 'string'); + $this->addType('server', 'string'); + $this->addType('appPassword', 'string'); + } +} diff --git a/core/Db/LoginFlowV2Mapper.php b/core/Db/LoginFlowV2Mapper.php new file mode 100644 index 00000000000..32664917cb5 --- /dev/null +++ b/core/Db/LoginFlowV2Mapper.php @@ -0,0 +1,90 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OC\Core\Db; + +use OCP\AppFramework\Db\DoesNotExistException; +use OCP\AppFramework\Db\QBMapper; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\IDBConnection; + +/** + * @template-extends QBMapper<LoginFlowV2> + */ +class LoginFlowV2Mapper extends QBMapper { + private const lifetime = 1200; + + public function __construct( + IDBConnection $db, + private ITimeFactory $timeFactory, + ) { + parent::__construct( + $db, + 'login_flow_v2', + LoginFlowV2::class, + ); + } + + /** + * @param string $pollToken + * @return LoginFlowV2 + * @throws DoesNotExistException + */ + public function getByPollToken(string $pollToken): LoginFlowV2 { + $qb = $this->db->getQueryBuilder(); + $qb->select('*') + ->from($this->getTableName()) + ->where( + $qb->expr()->eq('poll_token', $qb->createNamedParameter($pollToken)) + ); + + $entity = $this->findEntity($qb); + return $this->validateTimestamp($entity); + } + + /** + * @param string $loginToken + * @return LoginFlowV2 + * @throws DoesNotExistException + */ + public function getByLoginToken(string $loginToken): LoginFlowV2 { + $qb = $this->db->getQueryBuilder(); + $qb->select('*') + ->from($this->getTableName()) + ->where( + $qb->expr()->eq('login_token', $qb->createNamedParameter($loginToken)) + ); + + $entity = $this->findEntity($qb); + return $this->validateTimestamp($entity); + } + + public function cleanup(): void { + $qb = $this->db->getQueryBuilder(); + $qb->delete($this->getTableName()) + ->where( + $qb->expr()->lt('timestamp', $qb->createNamedParameter($this->timeFactory->getTime() - self::lifetime)) + ); + + $qb->executeStatement(); + } + + /** + * @param LoginFlowV2 $flowV2 + * @return LoginFlowV2 + * @throws DoesNotExistException + */ + private function validateTimestamp(LoginFlowV2 $flowV2): LoginFlowV2 { + if ($flowV2->getTimestamp() < ($this->timeFactory->getTime() - self::lifetime)) { + $this->delete($flowV2); + throw new DoesNotExistException('Token expired'); + } + + return $flowV2; + } +} diff --git a/core/Db/ProfileConfig.php b/core/Db/ProfileConfig.php new file mode 100644 index 00000000000..226a77b7330 --- /dev/null +++ b/core/Db/ProfileConfig.php @@ -0,0 +1,106 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OC\Core\Db; + +use JsonSerializable; +use OCP\AppFramework\Db\Entity; +use OCP\Profile\ParameterDoesNotExistException; +use function json_decode; +use function json_encode; + +/** + * @method string getUserId() + * @method void setUserId(string $userId) + * @method string getConfig() + * @method void setConfig(string $config) + */ +class ProfileConfig extends Entity implements JsonSerializable { + /** @var string */ + protected $userId; + + /** @var string */ + protected $config; + + public function __construct() { + $this->addType('userId', 'string'); + $this->addType('config', 'string'); + } + + /** + * Returns the config in an associative array + */ + public function getConfigArray(): array { + return json_decode($this->config, true); + } + + /** + * Set the config + */ + public function setConfigArray(array $config): void { + $this->setConfig(json_encode($config)); + } + + /** + * Returns the visibility map in an associative array + */ + public function getVisibilityMap(): array { + $config = $this->getConfigArray(); + $visibilityMap = []; + foreach ($config as $paramId => $paramConfig) { + $visibilityMap[$paramId] = $paramConfig['visibility']; + } + + return $visibilityMap; + } + + /** + * Set the visibility map + */ + public function setVisibilityMap(array $visibilityMap): void { + $config = $this->getConfigArray(); + foreach ($visibilityMap as $paramId => $visibility) { + $config[$paramId] = array_merge( + $config[$paramId] ?: [], + ['visibility' => $visibility], + ); + } + + $this->setConfigArray($config); + } + + /** + * Returns the visibility of the parameter + * + * @throws ParameterDoesNotExistException + */ + public function getVisibility(string $paramId): string { + $visibilityMap = $this->getVisibilityMap(); + if (isset($visibilityMap[$paramId])) { + return $visibilityMap[$paramId]; + } + throw new ParameterDoesNotExistException($paramId); + } + + /** + * Set the visibility of the parameter + */ + public function setVisibility(string $paramId, string $visibility): void { + $visibilityMap = $this->getVisibilityMap(); + $visibilityMap[$paramId] = $visibility; + $this->setVisibilityMap($visibilityMap); + } + + public function jsonSerialize(): array { + return [ + 'userId' => $this->userId, + 'config' => $this->getConfigArray(), + ]; + } +} diff --git a/core/Db/ProfileConfigMapper.php b/core/Db/ProfileConfigMapper.php new file mode 100644 index 00000000000..26e3d2da19f --- /dev/null +++ b/core/Db/ProfileConfigMapper.php @@ -0,0 +1,34 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OC\Core\Db; + +use OCP\AppFramework\Db\QBMapper; +use OCP\IDBConnection; + +/** + * @template-extends QBMapper<ProfileConfig> + */ +class ProfileConfigMapper extends QBMapper { + public function __construct(IDBConnection $db) { + parent::__construct($db, 'profile_config', ProfileConfig::class); + } + + public function get(string $userId): ProfileConfig { + $qb = $this->db->getQueryBuilder(); + $qb->select('*') + ->from($this->getTableName()) + ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId))); + return $this->findEntity($qb); + } + + public function getArray(string $userId): array { + return $this->get($userId)->getConfigArray(); + } +} |