SettingsController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch>
  5. *
  6. * @author Bjoern Schiessle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Patrik Kernstock <info@pkern.at>
  10. * @author rakekniven <mark.ziegler@rakekniven.de>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. *
  13. * @license GNU AGPL version 3 or any later version
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License as
  17. * published by the Free Software Foundation, either version 3 of the
  18. * License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *
  28. */
  29. namespace OCA\OAuth2\Controller;
  30. use OCP\Authentication\Token\IProvider as IAuthTokenProvider;
  31. use OCA\OAuth2\Db\AccessTokenMapper;
  32. use OCA\OAuth2\Db\Client;
  33. use OCA\OAuth2\Db\ClientMapper;
  34. use OCP\AppFramework\Controller;
  35. use OCP\AppFramework\Http;
  36. use OCP\AppFramework\Http\JSONResponse;
  37. use OCP\IL10N;
  38. use OCP\IRequest;
  39. use OCP\IUser;
  40. use OCP\IUserManager;
  41. use OCP\Security\ISecureRandom;
  42. use OCP\Security\ICrypto;
  43. class SettingsController extends Controller {
  44. /** @var ClientMapper */
  45. private $clientMapper;
  46. /** @var ISecureRandom */
  47. private $secureRandom;
  48. /** @var AccessTokenMapper */
  49. private $accessTokenMapper;
  50. /** @var IL10N */
  51. private $l;
  52. /** @var ICrypto */
  53. private $crypto;
  54. /** @var IAuthTokenProvider */
  55. private $tokenProvider;
  56. /**
  57. * @var IUserManager
  58. */
  59. private $userManager;
  60. public const validChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  61. public function __construct(string $appName,
  62. IRequest $request,
  63. ClientMapper $clientMapper,
  64. ISecureRandom $secureRandom,
  65. AccessTokenMapper $accessTokenMapper,
  66. IL10N $l,
  67. ICrypto $crypto,
  68. IAuthTokenProvider $tokenProvider,
  69. IUserManager $userManager
  70. ) {
  71. parent::__construct($appName, $request);
  72. $this->secureRandom = $secureRandom;
  73. $this->clientMapper = $clientMapper;
  74. $this->accessTokenMapper = $accessTokenMapper;
  75. $this->l = $l;
  76. $this->crypto = $crypto;
  77. $this->tokenProvider = $tokenProvider;
  78. $this->userManager = $userManager;
  79. }
  80. public function addClient(string $name,
  81. string $redirectUri): JSONResponse {
  82. if (filter_var($redirectUri, FILTER_VALIDATE_URL) === false) {
  83. return new JSONResponse(['message' => $this->l->t('Your redirect URL needs to be a full URL for example: https://yourdomain.com/path')], Http::STATUS_BAD_REQUEST);
  84. }
  85. $client = new Client();
  86. $client->setName($name);
  87. $client->setRedirectUri($redirectUri);
  88. $secret = $this->secureRandom->generate(64, self::validChars);
  89. $encryptedSecret = $this->crypto->encrypt($secret);
  90. $client->setSecret($encryptedSecret);
  91. $client->setClientIdentifier($this->secureRandom->generate(64, self::validChars));
  92. $client = $this->clientMapper->insert($client);
  93. $result = [
  94. 'id' => $client->getId(),
  95. 'name' => $client->getName(),
  96. 'redirectUri' => $client->getRedirectUri(),
  97. 'clientId' => $client->getClientIdentifier(),
  98. 'clientSecret' => $secret,
  99. ];
  100. return new JSONResponse($result);
  101. }
  102. public function deleteClient(int $id): JSONResponse {
  103. $client = $this->clientMapper->getByUid($id);
  104. $this->userManager->callForSeenUsers(function (IUser $user) use ($client) {
  105. $this->tokenProvider->invalidateTokensOfUser($user->getUID(), $client->getName());
  106. });
  107. $this->accessTokenMapper->deleteByClientId($id);
  108. $this->clientMapper->delete($client);
  109. return new JSONResponse([]);
  110. }
  111. }