tangled
alpha
login
or
join now
mackuba.eu
/
didkit
1
fork
atom
A library for handling DID identifiers used in Bluesky AT Protocol
1
fork
atom
overview
issues
pulls
pipelines
extracted request making & handling redirects to a module
mackuba.eu
2 years ago
737eda0d
8a787791
+37
-21
2 changed files
expand all
collapse all
unified
split
lib
didkit
requests.rb
resolver.rb
+28
lib/didkit/requests.rb
···
1
1
+
module DIDKit::Requests
2
2
+
def get_response(url, options = {})
3
3
+
url = URI(url) unless url.is_a?(URI)
4
4
+
request_options = { use_ssl: true }
5
5
+
6
6
+
if timeout = options[:timeout]
7
7
+
request_options[:open_timeout] = timeout
8
8
+
request_options[:read_timeout] = timeout
9
9
+
end
10
10
+
11
11
+
redirects = 0
12
12
+
max_redirects = options[:max_redirects] || 0
13
13
+
14
14
+
loop do
15
15
+
response = Net::HTTP.start(url.host, url.port, request_options) do |http|
16
16
+
request = Net::HTTP::Get.new(url)
17
17
+
http.request(request)
18
18
+
end
19
19
+
20
20
+
if response.is_a?(Net::HTTPRedirection) && redirects < max_redirects && (location = response['Location'])
21
21
+
url = URI(location.include?('://') ? location : (url.origin + location))
22
22
+
redirects += 1
23
23
+
else
24
24
+
return response
25
25
+
end
26
26
+
end
27
27
+
end
28
28
+
end
+9
-21
lib/didkit/resolver.rb
···
5
5
6
6
require_relative 'did'
7
7
require_relative 'document'
8
8
+
require_relative 'requests'
8
9
9
10
module DIDKit
10
11
class Resolver
11
12
RESERVED_DOMAINS = %w(alt arpa example internal invalid local localhost onion test)
12
13
MAX_REDIRECTS = 5
14
14
+
15
15
+
include Requests
13
16
14
17
attr_accessor :nameserver
15
18
···
32
35
end
33
36
34
37
def resolve_handle_by_dns(domain)
35
35
-
dns_records = Resolv::DNS.open(resolv_options) { |d|
38
38
+
dns_records = Resolv::DNS.open(resolv_options) do |d|
36
39
d.getresources("_atproto.#{domain}", Resolv::DNS::Resource::IN::TXT)
37
37
-
}
40
40
+
end
38
41
39
42
if record = dns_records.first
40
43
if string = record.strings.first
···
46
49
end
47
50
48
51
def resolve_handle_by_well_known(domain)
49
49
-
resolve_handle_from_url("https://#{domain}/.well-known/atproto-did")
50
50
-
end
51
51
-
52
52
-
def resolve_handle_from_url(url, redirects = 0)
53
53
-
url = URI(url) unless url.is_a?(URI)
54
54
-
55
55
-
response = Net::HTTP.start(url.host, url.port, use_ssl: true, open_timeout: 10, read_timeout: 10) do |http|
56
56
-
request = Net::HTTP::Get.new(url)
57
57
-
http.request(request)
58
58
-
end
52
52
+
url = "https://#{domain}/.well-known/atproto-did"
53
53
+
response = get_response(url, timeout: 10, max_redirects: MAX_REDIRECTS)
59
54
60
60
-
if response.is_a?(Net::HTTPSuccess)
61
61
-
if text = response.body
62
62
-
return parse_did_from_well_known(text)
63
63
-
end
64
64
-
elsif response.is_a?(Net::HTTPRedirection) && redirects < MAX_REDIRECTS
65
65
-
if location = response['Location']
66
66
-
target_url = location.include?('://') ? location : (url.origin + location)
67
67
-
return resolve_handle_from_url(target_url, redirects + 1)
68
68
-
end
55
55
+
if response.is_a?(Net::HTTPSuccess) && (text = response.body)
56
56
+
return parse_did_from_well_known(text)
69
57
end
70
58
71
59
nil