|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 $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 * 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 $document */ private static function matchesRequestedDid(array $document, string $did): bool { return \is_string($document['id'] ?? NULL) && $document['id'] === $did; } /** * @return array|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 $decoded */ return $decoded; } }