Class: DIDKit::Resolver

Inherits:
Object
  • Object
show all
Includes:
Requests
Defined in:
lib/didkit/resolver.rb

Overview

A class which manages resolving of handles to DIDs and DIDs to DID documents.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Resolver

Returns a new instance of Resolver.

Parameters:

  • options (Hash) (defaults to: {}) —

    resolver options

Options Hash (options):

  • :nameserver (String, Array<String>) —

    custom DNS nameserver(s) to use (IP or an array of IPs)

  • :plc_directory (String) —

    URL or host of an alternate PLC directory, without a path; defaults to HTTPS (HTTP allowed for localhost/loopback IPs only)

  • :timeout (Integer) —

    request timeout in seconds (default: 15)

  • :max_redirects (Integer) —

    maximum number of redirects to follow (default: 5)

Raises:

  • (ArgumentError) —

    if the PLC directory address is invalid



44
45
46
47
48
# File 'lib/didkit/resolver.rb', line 44

def initialize(options = {})
  @nameserver = options[:nameserver]
  @plc_service = options.key?(:plc_directory) ? parse_plc_service(options[:plc_directory]) : DEFAULT_PLC_DIRECTORY
  @request_options = options.slice(:timeout, :max_redirects).freeze
end

Instance Attribute Details

#nameserver ⇒ String, ... (readonly)

Returns custom DNS nameserver(s) to use for DNS TXT lookups.

Returns:

  • (String, Array<String>, nil) —

    custom DNS nameserver(s) to use for DNS TXT lookups



34
35
36
# File 'lib/didkit/resolver.rb', line 34

def nameserver
  @nameserver
end

Instance Method Details

#get_verified_handle(subject) ⇒ String?

Returns the current primary handle assigned to the given DID, if it verifies correctly.

Looks up the first syntactically valid handle in the DID document and checks if it is assigned correctly to this DID using DNS TXT or .well-known. Any later handles are ignored, even if the first handle fails verification. Returns nil if the primary handle cannot be verified.

Parameters:

  • subject (String, DID, Document) —

    a DID or its DID document

Returns:

  • (String, nil) —

    verified handle domain, if found



156
157
158
159
160
161
162
163
164
165
# File 'lib/didkit/resolver.rb', line 156

def get_verified_handle(subject)
  document = subject.is_a?(Document) ? subject : resolve_did(subject)

  primary_handle = document.handles.first

  return nil if primary_handle.nil?
  return nil unless resolve_handle(primary_handle, verify: false) == document.did.to_s

  primary_handle
end

#resolve_did(did) ⇒ Document

Resolve a DID to a DID document.

Looks up the DID document with the DID's identity details from an appropriate source, i.e. either plc.directory or its mirror for did:plc DIDs, or the did:web's domain for did:web DIDs.

Parameters:

  • did (String, DID) —

    DID string or object

Returns:

Raises:

  • (APIError) —

    if an incorrect response is returned



141
142
143
144
145
# File 'lib/didkit/resolver.rb', line 141

def resolve_did(did)
  did = DID.new(did) if did.is_a?(String)

  did.type == :plc ? resolve_did_plc(did) : resolve_did_web(did)
end

#resolve_handle(handle, verify: true) ⇒ DID?

Resolve a handle into a DID. Looks up the given ATProto domain handle using the DNS TXT method and the HTTP .well-known method and returns a DID if one is assigned using either of the methods.

If a DID string or a DID object is passed, it simply returns that DID, so you can use this method to pass it an input string from the user which can be a DID or handle, without having to check which one it is.

Parameters:

  • handle (String, DID) —

    a domain handle (may start with an @) or a DID string

  • verify (Boolean) (defaults to: true) —

    require the handle to be assigned to the DID in the DID document (default: true)

Returns:

  • (DID, nil) —

    resolved DID if found, nil otherwise

Raises:

  • (APIError) —

    if verification is enabled and the DID document request is unsuccessful

  • (FormatError) —

    if verification is enabled and the DID document has invalid data



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/didkit/resolver.rb', line 63

def resolve_handle(handle, verify: true)
  if !(handle.is_a?(DID) || handle.is_a?(String))
    raise ArgumentError, "handle argument should be a String or a DID object"
  end

  if handle.is_a?(DID) || handle =~ DIDKit::GENERIC_DID_REGEXP
    return DID.new(handle)
  end

  domain = handle.gsub(/^@/, '').downcase(:ascii)
  return nil unless valid_handle_identifier?(domain)

  if dns_did = resolve_handle_by_dns(domain)
    referenced_did = DID.new(dns_did, :dns)
  elsif http_did = resolve_handle_by_well_known(domain)
    referenced_did = DID.new(http_did, :http)
  else
    return nil
  end

  if verify
    document = self.resolve_did(referenced_did)
    return nil unless document.handles.first&.downcase == domain
  end

  referenced_did
end

#resolve_handle_by_dns(domain) ⇒ String?

Tries to resolve a handle into DID using the DNS TXT method.

Checks the DNS records for a given domain for an entry _atproto.#{domain} whose value is a correct DID string. Records are rejected if there exists more than one valid entry and they are referencing two or more different DIDs.

Parameters:

  • domain (String) —

    a domain handle to look up

Returns:

  • (String, nil) —

    resolved DID if one valid record is found, nil otherwise



100
101
102
103
104
105
106
107
108
# File 'lib/didkit/resolver.rb', line 100

def resolve_handle_by_dns(domain)
  dns_records = Resolv::DNS.open(resolv_options) do |d|
    d.getresources("_atproto.#{domain}", Resolv::DNS::Resource::IN::TXT)
  end

  dids = dns_records.map { |record| parse_did_from_dns(record.data) }.compact.uniq

  dids.length == 1 ? dids.first : nil
end

#resolve_handle_by_well_known(domain) ⇒ String?

Tries to resolve a handle into DID using the HTTP .well-known method.

Checks the /.well-known/atproto-did endpoint on the given domain to see if it returns a text file that contains a correct DID string.

Parameters:

  • domain (String) —

    a domain handle to look up

Returns:

  • (String, nil) —

    resolved DID if found, nil otherwise



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/didkit/resolver.rb', line 118

def resolve_handle_by_well_known(domain)
  url = "https://#{domain}/.well-known/atproto-did"
  response = get_response(url, @request_options)

  if response.is_a?(Net::HTTPSuccess) && (text = response.body)
    return parse_did_from_well_known(text)
  end

  nil
rescue StandardError
  nil
end