1
0

CredentialsManager.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author J0WI <J0WI@users.noreply.github.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Robin McCorkell <robin@mccorkell.me.uk>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\Security;
  28. use OCP\IDBConnection;
  29. use OCP\Security\ICredentialsManager;
  30. use OCP\Security\ICrypto;
  31. /**
  32. * Store and retrieve credentials for external services
  33. *
  34. * @package OC\Security
  35. */
  36. class CredentialsManager implements ICredentialsManager {
  37. public const DB_TABLE = 'storages_credentials';
  38. public function __construct(
  39. protected ICrypto $crypto,
  40. protected IDBConnection $dbConnection,
  41. ) {
  42. }
  43. /**
  44. * Store a set of credentials
  45. *
  46. * @param string $userId empty string for system-wide credentials
  47. * @param mixed $credentials
  48. */
  49. public function store(string $userId, string $identifier, $credentials): void {
  50. $value = $this->crypto->encrypt(json_encode($credentials));
  51. $this->dbConnection->setValues(self::DB_TABLE, [
  52. 'user' => $userId,
  53. 'identifier' => $identifier,
  54. ], [
  55. 'credentials' => $value,
  56. ]);
  57. }
  58. /**
  59. * Retrieve a set of credentials
  60. *
  61. * @param string $userId empty string for system-wide credentials
  62. */
  63. public function retrieve(string $userId, string $identifier): mixed {
  64. $qb = $this->dbConnection->getQueryBuilder();
  65. $qb->select('credentials')
  66. ->from(self::DB_TABLE)
  67. ->where($qb->expr()->eq('identifier', $qb->createNamedParameter($identifier)));
  68. if ($userId === '') {
  69. $qb->andWhere($qb->expr()->emptyString('user'));
  70. } else {
  71. $qb->andWhere($qb->expr()->eq('user', $qb->createNamedParameter($userId)));
  72. }
  73. $qResult = $qb->execute();
  74. $result = $qResult->fetch();
  75. $qResult->closeCursor();
  76. if (!$result) {
  77. return null;
  78. }
  79. $value = $result['credentials'];
  80. return json_decode($this->crypto->decrypt($value), true);
  81. }
  82. /**
  83. * Delete a set of credentials
  84. *
  85. * @param string $userId empty string for system-wide credentials
  86. * @return int rows removed
  87. */
  88. public function delete(string $userId, string $identifier): int {
  89. $qb = $this->dbConnection->getQueryBuilder();
  90. $qb->delete(self::DB_TABLE)
  91. ->where($qb->expr()->eq('identifier', $qb->createNamedParameter($identifier)));
  92. if ($userId === '') {
  93. $qb->andWhere($qb->expr()->emptyString('user'));
  94. } else {
  95. $qb->andWhere($qb->expr()->eq('user', $qb->createNamedParameter($userId)));
  96. }
  97. return $qb->execute();
  98. }
  99. /**
  100. * Erase all credentials stored for a user
  101. *
  102. * @return int rows removed
  103. */
  104. public function erase(string $userId): int {
  105. $qb = $this->dbConnection->getQueryBuilder();
  106. $qb->delete(self::DB_TABLE)
  107. ->where($qb->expr()->eq('user', $qb->createNamedParameter($userId)))
  108. ;
  109. return $qb->execute();
  110. }
  111. }