Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166<?php
declare(strict_types=1);
namespace Wotnak\Atproto;
use Psr\SimpleCache\CacheInterface;use Wotnak\Atproto\Cache\CacheKey;use Wotnak\Atproto\Client\XrpcClient;use Wotnak\Atproto\Client\XrpcOptions;use Wotnak\Atproto\Exception\XrpcException;use Wotnak\Atproto\Identifier\AtUriParser;use Wotnak\Atproto\Identifier\CidParser;use Wotnak\Atproto\Identifier\DidParser;use Wotnak\Atproto\Identifier\NsidParser;use Wotnak\Atproto\Identity\DidResolver;use Wotnak\Atproto\Identity\DnsTxtResolverInterface;use Wotnak\Atproto\Lexicon\LexiconDocument;use Wotnak\Atproto\Lexicon\LexiconParser;use Wotnak\Atproto\Lexicon\LexiconRepositoryInterface;
/** * Resolves authenticated Lexicon publication records from the AT network. * * @api */final readonly class AuthoritativeLexiconRepository implements LexiconRepositoryInterface {
public function __construct( private DnsTxtResolverInterface $dnsTxtResolver, private DidResolver $didResolver, private XrpcClient $xrpcClient, private ?CacheInterface $cache = NULL, private int|\DateInterval|null $cacheTtl = 86_400, private LexiconParser $lexiconParser = new LexiconParser(), private NsidParser $nsidParser = new NsidParser(), private DidParser $didParser = new DidParser(), private AtUriParser $atUriParser = new AtUriParser(), private CidParser $cidParser = new CidParser(), private string $cacheKeyPrefix = 'atproto.lexicon_document', ) {}
/** * Returns the document. */ public function getDocument(string $nsid): ?LexiconDocument { $id = \explode('#', $nsid, 2)[0]; if (!$this->nsidParser->isValid($id)) { return NULL; } $id = $this->nsidParser->normalize($id); $cacheKey = CacheKey::build($this->cacheKeyPrefix, $id); $cached = $this->cache?->get($cacheKey); if (\is_array($cached)) { /** @var array<string, mixed> $cached */ $document = $this->parseVerifiedSchema($cached, $id); if ($document !== NULL) { return $document; } }
$publisherDid = $this->resolvePublisherDid($id); if ($publisherDid === NULL) { return NULL; } $didDocument = $this->didResolver->resolveDocument($publisherDid); $pds = $didDocument?->getPdsEndpoint(); if ($pds === NULL) { return NULL; }
try { $response = $this->xrpcClient->get( 'com.atproto.repo.getRecord', [ 'repo' => $publisherDid, 'collection' => 'com.atproto.lexicon.schema', 'rkey' => $id, ], new XrpcOptions(serviceUrl: $pds, maxResponseBytes: 1_048_576), ); } catch (XrpcException) { return NULL; } catch (\InvalidArgumentException) { return NULL; }
$schema = $this->verifyRecordResponse($response, $publisherDid, $id); if ($schema === NULL) { return NULL; } $document = $this->parseVerifiedSchema($schema, $id); if ($document === NULL) { return NULL; } $this->cache?->set($cacheKey, $schema, $this->cacheTtl); return $document; }
private function resolvePublisherDid(string $nsid): ?string { $segments = \explode('.', $nsid); \array_pop($segments); $dnsName = '_lexicon.' . \implode('.', \array_reverse($segments)); $dids = []; foreach ($this->dnsTxtResolver->resolveTxt($dnsName) as $record) { if (!\str_starts_with($record, 'did=')) { continue; } $did = \substr($record, 4); if (!DidParser::isValid($did) || !$this->didParser->isSupported($did)) { return NULL; } $dids[$did] = TRUE; } return \count($dids) === 1 ? \array_key_first($dids) : NULL; }
/** * @param array<string, mixed> $response * @return array<string, mixed>|null */ private function verifyRecordResponse(array $response, string $publisherDid, string $nsid): ?array { if ( !\is_string($response['uri'] ?? NULL) || !\is_string($response['cid'] ?? NULL) || !$this->cidParser->isValid($response['cid']) || !\is_array($response['value'] ?? NULL) ) { return NULL; } try { $uri = $this->atUriParser->parse($response['uri']); } catch (\InvalidArgumentException) { return NULL; } if ( $uri->repo !== $publisherDid || $uri->collection !== 'com.atproto.lexicon.schema' || $uri->rkey !== $nsid ) { return NULL; } /** @var array<string, mixed> $schema */ $schema = $response['value']; if (($schema['$type'] ?? NULL) !== 'com.atproto.lexicon.schema') { return NULL; } unset($schema['$type']); return $schema; }
/** * @param array<string, mixed> $schema */ private function parseVerifiedSchema(array $schema, string $nsid): ?LexiconDocument { $result = $this->lexiconParser->parse($schema); return $result->value instanceof LexiconDocument && $result->value->id === $nsid ? $result->value : NULL; }
}