Class: AtprotoAuth::Identity::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/atproto_auth/identity/document.rb

Overview

Represents and validates a DID Document in the AT Protocol.

DID Documents contain critical service information about user accounts, including:

  • The Personal Data Server (PDS) hosting the account

  • Associated handles for the account

  • Key material for identity verification

  • Service endpoints for various protocols

This class handles both current and legacy DID document formats, providing a consistent interface for accessing and validating document data.

Examples:

Creating a document from JSON

data = {
  "id" => "did:plc:abc123",
  "alsoKnownAs" => ["at://alice.example.com"],
  "pds" => "https://pds.example.com"
}
doc = AtprotoAuth::Identity::Document.new(data)

puts doc.pds                    # => "https://pds.example.com"
puts doc.has_handle?("alice.example.com")  # => true

Handling legacy format

legacy_data = {
  "id" => "did:plc:abc123",
  "service" => [{
    "id" => "#atproto_pds",
    "type" => "AtprotoPersonalDataServer",
    "serviceEndpoint" => "https://pds.example.com"
  }]
}
doc = AtprotoAuth::Identity::Document.new(legacy_data)
puts doc.pds  # => "https://pds.example.com"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Document

Creates a new Document from parsed JSON

Parameters:

  • data (Hash)

    Parsed DID document data

Raises:



44
45
46
47
48
49
50
51
52
# File 'lib/atproto_auth/identity/document.rb', line 44

def initialize(data)
  validate_document!(data)

  @did = data["id"]
  @rotation_keys = data["verificationMethod"]&.map { |m| m["publicKeyMultibase"] } || []
  @also_known_as = data["alsoKnownAs"] || []
  @services = data["service"] || []
  @pds = extract_pds!(data)
end

Instance Attribute Details

#also_known_asObject (readonly)

Returns the value of attribute also_known_as.



39
40
41
# File 'lib/atproto_auth/identity/document.rb', line 39

def also_known_as
  @also_known_as
end

#didObject (readonly)

Returns the value of attribute did.



39
40
41
# File 'lib/atproto_auth/identity/document.rb', line 39

def did
  @did
end

#pdsObject (readonly)

Returns the value of attribute pds.



39
40
41
# File 'lib/atproto_auth/identity/document.rb', line 39

def pds
  @pds
end

#rotation_keysObject (readonly)

Returns the value of attribute rotation_keys.



39
40
41
# File 'lib/atproto_auth/identity/document.rb', line 39

def rotation_keys
  @rotation_keys
end

#servicesObject (readonly)

Returns the value of attribute services.



39
40
41
# File 'lib/atproto_auth/identity/document.rb', line 39

def services
  @services
end

Instance Method Details

#has_handle?(handle) ⇒ Boolean

Checks if this document contains a specific handle

Parameters:

  • handle (String)

    Handle to check (with or without @ prefix)

Returns:

  • (Boolean)

    true if handle is listed in alsoKnownAs



57
58
59
60
61
62
# File 'lib/atproto_auth/identity/document.rb', line 57

def has_handle?(handle) # rubocop:disable Naming/PredicateName
  normalized = handle.start_with?("@") ? handle[1..] : handle
  @also_known_as.any? do |aka|
    aka.start_with?("at://") && aka.delete_prefix("at://") == normalized
  end
end