Client.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. /**
  20. * OAuth2 implementation for GNU social
  21. *
  22. * @package OAuth2
  23. * @category API
  24. *
  25. * @author Diogo Peralta Cordeiro <mail@diogo.site>
  26. * @author Hugo Sales <hugo@hsal.es>
  27. * @copyright 2022 Free Software Foundation, Inc http://www.fsf.org
  28. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  29. */
  30. namespace Plugin\OAuth2\Controller;
  31. use App\Core\Controller;
  32. use App\Core\DB;
  33. use function App\Core\I18n\_m;
  34. use App\Core\Log;
  35. use App\Util\Exception\ClientException;
  36. use App\Util\Exception\ServerException;
  37. use Exception;
  38. use Plugin\OAuth2\Entity;
  39. use Symfony\Component\HttpFoundation\JsonResponse;
  40. use Symfony\Component\HttpFoundation\Request;
  41. /**
  42. * Client Management Endpoint
  43. *
  44. * @copyright 2022 Free Software Foundation, Inc http://www.fsf.org
  45. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  46. */
  47. class Client extends Controller
  48. {
  49. /**
  50. * @throws ClientException
  51. * @throws Exception
  52. * @throws ServerException
  53. */
  54. public function onPost(Request $request): JsonResponse
  55. {
  56. Log::debug('OAuth2 Apps: Received a POST request.');
  57. Log::debug('OAuth2 Apps: Request content: ', [$body = $request->getContent()]);
  58. $args = json_decode($body, true);
  59. if (\is_null($args) || !\array_key_exists('redirect_uris', $args)) {
  60. throw new ClientException(_m('Invalid request'), code: 400);
  61. }
  62. $identifier = hash('md5', random_bytes(16));
  63. // Random string Length should be between 43 and 128, thus 57
  64. $secret = hash('sha256', random_bytes(57));
  65. // TODO more validation
  66. $client = Entity\Client::create([
  67. 'id' => $identifier,
  68. 'secret' => $secret,
  69. 'active' => true,
  70. 'plain_pcke' => false,
  71. 'is_confidential' => true,
  72. 'redirect_uris' => $args['redirect_uris'],
  73. 'grants' => 'client_credentials',
  74. 'scopes' => $args['scopes'] ?? 'read',
  75. 'client_name' => $args['client_name'],
  76. 'website' => $args['website'] ?? null,
  77. ]);
  78. DB::persist($client);
  79. DB::flush();
  80. Log::debug('OAuth2 Apps: Created App: ', [$client]);
  81. $app_response = [
  82. 'id' => 42, // TODO ???
  83. 'name' => $client->getName(),
  84. 'website' => $client->getWebsite(),
  85. 'redirect_uri' => $client->getRedirectUri(),
  86. 'client_id' => $client->getIdentifier(),
  87. 'client_secret' => $client->getSecret(),
  88. ];
  89. Log::debug('OAuth2 Apps: Create App response: ', [$app_response]);
  90. // Success
  91. return new JsonResponse($app_response, status: 200, headers: ['content_type' => 'application/json; charset=utf-8']);
  92. }
  93. }