1
0

CredentialsManager.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OC\Security;
  9. use OCP\IDBConnection;
  10. use OCP\Security\ICredentialsManager;
  11. use OCP\Security\ICrypto;
  12. /**
  13. * Store and retrieve credentials for external services
  14. *
  15. * @package OC\Security
  16. */
  17. class CredentialsManager implements ICredentialsManager {
  18. public const DB_TABLE = 'storages_credentials';
  19. public function __construct(
  20. protected ICrypto $crypto,
  21. protected IDBConnection $dbConnection,
  22. ) {
  23. }
  24. /**
  25. * Store a set of credentials
  26. *
  27. * @param string $userId empty string for system-wide credentials
  28. * @param mixed $credentials
  29. */
  30. public function store(string $userId, string $identifier, $credentials): void {
  31. $value = $this->crypto->encrypt(json_encode($credentials));
  32. $this->dbConnection->setValues(self::DB_TABLE, [
  33. 'user' => $userId,
  34. 'identifier' => $identifier,
  35. ], [
  36. 'credentials' => $value,
  37. ]);
  38. }
  39. /**
  40. * Retrieve a set of credentials
  41. *
  42. * @param string $userId empty string for system-wide credentials
  43. */
  44. public function retrieve(string $userId, string $identifier): mixed {
  45. $qb = $this->dbConnection->getQueryBuilder();
  46. $qb->select('credentials')
  47. ->from(self::DB_TABLE)
  48. ->where($qb->expr()->eq('identifier', $qb->createNamedParameter($identifier)));
  49. if ($userId === '') {
  50. $qb->andWhere($qb->expr()->emptyString('user'));
  51. } else {
  52. $qb->andWhere($qb->expr()->eq('user', $qb->createNamedParameter($userId)));
  53. }
  54. $qResult = $qb->execute();
  55. $result = $qResult->fetch();
  56. $qResult->closeCursor();
  57. if (!$result) {
  58. return null;
  59. }
  60. $value = $result['credentials'];
  61. return json_decode($this->crypto->decrypt($value), true);
  62. }
  63. /**
  64. * Delete a set of credentials
  65. *
  66. * @param string $userId empty string for system-wide credentials
  67. * @return int rows removed
  68. */
  69. public function delete(string $userId, string $identifier): int {
  70. $qb = $this->dbConnection->getQueryBuilder();
  71. $qb->delete(self::DB_TABLE)
  72. ->where($qb->expr()->eq('identifier', $qb->createNamedParameter($identifier)));
  73. if ($userId === '') {
  74. $qb->andWhere($qb->expr()->emptyString('user'));
  75. } else {
  76. $qb->andWhere($qb->expr()->eq('user', $qb->createNamedParameter($userId)));
  77. }
  78. return $qb->execute();
  79. }
  80. /**
  81. * Erase all credentials stored for a user
  82. *
  83. * @return int rows removed
  84. */
  85. public function erase(string $userId): int {
  86. $qb = $this->dbConnection->getQueryBuilder();
  87. $qb->delete(self::DB_TABLE)
  88. ->where($qb->expr()->eq('user', $qb->createNamedParameter($userId)))
  89. ;
  90. return $qb->execute();
  91. }
  92. }