handleParser->normalize($handle); } catch (\InvalidArgumentException) { return NULL; } if (!$this->handleParser->isResolvableSyntax($handle)) { return NULL; } $cacheKey = CacheKey::build($this->cacheKeyPrefix, $handle); if ($this->cache !== NULL && $this->cache->has($cacheKey)) { $cached = $this->cache->get($cacheKey); if (\is_string($cached) && $this->didParser->isValid($cached)) { return $cached; } } $dns = $this->resolveDidFromDns($handle); if ($dns['ambiguous']) { return NULL; } $did = $dns['did'] ?? $this->resolveDidFromWellKnown($handle); if ($did === NULL) { return NULL; } $this->cache?->set($cacheKey, $did, $this->cacheTtl); return $did; } /** * Resolves the DID controlled by a handle or throws on failure. */ public function resolveDidOrFail(string $handle): string { return $this->resolveDid($handle) ?? throw new IdentityResolutionException($handle, 'handle'); } /** * @return array{did: ?string, ambiguous: bool} */ private function resolveDidFromDns(string $handle): array { $dids = []; foreach ($this->dnsTxtResolver->resolveTxt('_atproto.' . $handle) as $txt) { $txt = \trim($txt, " \t\n\r\0\x0B\""); if (!\str_starts_with($txt, 'did=')) { continue; } $did = \substr($txt, 4); if ($this->didParser->isValid($did)) { $dids[$this->didParser->normalize($did)] = TRUE; } } if (\count($dids) > 1) { return ['did' => NULL, 'ambiguous' => TRUE]; } $did = \array_key_first($dids); return ['did' => \is_string($did) ? $did : NULL, 'ambiguous' => FALSE]; } private function resolveDidFromWellKnown(string $handle): ?string { $url = 'https://' . $handle . '/.well-known/atproto-did'; if (!$this->urlPolicy->isAllowedHttpsUrl($url)) { return NULL; } $request = $this->requestFactory ->createRequest('GET', $url) ->withHeader('Accept', 'text/plain'); try { $response = $this->httpClient->sendRequest($request); } catch (ClientExceptionInterface) { return NULL; } if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) { return NULL; } try { $did = \trim(BoundedResponseBody::read($response, self::MAX_WELL_KNOWN_BYTES)); } catch (\RuntimeException) { return NULL; } if (!$this->didParser->isValid($did)) { return NULL; } return $this->didParser->normalize($did); } }