Class: DIDKit::Resolver
Constant Summary
collapse
- RESERVED_DOMAINS =
%w(alt arpa example internal invalid local localhost onion test)
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Requests
#content_type_matches, #get_data, #get_json, #get_response, #uri_origin
Constructor Details
#initialize(options = {}) ⇒ Resolver
Returns a new instance of Resolver.
16
17
18
19
|
# File 'lib/didkit/resolver.rb', line 16
def initialize(options = {})
@nameserver = options[:nameserver]
@request_options = options.slice(:timeout, :max_redirects)
end
|
Instance Attribute Details
#nameserver ⇒ Object
Returns the value of attribute nameserver.
14
15
16
|
# File 'lib/didkit/resolver.rb', line 14
def nameserver
@nameserver
end
|
Instance Method Details
#first_verified_handle(did, handles) ⇒ Object
103
104
105
|
# File 'lib/didkit/resolver.rb', line 103
def first_verified_handle(did, handles)
handles.detect { |h| resolve_handle(h) == did.to_s }
end
|
#get_verified_handle(subject) ⇒ Object
97
98
99
100
101
|
# File 'lib/didkit/resolver.rb', line 97
def get_verified_handle(subject)
document = subject.is_a?(Document) ? subject : resolve_did(subject)
first_verified_handle(document.did, document.handles)
end
|
#parse_did_from_dns(txt) ⇒ Object
72
73
74
|
# File 'lib/didkit/resolver.rb', line 72
def parse_did_from_dns(txt)
txt =~ /\Adid\=(did\:\w+\:.*)\z/ ? $1 : nil
end
|
#parse_did_from_well_known(text) ⇒ Object
76
77
78
79
|
# File 'lib/didkit/resolver.rb', line 76
def parse_did_from_well_known(text)
text = text.strip
text.lines.length == 1 && text =~ DID::GENERIC_REGEXP ? text : nil
end
|
#resolv_options ⇒ Object
66
67
68
69
70
|
# File 'lib/didkit/resolver.rb', line 66
def resolv_options
options = Resolv::DNS::Config.default_config_hash.dup
options[:nameserver] = nameserver if nameserver
options
end
|
#resolve_did(did) ⇒ Object
81
82
83
84
85
|
# File 'lib/didkit/resolver.rb', line 81
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_did_plc(did) ⇒ Object
87
88
89
90
|
# File 'lib/didkit/resolver.rb', line 87
def resolve_did_plc(did)
json = get_json("https://plc.directory/#{did}", content_type: /^application\/did\+ld\+json(;.+)?$/)
Document.new(did, json)
end
|
#resolve_did_web(did) ⇒ Object
92
93
94
95
|
# File 'lib/didkit/resolver.rb', line 92
def resolve_did_web(did)
json = get_json("https://#{did.web_domain}/.well-known/did.json")
Document.new(did, json)
end
|
#resolve_handle(handle) ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/didkit/resolver.rb', line 21
def resolve_handle(handle)
if handle.is_a?(DID) || handle =~ DID::GENERIC_REGEXP
return DID.new(handle)
end
domain = handle.gsub(/^@/, '')
return nil if RESERVED_DOMAINS.include?(domain.split('.').last)
if dns_did = resolve_handle_by_dns(domain)
DID.new(dns_did, :dns)
elsif http_did = resolve_handle_by_well_known(domain)
DID.new(http_did, :http)
else
nil
end
end
|
#resolve_handle_by_dns(domain) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/didkit/resolver.rb', line 39
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
if record = dns_records.first
if string = record.strings.first
return parse_did_from_dns(string)
end
end
nil
end
|
#resolve_handle_by_well_known(domain) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/didkit/resolver.rb', line 53
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 => e
nil
end
|