CredentialsManager.php 3.2 KB

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