Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Identity;
use Psr\Http\Client\ClientExceptionInterface;use Psr\Http\Client\ClientInterface;use Psr\Http\Message\RequestFactoryInterface;use Psr\SimpleCache\CacheInterface;use Wotnak\Atproto\Cache\CacheKey;use Wotnak\Atproto\Exception\IdentityResolutionException;use Wotnak\Atproto\Http\BoundedResponseBody;use Wotnak\Atproto\Identifier\DidParser;use Wotnak\Atproto\Identifier\HandleParser;use Wotnak\Atproto\Security\OutboundUrlPolicy;use Wotnak\Atproto\Security\OutboundUrlPolicyInterface;
/** * Resolves DID documents for supported AT Protocol DID methods. * * @api */final class DidResolver {
private const MAX_DOCUMENT_BYTES = 1_048_576;
public function __construct( private readonly ClientInterface $httpClient, private readonly RequestFactoryInterface $requestFactory, private readonly ?CacheInterface $cache = NULL, private readonly int|\DateInterval|null $cacheTtl = 3600, private readonly OutboundUrlPolicyInterface $urlPolicy = new OutboundUrlPolicy(), private readonly HandleParser $handleParser = new HandleParser(), private readonly string $plcDirectoryUrl = 'https://plc.directory', private readonly string $cacheKeyPrefix = 'atproto.did_document', private readonly DidParser $didParser = new DidParser(), ) {}
/** * Resolves raw DID document data, returning null on failure. * * @return array<string, mixed>|null * DID document data, or NULL if it cannot be resolved. */ public function resolve(string $did): ?array { if (!DidParser::isValid($did) || !$this->didParser->isSupported($did)) { return NULL; }
$cacheKey = CacheKey::build($this->cacheKeyPrefix, $did); if ($this->cache !== NULL && $this->cache->has($cacheKey)) { $cached = $this->cache->get($cacheKey); if (\is_array($cached)) { /** @var array<string, mixed> $cached */ if ($this->matchesRequestedDid($cached, $did)) { return $cached; } } }
$document = NULL; if (\str_starts_with($did, 'did:plc:')) { $document = $this->fetchJson(\rtrim($this->plcDirectoryUrl, '/') . '/' . \rawurlencode($did)); } elseif (\str_starts_with($did, 'did:web:')) { $url = $this->didWebDocumentUrl($did); $document = $url !== NULL ? $this->fetchJson($url) : NULL; }
if ($document !== NULL && !$this->matchesRequestedDid($document, $did)) { $document = NULL; }
if ($document !== NULL) { $this->cache?->set($cacheKey, $document, $this->cacheTtl); } return $document; }
/** * Resolves raw DID document data or throws on failure. * * @return array<string, mixed> * DID document data. */ public function resolveOrFail(string $did): array { return $this->resolve($did) ?? throw new IdentityResolutionException($did, 'DID'); }
/** * Resolves the document. */ public function resolveDocument(string $did): ?DidDocument { $document = $this->resolve($did); return $document !== NULL ? DidDocument::fromArray($document) : NULL; }
/** * Resolves a parsed DID document or throws on failure. */ public function resolveDocumentOrFail(string $did): DidDocument { return $this->resolveDocument($did) ?? throw new IdentityResolutionException($did, 'DID document'); }
private function didWebDocumentUrl(string $did): ?string { $identifier = \substr($did, \strlen('did:web:')); if ($identifier === '') { return NULL; }
$segments = \explode(':', $identifier); $host = \rawurldecode(\array_shift($segments)); if ( $host === '' || $segments !== [] || $host !== \strtolower($host) || \strpbrk($host, ":/?#@ \t\r\n") !== FALSE || !$this->handleParser->isResolvableSyntax($host) ) { return NULL; } if (!$this->urlPolicy->isAllowedHttpsUrl('https://' . $host)) { return NULL; }
return 'https://' . $host . '/.well-known/did.json'; }
/** * @param array<string, mixed> $document */ private static function matchesRequestedDid(array $document, string $did): bool { return \is_string($document['id'] ?? NULL) && $document['id'] === $did; }
/** * @return array<string, mixed>|null * Decoded JSON response, or NULL. */ private function fetchJson(string $url): ?array { if (!$this->urlPolicy->isAllowedHttpsUrl($url)) { return NULL; }
$request = $this->requestFactory ->createRequest('GET', $url) ->withHeader('Accept', 'application/json');
try { $response = $this->httpClient->sendRequest($request); } catch (ClientExceptionInterface) { return NULL; }
if ($response->getStatusCode() !== 200) { return NULL; }
try { $body = BoundedResponseBody::read($response, self::MAX_DOCUMENT_BYTES); $decoded = \json_decode($body, TRUE, 64); } catch (\RuntimeException) { return NULL; } if (!\is_array($decoded)) { return NULL; } /** @var array<string, mixed> $decoded */ return $decoded; }
}