OAuth2.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. * ActivityPub implementation for GNU social
  21. *
  22. * @package GNUsocial
  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;
  31. use App\Core\Event;
  32. use App\Core\Modules\Plugin;
  33. use App\Core\Router\RouteLoader;
  34. use App\Core\Router\Router;
  35. use App\Util\Common;
  36. use DateInterval;
  37. use Exception;
  38. use League\OAuth2\Server\AuthorizationServer;
  39. use League\OAuth2\Server\CryptKey;
  40. use League\OAuth2\Server\Grant\AuthCodeGrant;
  41. use Plugin\OAuth2\Controller as C;
  42. use Plugin\OAuth2\Util\ExpandedBearerTokenResponse;
  43. use XML_XRD_Element_Link;
  44. /**
  45. * Adds OAuth2 support to GNU social when enabled
  46. *
  47. * @copyright 2022 Free Software Foundation, Inc http://www.fsf.org
  48. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  49. */
  50. class OAuth2 extends Plugin
  51. {
  52. public const OAUTH_REQUEST_TOKEN_REL = 'http://apinamespace.org/oauth/request_token';
  53. public const OAUTH_ACCESS_TOKEN_REL = 'http://apinamespace.org/oauth/access_token';
  54. public const OAUTH_AUTHORIZE_REL = 'http://apinamespace.org/oauth/authorize';
  55. public static ?AuthorizationServer $authorization_server = null;
  56. public function version(): string
  57. {
  58. return '3.0.0';
  59. }
  60. /**
  61. * @throws Exception
  62. */
  63. public function onInitializePlugin()
  64. {
  65. self::$authorization_server = new AuthorizationServer(
  66. new Repository\Client,
  67. new Repository\AccessToken,
  68. new Repository\Scope,
  69. privateKey: new CryptKey(keyPath: Common::config('oauth2', 'private_key'), passPhrase: Common::config('oauth2', 'private_key_password')),
  70. encryptionKey: Common::config('oauth2', 'encryption_key'),
  71. responseType: new ExpandedBearerTokenResponse(),
  72. );
  73. self::$authorization_server->enableGrantType(
  74. new AuthCodeGrant(
  75. new Repository\AuthCode,
  76. new Repository\RefreshToken,
  77. new DateInterval('PT10M'), // ???
  78. ),
  79. new DateInterval('PT1H'),
  80. );
  81. }
  82. /**
  83. * This code executes when GNU social creates the page routing, and we hook
  84. * on this event to add our Inbox and Outbox handler for ActivityPub.
  85. *
  86. * @param RouteLoader $r the router that was initialized
  87. */
  88. public function onAddRoute(RouteLoader $r): bool
  89. {
  90. $r->connect('oauth2_mastodon_api_apps', '/api/v1/apps', C\Client::class, ['http-methods' => ['POST']]);
  91. $r->connect('oauth2_client', '/oauth/client', C\Client::class, ['http-methods' => ['POST']]);
  92. $r->connect('oauth2_authorize', '/oauth/authorize', C\Authorize::class);
  93. $r->connect('oauth2_token', '/oauth/token', C\Token::class);
  94. return Event::next;
  95. }
  96. public function onEndHostMetaLinks(array &$links): bool
  97. {
  98. $links[] = new XML_XRD_Element_Link(self::OAUTH_REQUEST_TOKEN_REL, Router::url('oauth2_client', type: Router::ABSOLUTE_URL));
  99. $links[] = new XML_XRD_Element_Link(self::OAUTH_AUTHORIZE_REL, Router::url('oauth2_authorize', type: Router::ABSOLUTE_URL));
  100. $links[] = new XML_XRD_Element_Link(self::OAUTH_ACCESS_TOKEN_REL, Router::url('oauth2_token', type: Router::ABSOLUTE_URL));
  101. return Event::next;
  102. }
  103. }