summaryrefslogtreecommitdiffstats
path: root/lib/WellKnown
diff options
context:
space:
mode:
authorCarl Schwan <carl@carlschwan.eu>2022-04-20 23:22:14 +0200
committerCarl Schwan <carl@carlschwan.eu>2022-04-21 14:25:54 +0200
commit8bc648a9726217b9994a67c4dacdb7e7a8000fd7 (patch)
tree988876f31378527c64ef8a7f91e222257fa2f7bd /lib/WellKnown
parentf7b8e70aa8a016103b0e3fe61e0f3e08413b6863 (diff)
Fix JrdResponse not containing template
For now this is modified JrdResponse from https://github.com/nextcloud/server/pull/26178 Let's hope that we can switch to the OCP one in NC 25 Signed-off-by: Carl Schwan <carl@carlschwan.eu>
Diffstat (limited to 'lib/WellKnown')
-rw-r--r--lib/WellKnown/JrdResponse.php174
-rw-r--r--lib/WellKnown/WebfingerHandler.php13
2 files changed, 183 insertions, 4 deletions
diff --git a/lib/WellKnown/JrdResponse.php b/lib/WellKnown/JrdResponse.php
new file mode 100644
index 00000000..030e6d05
--- /dev/null
+++ b/lib/WellKnown/JrdResponse.php
@@ -0,0 +1,174 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @author Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @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 <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCA\Social\WellKnown;
+
+use OCP\AppFramework\Http\JSONResponse;
+use OCP\AppFramework\Http\Response;
+use OCP\Http\WellKnown\IResponse;
+use function array_filter;
+
+/**
+ * A JSON Document Format (JDF) response to a well-known request
+ *
+ * @ref https://tools.ietf.org/html/rfc6415#appendix-A
+ * @ref https://tools.ietf.org/html/rfc7033#section-4.4
+ */
+final class JrdResponse implements IResponse {
+ private string $subject;
+ private ?string $expires = null;
+
+ /** @var string[] */
+ private array $aliases = [];
+
+ /** @var (string|null)[] */
+ private array $properties = [];
+
+ /** @var mixed[] */
+ private array $links = [];
+
+ /**
+ * @param string $subject https://tools.ietf.org/html/rfc7033#section-4.4.1
+ *
+ * @since 21.0.0
+ */
+ public function __construct(string $subject) {
+ $this->subject = $subject;
+ }
+
+ /**
+ * @param string $expires
+ *
+ * @return $this
+ *
+ * @since 21.0.0
+ */
+ public function setExpires(string $expires): self {
+ $this->expires = $expires;
+
+ return $this;
+ }
+
+ /**
+ * Add an alias
+ *
+ * @ref https://tools.ietf.org/html/rfc7033#section-4.4.2
+ *
+ * @param string $alias
+ *
+ * @return $this
+ *
+ * @since 21.0.0
+ */
+ public function addAlias(string $alias): self {
+ $this->aliases[] = $alias;
+
+ return $this;
+ }
+
+ /**
+ * Add a property
+ *
+ * @ref https://tools.ietf.org/html/rfc7033#section-4.4.3
+ *
+ * @param string $property
+ * @param string|null $value
+ *
+ * @return $this
+ *
+ * @since 21.0.0
+ */
+ public function addProperty(string $property, ?string $value): self {
+ $this->properties[$property] = $value;
+
+ return $this;
+ }
+
+ /**
+ * Add a link
+ *
+ * @ref https://tools.ietf.org/html/rfc7033#section-8.4
+ *
+ * @param string $rel https://tools.ietf.org/html/rfc7033#section-4.4.4.1
+ * @param string|null $type https://tools.ietf.org/html/rfc7033#section-4.4.4.2
+ * @param string|null $href https://tools.ietf.org/html/rfc7033#section-4.4.4.3
+ * @param string[]|null $titles https://tools.ietf.org/html/rfc7033#section-4.4.4.4
+ * @param string[]|null $properties https://tools.ietf.org/html/rfc7033#section-4.4.4.5
+ * @param string[] $entries
+ *
+ * @psalm-param array<string,(string|null)>|null $properties https://tools.ietf.org/html/rfc7033#section-4.4.4.5
+ *
+ * @return JrdResponse
+ * @since 21.0.0
+ */
+ public function addLink(string $rel,
+ ?string $type,
+ ?string $href,
+ ?array $titles = [],
+ ?array $properties = [],
+ array $entries = []
+ ): self {
+ $this->links[] = array_filter(
+ array_merge(
+ [
+ 'rel' => $rel,
+ 'type' => $type,
+ 'href' => $href,
+ 'titles' => $titles,
+ 'properties' => $properties
+ ],
+ $entries
+ )
+ );
+
+ return $this;
+ }
+
+ /**
+ * @since 21.0.0
+ */
+ public function toHttpResponse(): Response {
+ return new JSONResponse(array_filter([
+ 'subject' => $this->subject,
+ 'expires' => $this->expires,
+ 'aliases' => $this->aliases,
+ 'properties' => $this->properties,
+ 'links' => $this->links,
+ ]));
+ }
+
+ /**
+ * Does this response have any data attached to it?
+ *
+ * @since 21.0.0
+ */
+ public function isEmpty(): bool {
+ return $this->expires === null
+ && empty($this->aliases)
+ && empty($this->properties)
+ && empty($this->links);
+ }
+}
diff --git a/lib/WellKnown/WebfingerHandler.php b/lib/WellKnown/WebfingerHandler.php
index a4f4b228..89de7ca1 100644
--- a/lib/WellKnown/WebfingerHandler.php
+++ b/lib/WellKnown/WebfingerHandler.php
@@ -33,7 +33,6 @@ use OCA\Social\Service\FediverseService;
use OCP\Http\WellKnown\IHandler;
use OCP\Http\WellKnown\IRequestContext;
use OCP\Http\WellKnown\IResponse;
-use OCP\Http\WellKnown\JrdResponse;
use OCP\IURLGenerator;
class WebfingerHandler implements IHandler {
@@ -90,9 +89,15 @@ class WebfingerHandler implements IHandler {
$response->addLink('http://webfinger.net/rel/profile-page', 'text/html', $profilePageUrl);
// Ostatus subscribe url
- // JrdResponse doesn't support template
- // $subscribe = $this->urlGenerator->linkToRouteAbsolute('social.OStatus.subscribe') . '?uri={uri}';
- // $response->addLink('http://ostatus.org/schema/1.0/subscribe', $subscribe);
+ $subscribe = $this->urlGenerator->linkToRouteAbsolute('social.OStatus.subscribe') . '?uri={uri}';
+ $response->addLink(
+ 'http://ostatus.org/schema/1.0/subscribe',
+ '',
+ '',
+ null,
+ null,
+ ['template' => $subscribe]
+ );
return $response;
}