Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Identity;
use Wotnak\Atproto\Identifier\HandleParser;
/** * Generic DID document helpers for AT Protocol identity verification. * * @api */final readonly class DidDocument {
/** * @param list<string> $alsoKnownAs * @param list<AtprotoServiceEndpoint> $services * @param array<string, mixed> $raw */ public function __construct( public string $id, public array $alsoKnownAs, public array $services, public array $raw, ) {}
/** * Parses a DID document from decoded JSON data. * * @param array<string, mixed> $document */ public static function fromArray(array $document): ?self { if (!\is_string($document['id'] ?? NULL)) { return NULL; }
$alsoKnownAs = []; if (isset($document['alsoKnownAs']) && \is_array($document['alsoKnownAs'])) { foreach ($document['alsoKnownAs'] as $value) { if (!\is_string($value)) { continue; }
$alsoKnownAs[] = $value; } }
$services = []; if (isset($document['service']) && \is_array($document['service'])) { foreach ($document['service'] as $service) { if (!\is_array($service)) { continue; }
/** @var array<string, mixed> $service */ $endpoint = AtprotoServiceEndpoint::fromArray($service); if ($endpoint !== NULL) { $services[] = $endpoint; } } }
return new self($document['id'], $alsoKnownAs, $services, $document); }
/** * Returns whether the document claims the normalized handle. */ public function hasAlsoKnownAsHandle(string $handle): bool { $parser = new HandleParser(); try { $expected = $parser->normalize($handle); } catch (\InvalidArgumentException) { return FALSE; }
foreach ($this->alsoKnownAs as $uri) { if (!\str_starts_with($uri, 'at://')) { continue; } $claimed = \substr($uri, \strlen('at://')); if ($claimed !== \trim($claimed) || !$parser->isValid($claimed)) { continue; }
// ATProto uses only the first syntactically valid handle claim. return $parser->normalize($claimed) === $expected; } return FALSE; }
/** * Returns the document PDS service endpoint when present. */ public function getPdsEndpoint(): ?string { foreach ($this->services as $service) { if ( $service->type === 'AtprotoPersonalDataServer' && self::isPdsServiceId($service->id) && $this->isValidPdsEndpoint($service->serviceEndpoint) ) { return $service->serviceEndpoint; } } return NULL; }
private static function isPdsServiceId(string $id): bool { if ($id === '#atproto_pds') { return TRUE; } return ( \str_ends_with($id, '#atproto_pds') && \preg_match('/^[A-Za-z][A-Za-z0-9+.-]*:/D', $id) === 1 && \preg_match('/[\x00-\x20\x7F]/', $id) !== 1 && \preg_match('/%(?![0-9A-Fa-f]{2})/', $id) !== 1 ); }
private static function isValidPdsEndpoint(string $endpoint): bool { try { $parts = \parse_url($endpoint); } // ValueError is an expected invalid-input result at this API boundary. // @mago-expect analysis:avoid-catching-error catch (\ValueError) { return FALSE; } return ( \is_array($parts) && \strtolower($parts['scheme'] ?? '') === 'https' && \is_string($parts['host'] ?? NULL) && !isset($parts['user']) && !isset($parts['pass']) && !isset($parts['path']) && !isset($parts['query']) && !isset($parts['fragment']) ); }
}