Class: DIDKit::Resolver
- Inherits:
-
Object
- Object
- DIDKit::Resolver
- 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
-
#nameserver ⇒ String, ...
readonly
Custom DNS nameserver(s) to use for DNS TXT lookups.
Instance Method Summary collapse
-
#get_verified_handle(subject) ⇒ String?
Returns the current primary handle assigned to the given DID, if it verifies correctly.
-
#initialize(options = {}) ⇒ Resolver
constructor
A new instance of Resolver.
-
#resolve_did(did) ⇒ Document
Resolve a DID to a DID document.
-
#resolve_handle(handle, verify: true) ⇒ DID?
Resolve a handle into a DID.
-
#resolve_handle_by_dns(domain) ⇒ String?
Tries to resolve a handle into DID using the DNS TXT method.
-
#resolve_handle_by_well_known(domain) ⇒ String?
Tries to resolve a handle into DID using the HTTP .well-known method.
Constructor Details
#initialize(options = {}) ⇒ Resolver
Returns a new instance of Resolver.
44 45 46 47 48 |
# File 'lib/didkit/resolver.rb', line 44 def initialize( = {}) @nameserver = [:nameserver] @plc_service = .key?(:plc_directory) ? parse_plc_service([:plc_directory]) : DEFAULT_PLC_DIRECTORY @request_options = .slice(:timeout, :max_redirects).freeze end |
Instance Attribute Details
#nameserver ⇒ String, ... (readonly)
Returns 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.
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.
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.
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.
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() 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.
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 |