diff options
author | Maxence Lange <maxence@artificial-owl.com> | 2022-11-03 12:45:56 -0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-03 12:45:56 -0100 |
commit | 91158956b795022bb9ae639ad9b12f71212ea4de (patch) | |
tree | 224d8731624003ef1d4b7b5647bda5c6ce5ede80 /lib/WellKnown/WebfingerHandler.php | |
parent | d573961576b23935811b3ceaa2118de3543c47fa (diff) | |
parent | 06218acd52389353d3568f6b303bb89591bbc4fb (diff) |
Merge pull request #1508 from nextcloud/enh/noid/well-known-services
manage multiple well-known services
Diffstat (limited to 'lib/WellKnown/WebfingerHandler.php')
-rw-r--r-- | lib/WellKnown/WebfingerHandler.php | 95 |
1 files changed, 90 insertions, 5 deletions
diff --git a/lib/WellKnown/WebfingerHandler.php b/lib/WellKnown/WebfingerHandler.php index 8eda7864..7cabf75c 100644 --- a/lib/WellKnown/WebfingerHandler.php +++ b/lib/WellKnown/WebfingerHandler.php @@ -27,6 +27,8 @@ namespace OCA\Social\WellKnown; use OCA\Social\Db\CacheActorsRequest; use OCA\Social\Exceptions\CacheActorDoesNotExistException; +use OCA\Social\Exceptions\SocialAppConfigException; +use OCA\Social\Exceptions\UnauthorizedFediverseException; use OCA\Social\Service\CacheActorService; use OCA\Social\Service\ConfigService; use OCA\Social\Service\FediverseService; @@ -55,12 +57,51 @@ class WebfingerHandler implements IHandler { $this->configService = $configService; } - public function handle(string $service, IRequestContext $context, ?IResponse $previousResponse): ?IResponse { - // See https://docs.joinmastodon.org/spec/webfinger/ - $this->fediverseService->jailed(); - $subject = $context->getHttpRequest()->getParam('resource'); + /** + * @see https://docs.joinmastodon.org/spec/webfinger/ + * + * @param string $service + * @param IRequestContext $context + * @param IResponse|null $previousResponse + * + * @return IResponse|null + */ + public function handle( + string $service, + IRequestContext $context, + ?IResponse $previousResponse + ): ?IResponse { + try { + $this->fediverseService->jailed(); + } catch (UnauthorizedFediverseException $e) { + return null; + } + + switch (strtolower($service)) { + case 'webfinger': + return $this->handleWebfinger($context); + + case 'nodeinfo': + return $this->handleNodeInfo($context); + + case 'host-meta': + return $this->handleHostMeta($context); + } + return null; + } + + + /** + * handle request on /.well-known/webfinger + * + * @param IRequestContext $context + * + * @return IResponse|null + */ + public function handleWebfinger(IRequestContext $context): ?IResponse { + $subject = $context->getHttpRequest()->getParam('resource'); if (strpos($subject, 'acct:') === 0) { $subject = substr($subject, 5); } @@ -68,6 +109,8 @@ class WebfingerHandler implements IHandler { $actor = null; try { $actor = $this->cacheActorService->getFromLocalAccount($subject); + } catch (SocialAppConfigException $e) { + return null; } catch (CacheActorDoesNotExistException $e) { } @@ -81,7 +124,7 @@ class WebfingerHandler implements IHandler { if ($actor === null || !$actor->isLocal()) { return new JrdResponse('', Http::STATUS_NOT_FOUND); } - + // ActivityPub profile $href = $this->configService->getSocialUrl() . '@' . $actor->getPreferredUsername(); $href = rtrim($href, '/'); @@ -109,4 +152,46 @@ class WebfingerHandler implements IHandler { return $response; } + + + /** + * handle request on /.well-known/nodeinfo + * returns Json + * + * @param IRequestContext $context + * + * @return IResponse|null + */ + private function handleNodeInfo(IRequestContext $context): ?IResponse { + $response = new JrdResponse(); + $response->addLink( + 'http://nodeinfo.diaspora.software/ns/schema/2.0', + null, + $this->urlGenerator->linkToRouteAbsolute('social.OAuth.nodeinfo2') + ); + + return $response; + } + + + /** + * handle request on /.well-known/host-meta + * returns xml/xrd + * + * @param IRequestContext $context + * + * @return IResponse|null + */ + private function handleHostMeta(IRequestContext $context): ?IResponse { + $response = new XrdResponse(); + try { + $url = $this->configService->getCloudUrl(true) . '/.well-known/webfinger?resource={uri}'; + } catch (SocialAppConfigException $e) { + return null; + } + + $response->addLink('lrdd', $url); + + return $response; + } } |