You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
3.4 KiB

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