SettingsController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\IL10N;
  37. use OCP\IRequest;
  38. use OCP\Security\ISecureRandom;
  39. class SettingsController extends Controller {
  40. /** @var ClientMapper */
  41. private $clientMapper;
  42. /** @var ISecureRandom */
  43. private $secureRandom;
  44. /** @var AccessTokenMapper */
  45. private $accessTokenMapper;
  46. /** @var IL10N */
  47. private $l;
  48. public const validChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  49. public function __construct(string $appName,
  50. IRequest $request,
  51. ClientMapper $clientMapper,
  52. ISecureRandom $secureRandom,
  53. AccessTokenMapper $accessTokenMapper,
  54. IL10N $l
  55. ) {
  56. parent::__construct($appName, $request);
  57. $this->secureRandom = $secureRandom;
  58. $this->clientMapper = $clientMapper;
  59. $this->accessTokenMapper = $accessTokenMapper;
  60. $this->l = $l;
  61. }
  62. public function addClient(string $name,
  63. string $redirectUri): JSONResponse {
  64. if (filter_var($redirectUri, FILTER_VALIDATE_URL) === false) {
  65. 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);
  66. }
  67. $client = new Client();
  68. $client->setName($name);
  69. $client->setRedirectUri($redirectUri);
  70. $client->setSecret($this->secureRandom->generate(64, self::validChars));
  71. $client->setClientIdentifier($this->secureRandom->generate(64, self::validChars));
  72. $client = $this->clientMapper->insert($client);
  73. $result = [
  74. 'id' => $client->getId(),
  75. 'name' => $client->getName(),
  76. 'redirectUri' => $client->getRedirectUri(),
  77. 'clientId' => $client->getClientIdentifier(),
  78. 'clientSecret' => $client->getSecret(),
  79. ];
  80. return new JSONResponse($result);
  81. }
  82. public function deleteClient(int $id): JSONResponse {
  83. $client = $this->clientMapper->getByUid($id);
  84. $this->accessTokenMapper->deleteByClientId($id);
  85. $this->clientMapper->delete($client);
  86. return new JSONResponse([]);
  87. }
  88. }