Internal support for Bluesky tokens
authorMichael <heluecht@pirati.ca>
Sat, 16 Mar 2024 12:54:17 +0000 (12:54 +0000)
committerMichael <heluecht@pirati.ca>
Thu, 21 Mar 2024 21:33:12 +0000 (21:33 +0000)
src/App.php
src/Protocol/ATProtocol/DID.php [new file with mode: 0644]

index bcd49ff..2e19841 100644 (file)
@@ -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 (file)
index 0000000..465eee8
--- /dev/null
@@ -0,0 +1,81 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2024, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+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();
+       }
+}