From 325932dc5a8da2d493808ddb385c6b05ae5be8ee Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 16 Mar 2024 12:54:17 +0000 Subject: [PATCH] Internal support for Bluesky tokens --- src/App.php | 13 ++++-- src/Protocol/ATProtocol/DID.php | 81 +++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 src/Protocol/ATProtocol/DID.php diff --git a/src/App.php b/src/App.php index bcd49ff8c3..2e19841a1b 100644 --- a/src/App.php +++ b/src/App.php @@ -43,6 +43,7 @@ use Friendica\Model\Contact; use Friendica\Model\Profile; use Friendica\Module\Special\HTTPException as ModuleHTTPException; use Friendica\Network\HTTPException; +use Friendica\Protocol\ATProtocol\DID; use Friendica\Util\DateTimeFormat; use Friendica\Util\HTTPInputData; use Friendica\Util\HTTPSignature; @@ -565,8 +566,8 @@ class App */ public function runFrontend(App\Router $router, IManagePersonalConfigValues $pconfig, Authentication $auth, App\Page $page, Nav $nav, ModuleHTTPException $httpException, HTTPInputData $httpInput, float $start_time, array $server) { - $requeststring = ($_SERVER['REQUEST_METHOD'] ?? '') . ' ' . ($_SERVER['REQUEST_URI'] ?? '') . ' ' . ($_SERVER['SERVER_PROTOCOL'] ?? ''); - $this->logger->debug('Request received', ['address' => $_SERVER['REMOTE_ADDR'] ?? '', 'request' => $requeststring, 'referer' => $_SERVER['HTTP_REFERER'] ?? '', 'user-agent' => $_SERVER['HTTP_USER_AGENT'] ?? '']); + $requeststring = ($server['REQUEST_METHOD'] ?? '') . ' ' . ($server['REQUEST_URI'] ?? '') . ' ' . ($server['SERVER_PROTOCOL'] ?? ''); + $this->logger->debug('Request received', ['address' => $server['REMOTE_ADDR'] ?? '', 'request' => $requeststring, 'referer' => $server['HTTP_REFERER'] ?? '', 'user-agent' => $server['HTTP_USER_AGENT'] ?? '']); $request_start = microtime(true); $this->profiler->set($start_time, 'start'); @@ -593,8 +594,10 @@ class App Core\Hook::callAll('init_1'); } + DID::routeRequest($this->args->getCommand(), $server); + if ($this->mode->isNormal() && !$this->mode->isBackend()) { - $requester = HTTPSignature::getSigner('', $_SERVER); + $requester = HTTPSignature::getSigner('', $server); if (!empty($requester)) { Profile::addVisitorCookieForHandle($requester); } @@ -716,10 +719,10 @@ class App $response = $page->run($this, $this->baseURL, $this->args, $this->mode, $response, $this->l10n, $this->profiler, $this->config, $pconfig, $nav, $this->session->getLocalUserId()); } - $this->logger->debug('Request processed sucessfully', ['response' => $response->getStatusCode(), 'address' => $_SERVER['REMOTE_ADDR'] ?? '', 'request' => $requeststring, 'referer' => $_SERVER['HTTP_REFERER'] ?? '', 'user-agent' => $_SERVER['HTTP_USER_AGENT'] ?? '', 'duration' => number_format(microtime(true) - $request_start, 3)]); + $this->logger->debug('Request processed sucessfully', ['response' => $response->getStatusCode(), 'address' => $server['REMOTE_ADDR'] ?? '', 'request' => $requeststring, 'referer' => $server['HTTP_REFERER'] ?? '', 'user-agent' => $server['HTTP_USER_AGENT'] ?? '', 'duration' => number_format(microtime(true) - $request_start, 3)]); System::echoResponse($response); } catch (HTTPException $e) { - $this->logger->debug('Request processed with exception', ['response' => $e->getCode(), 'address' => $_SERVER['REMOTE_ADDR'] ?? '', 'request' => $requeststring, 'referer' => $_SERVER['HTTP_REFERER'] ?? '', 'user-agent' => $_SERVER['HTTP_USER_AGENT'] ?? '', 'duration' => number_format(microtime(true) - $request_start, 3)]); + $this->logger->debug('Request processed with exception', ['response' => $e->getCode(), 'address' => $server['REMOTE_ADDR'] ?? '', 'request' => $requeststring, 'referer' => $server['HTTP_REFERER'] ?? '', 'user-agent' => $server['HTTP_USER_AGENT'] ?? '', 'duration' => number_format(microtime(true) - $request_start, 3)]); $httpException->rawContent($e); } $page->logRuntime($this->config, 'runFrontend'); diff --git a/src/Protocol/ATProtocol/DID.php b/src/Protocol/ATProtocol/DID.php new file mode 100644 index 0000000000..465eee82dc --- /dev/null +++ b/src/Protocol/ATProtocol/DID.php @@ -0,0 +1,81 @@ +. + * + */ + +namespace Friendica\Protocol\ATProtocol; + +use Friendica\Core\System; +use Friendica\Database\DBA; +use Friendica\DI; +use Friendica\Network\HTTPException; + +/** + * This class handles DID related activities from the AT Protocol + */ +class DID +{ + /** + * Routes AT Protocol DID requests + * + * @param string $path + * @param array $server + * @return void + */ + public static function routeRequest(string $path, array $server) + { + $host = DI::baseUrl()->getHost(); + + if (($host == $server['SERVER_NAME']) || !strpos($server['SERVER_NAME'], '.' . $host)) { + return; + } + + if (!DI::config()->get('bluesky', 'friendica_handles')) { + throw new HTTPException\NotFoundException(); + } + + if (!in_array($path, ['.well-known/atproto-did', ''])) { + throw new HTTPException\NotFoundException(); + } + + $nick = str_replace('.' . $host, '', $server['SERVER_NAME']); + + $user = DBA::selectFirst('user', ['uid'], ['nickname' => $nick, 'verified' => true, 'blocked' => false, 'account_removed' => false, 'account_expired' => false]); + if (empty($user['uid'])) { + throw new HTTPException\NotFoundException(); + } + + if (!DI::pConfig()->get($user['uid'], 'bluesky', 'friendica_handle')) { + throw new HTTPException\NotFoundException(); + } + + if ($path == '') { + System::externalRedirect(DI::baseUrl() . '/profile/' . urlencode($nick), 0); + } + + $did = DI::pConfig()->get($user['uid'], 'bluesky', 'did'); + if (empty($did)) { + throw new HTTPException\NotFoundException(); + } + + header('Content-Type: text/plain'); + echo $did; + System::exit(); + } +}