1
0

SettingsController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. class SettingsController extends Controller {
  43. /** @var ClientMapper */
  44. private $clientMapper;
  45. /** @var ISecureRandom */
  46. private $secureRandom;
  47. /** @var AccessTokenMapper */
  48. private $accessTokenMapper;
  49. /** @var IL10N */
  50. private $l;
  51. /** @var IAuthTokenProvider */
  52. private $tokenProvider;
  53. /**
  54. * @var IUserManager
  55. */
  56. private $userManager;
  57. public const validChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  58. public function __construct(string $appName,
  59. IRequest $request,
  60. ClientMapper $clientMapper,
  61. ISecureRandom $secureRandom,
  62. AccessTokenMapper $accessTokenMapper,
  63. IL10N $l,
  64. IAuthTokenProvider $tokenProvider,
  65. IUserManager $userManager
  66. ) {
  67. parent::__construct($appName, $request);
  68. $this->secureRandom = $secureRandom;
  69. $this->clientMapper = $clientMapper;
  70. $this->accessTokenMapper = $accessTokenMapper;
  71. $this->l = $l;
  72. $this->tokenProvider = $tokenProvider;
  73. $this->userManager = $userManager;
  74. }
  75. public function addClient(string $name,
  76. string $redirectUri): JSONResponse {
  77. if (filter_var($redirectUri, FILTER_VALIDATE_URL) === false) {
  78. 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);
  79. }
  80. $client = new Client();
  81. $client->setName($name);
  82. $client->setRedirectUri($redirectUri);
  83. $client->setSecret($this->secureRandom->generate(64, self::validChars));
  84. $client->setClientIdentifier($this->secureRandom->generate(64, self::validChars));
  85. $client = $this->clientMapper->insert($client);
  86. $result = [
  87. 'id' => $client->getId(),
  88. 'name' => $client->getName(),
  89. 'redirectUri' => $client->getRedirectUri(),
  90. 'clientId' => $client->getClientIdentifier(),
  91. 'clientSecret' => $client->getSecret(),
  92. ];
  93. return new JSONResponse($result);
  94. }
  95. public function deleteClient(int $id): JSONResponse {
  96. $client = $this->clientMapper->getByUid($id);
  97. $this->userManager->callForAllUsers(function (IUser $user) use ($client) {
  98. $this->tokenProvider->invalidateTokensOfUser($user->getUID(), $client->getName());
  99. });
  100. $this->accessTokenMapper->deleteByClientId($id);
  101. $this->clientMapper->delete($client);
  102. return new JSONResponse([]);
  103. }
  104. }