returns 404 on missing Actor

Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
pull/1506/head
Maxence Lange 2022-11-02 18:57:04 -01:00
rodzic ce9c6c05d6
commit d3de0bfac4
2 zmienionych plików z 48 dodań i 23 usunięć

Wyświetl plik

@ -26,6 +26,8 @@ declare(strict_types=1);
namespace OCA\Social\WellKnown; namespace OCA\Social\WellKnown;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\Response; use OCP\AppFramework\Http\Response;
use OCP\Http\WellKnown\IResponse; use OCP\Http\WellKnown\IResponse;
@ -40,6 +42,7 @@ use function array_filter;
final class JrdResponse implements IResponse { final class JrdResponse implements IResponse {
private string $subject; private string $subject;
private ?string $expires = null; private ?string $expires = null;
private int $httpCode;
/** @var string[] */ /** @var string[] */
private array $aliases = []; private array $aliases = [];
@ -55,8 +58,9 @@ final class JrdResponse implements IResponse {
* *
* @since 21.0.0 * @since 21.0.0
*/ */
public function __construct(string $subject) { public function __construct(string $subject, int $httpCode = Http::STATUS_OK) {
$this->subject = $subject; $this->subject = $subject;
$this->httpCode = $httpCode;
} }
/** /**
@ -72,6 +76,13 @@ final class JrdResponse implements IResponse {
return $this; return $this;
} }
public function setHttpCode(int $httpCode): self {
$this->httpCode = $httpCode;
return $this;
}
/** /**
* Add an alias * Add an alias
* *
@ -119,17 +130,19 @@ final class JrdResponse implements IResponse {
* @param string[]|null $properties https://tools.ietf.org/html/rfc7033#section-4.4.4.5 * @param string[]|null $properties https://tools.ietf.org/html/rfc7033#section-4.4.4.5
* @param string[] $entries * @param string[] $entries
* *
* @psalm-param array<string,(string|null)>|null $properties https://tools.ietf.org/html/rfc7033#section-4.4.4.5 * @psalm-param array<string,(string|null)>|null $properties
* https://tools.ietf.org/html/rfc7033#section-4.4.4.5
* *
* @return JrdResponse * @return JrdResponse
* @since 21.0.0 * @since 21.0.0
*/ */
public function addLink(string $rel, public function addLink(
?string $type, string $rel,
?string $href, ?string $type,
?array $titles = [], ?string $href,
?array $properties = [], ?array $titles = [],
array $entries = [] ?array $properties = [],
array $entries = []
): self { ): self {
$this->links[] = array_filter( $this->links[] = array_filter(
array_merge( array_merge(
@ -151,13 +164,17 @@ final class JrdResponse implements IResponse {
* @since 21.0.0 * @since 21.0.0
*/ */
public function toHttpResponse(): Response { public function toHttpResponse(): Response {
return new JSONResponse(array_filter([ $data = array_filter(
'subject' => $this->subject, [
'expires' => $this->expires, 'subject' => $this->subject,
'aliases' => $this->aliases, 'expires' => $this->expires,
'properties' => $this->properties, 'aliases' => $this->aliases,
'links' => $this->links, 'properties' => $this->properties,
])); 'links' => $this->links,
]
);
return (empty($data)) ? new DataResponse('', $this->httpCode) : new JSONResponse($data, $this->httpCode);
} }
/** /**
@ -167,8 +184,8 @@ final class JrdResponse implements IResponse {
*/ */
public function isEmpty(): bool { public function isEmpty(): bool {
return $this->expires === null return $this->expires === null
&& empty($this->aliases) && empty($this->aliases)
&& empty($this->properties) && empty($this->properties)
&& empty($this->links); && empty($this->links);
} }
} }

Wyświetl plik

@ -30,6 +30,7 @@ use OCA\Social\Exceptions\CacheActorDoesNotExistException;
use OCA\Social\Service\CacheActorService; use OCA\Social\Service\CacheActorService;
use OCA\Social\Service\ConfigService; use OCA\Social\Service\ConfigService;
use OCA\Social\Service\FediverseService; use OCA\Social\Service\FediverseService;
use OCP\AppFramework\Http;
use OCP\Http\WellKnown\IHandler; use OCP\Http\WellKnown\IHandler;
use OCP\Http\WellKnown\IRequestContext; use OCP\Http\WellKnown\IRequestContext;
use OCP\Http\WellKnown\IResponse; use OCP\Http\WellKnown\IResponse;
@ -64,20 +65,27 @@ class WebfingerHandler implements IHandler {
$subject = substr($subject, 5); $subject = substr($subject, 5);
} }
$actor = null;
try { try {
$actor = $this->cacheActorService->getFromLocalAccount($subject); $actor = $this->cacheActorService->getFromLocalAccount($subject);
} catch (CacheActorDoesNotExistException $e) { } catch (CacheActorDoesNotExistException $e) {
$actor = $this->cacheActorsRequest->getFromId($subject); }
if (!$actor->isLocal()) {
throw new CacheActorDoesNotExistException(); if ($actor === null) {
try {
$actor = $this->cacheActorsRequest->getFromId($subject);
} catch (CacheActorDoesNotExistException $e) {
} }
} }
$response = new JrdResponse($subject); if ($actor === null || !$actor->isLocal()) {
return new JrdResponse('', Http::STATUS_NOT_FOUND);
}
// ActivityPub profile // ActivityPub profile
$href = $this->configService->getSocialUrl() . '@' . $actor->getPreferredUsername(); $href = $this->configService->getSocialUrl() . '@' . $actor->getPreferredUsername();
$href = rtrim($href, '/'); $href = rtrim($href, '/');
$response = new JrdResponse($subject);
$response->addAlias($href); $response->addAlias($href);
$response->addLink('self', 'application/activity+json', $href); $response->addLink('self', 'application/activity+json', $href);