Admin.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch>
  5. *
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\OAuth2\Settings;
  26. use OCA\OAuth2\Db\ClientMapper;
  27. use OCP\AppFramework\Http\TemplateResponse;
  28. use OCP\AppFramework\Services\IInitialState;
  29. use OCP\Security\ICrypto;
  30. use OCP\Settings\ISettings;
  31. use OCP\IURLGenerator;
  32. use Psr\Log\LoggerInterface;
  33. class Admin implements ISettings {
  34. private IInitialState $initialState;
  35. private ClientMapper $clientMapper;
  36. private IURLGenerator $urlGenerator;
  37. private ICrypto $crypto;
  38. private LoggerInterface $logger;
  39. public function __construct(
  40. IInitialState $initialState,
  41. ClientMapper $clientMapper,
  42. IURLGenerator $urlGenerator,
  43. ICrypto $crypto,
  44. LoggerInterface $logger
  45. ) {
  46. $this->initialState = $initialState;
  47. $this->clientMapper = $clientMapper;
  48. $this->urlGenerator = $urlGenerator;
  49. $this->crypto = $crypto;
  50. $this->logger = $logger;
  51. }
  52. public function getForm(): TemplateResponse {
  53. $clients = $this->clientMapper->getClients();
  54. $result = [];
  55. foreach ($clients as $client) {
  56. try {
  57. $secret = $this->crypto->decrypt($client->getSecret());
  58. $result[] = [
  59. 'id' => $client->getId(),
  60. 'name' => $client->getName(),
  61. 'redirectUri' => $client->getRedirectUri(),
  62. 'clientId' => $client->getClientIdentifier(),
  63. 'clientSecret' => $secret,
  64. ];
  65. } catch (\Exception $e) {
  66. $this->logger->error('[Settings] OAuth client secret decryption error', ['exception' => $e]);
  67. }
  68. }
  69. $this->initialState->provideInitialState('clients', $result);
  70. $this->initialState->provideInitialState('oauth2-doc-link', $this->urlGenerator->linkToDocs('admin-oauth2'));
  71. return new TemplateResponse(
  72. 'oauth2',
  73. 'admin',
  74. [],
  75. ''
  76. );
  77. }
  78. public function getSection(): string {
  79. return 'security';
  80. }
  81. public function getPriority(): int {
  82. return 100;
  83. }
  84. }