1
0

CredentialsManager.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. /** @var ICrypto */
  39. protected $crypto;
  40. /** @var IDBConnection */
  41. protected $dbConnection;
  42. /**
  43. * @param ICrypto $crypto
  44. * @param IDBConnection $dbConnection
  45. */
  46. public function __construct(ICrypto $crypto, IDBConnection $dbConnection) {
  47. $this->crypto = $crypto;
  48. $this->dbConnection = $dbConnection;
  49. }
  50. /**
  51. * Store a set of credentials
  52. *
  53. * @param string $userId empty string for system-wide credentials
  54. * @param string $identifier
  55. * @param mixed $credentials
  56. */
  57. public function store(string $userId, string $identifier, $credentials): void {
  58. $value = $this->crypto->encrypt(json_encode($credentials));
  59. $this->dbConnection->setValues(self::DB_TABLE, [
  60. 'user' => $userId,
  61. 'identifier' => $identifier,
  62. ], [
  63. 'credentials' => $value,
  64. ]);
  65. }
  66. /**
  67. * Retrieve a set of credentials
  68. *
  69. * @param string $userId empty string for system-wide credentials
  70. * @param string $identifier
  71. * @return mixed
  72. */
  73. public function retrieve(string $userId, string $identifier) {
  74. $qb = $this->dbConnection->getQueryBuilder();
  75. $qb->select('credentials')
  76. ->from(self::DB_TABLE)
  77. ->where($qb->expr()->eq('identifier', $qb->createNamedParameter($identifier)));
  78. if ($userId === '') {
  79. $qb->andWhere($qb->expr()->emptyString('user'));
  80. } else {
  81. $qb->andWhere($qb->expr()->eq('user', $qb->createNamedParameter($userId)));
  82. }
  83. $qResult = $qb->execute();
  84. $result = $qResult->fetch();
  85. $qResult->closeCursor();
  86. if (!$result) {
  87. return null;
  88. }
  89. $value = $result['credentials'];
  90. return json_decode($this->crypto->decrypt($value), true);
  91. }
  92. /**
  93. * Delete a set of credentials
  94. *
  95. * @param string $userId empty string for system-wide credentials
  96. * @param string $identifier
  97. * @return int rows removed
  98. */
  99. public function delete(string $userId, string $identifier): int {
  100. $qb = $this->dbConnection->getQueryBuilder();
  101. $qb->delete(self::DB_TABLE)
  102. ->where($qb->expr()->eq('identifier', $qb->createNamedParameter($identifier)));
  103. if ($userId === '') {
  104. $qb->andWhere($qb->expr()->emptyString('user'));
  105. } else {
  106. $qb->andWhere($qb->expr()->eq('user', $qb->createNamedParameter($userId)));
  107. }
  108. return $qb->execute();
  109. }
  110. /**
  111. * Erase all credentials stored for a user
  112. *
  113. * @param string $userId
  114. * @return int rows removed
  115. */
  116. public function erase(string $userId): int {
  117. $qb = $this->dbConnection->getQueryBuilder();
  118. $qb->delete(self::DB_TABLE)
  119. ->where($qb->expr()->eq('user', $qb->createNamedParameter($userId)))
  120. ;
  121. return $qb->execute();
  122. }
  123. }