SettingsController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 OCA\OAuth2\Db\AccessTokenMapper;
  31. use OCA\OAuth2\Db\Client;
  32. use OCA\OAuth2\Db\ClientMapper;
  33. use OCP\AppFramework\Controller;
  34. use OCP\AppFramework\Http;
  35. use OCP\AppFramework\Http\JSONResponse;
  36. use OCP\Authentication\Token\IProvider as IAuthTokenProvider;
  37. use OCP\IL10N;
  38. use OCP\IRequest;
  39. use OCP\IUser;
  40. use OCP\IUserManager;
  41. use OCP\Security\ICrypto;
  42. use OCP\Security\ISecureRandom;
  43. class SettingsController extends Controller {
  44. public const validChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  45. public function __construct(
  46. string $appName,
  47. IRequest $request,
  48. private ClientMapper $clientMapper,
  49. private ISecureRandom $secureRandom,
  50. private AccessTokenMapper $accessTokenMapper,
  51. private IL10N $l,
  52. private IAuthTokenProvider $tokenProvider,
  53. private IUserManager $userManager,
  54. private ICrypto $crypto
  55. ) {
  56. parent::__construct($appName, $request);
  57. }
  58. public function addClient(string $name,
  59. string $redirectUri): JSONResponse {
  60. if (filter_var($redirectUri, FILTER_VALIDATE_URL) === false) {
  61. 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);
  62. }
  63. $client = new Client();
  64. $client->setName($name);
  65. $client->setRedirectUri($redirectUri);
  66. $secret = $this->secureRandom->generate(64, self::validChars);
  67. $encryptedSecret = $this->crypto->encrypt($secret);
  68. $client->setSecret($encryptedSecret);
  69. $client->setClientIdentifier($this->secureRandom->generate(64, self::validChars));
  70. $client = $this->clientMapper->insert($client);
  71. $result = [
  72. 'id' => $client->getId(),
  73. 'name' => $client->getName(),
  74. 'redirectUri' => $client->getRedirectUri(),
  75. 'clientId' => $client->getClientIdentifier(),
  76. 'clientSecret' => $secret,
  77. ];
  78. return new JSONResponse($result);
  79. }
  80. public function deleteClient(int $id): JSONResponse {
  81. $client = $this->clientMapper->getByUid($id);
  82. $this->userManager->callForAllUsers(function (IUser $user) use ($client) {
  83. $this->tokenProvider->invalidateTokensOfUser($user->getUID(), $client->getName());
  84. });
  85. $this->accessTokenMapper->deleteByClientId($id);
  86. $this->clientMapper->delete($client);
  87. return new JSONResponse([]);
  88. }
  89. }