Skip to content

Commit

Permalink
resolve handles with redirecting .well-known
Browse files Browse the repository at this point in the history
  • Loading branch information
mackuba committed Mar 12, 2024
1 parent a89c88a commit 6d7350f
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/didkit/resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
module DIDKit
class Resolver
RESERVED_DOMAINS = %w(alt arpa example internal invalid local localhost onion test)
MAX_REDIRECTS = 5

attr_accessor :nameserver

Expand Down Expand Up @@ -41,7 +42,11 @@ def resolve_handle_by_dns(domain)
end

def resolve_handle_by_well_known(domain)
url = URI("https://#{domain}/.well-known/atproto-did")
resolve_handle_from_url("https://#{domain}/.well-known/atproto-did")
end

def resolve_handle_from_url(url, redirects = 0)
url = URI(url) unless url.is_a?(URI)

response = Net::HTTP.start(url.host, url.port, use_ssl: true, open_timeout: 10, read_timeout: 10) do |http|
request = Net::HTTP::Get.new(url)
Expand All @@ -52,6 +57,11 @@ def resolve_handle_by_well_known(domain)
if text = response.body
return parse_did_from_well_known(text)
end
elsif response.is_a?(Net::HTTPRedirection) && redirects < MAX_REDIRECTS
if location = response['Location']
target_url = location.include?('://') ? location : (url.origin + location)
return resolve_handle_from_url(target_url, redirects + 1)
end
end

nil
Expand Down

0 comments on commit 6d7350f

Please sign in to comment.