Class: Coppertone::Utils::ValidatedDomainFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/coppertone/utils/validated_domain_finder.rb

Overview

A class used to find validated domains as defined in section 5.5 of the RFC.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(macro_context, request_context, subdomain_only = true) ⇒ ValidatedDomainFinder

Returns a new instance of ValidatedDomainFinder.



7
8
9
10
11
# File 'lib/coppertone/utils/validated_domain_finder.rb', line 7

def initialize(macro_context, request_context, subdomain_only = true)
  @mc = macro_context
  @request_context = request_context
  @subdomain_only = subdomain_only
end

Instance Attribute Details

#subdomain_onlyObject (readonly)

Returns the value of attribute subdomain_only.



6
7
8
# File 'lib/coppertone/utils/validated_domain_finder.rb', line 6

def subdomain_only
  @subdomain_only
end

Instance Method Details

#fetch_ptr_names(ip) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/coppertone/utils/validated_domain_finder.rb', line 20

def fetch_ptr_names(ip)
  dns_client = @request_context.dns_client
  names = dns_client.fetch_ptr_records(ip.reverse).map do |ptr|
    ptr[:name]
  end
  record_limit =
    @request_context.dns_lookups_per_ptr_mechanism_limit
  record_limit ? names.slice(0, record_limit) : names
end

#find(target_name) ⇒ Object



13
14
15
16
17
18
# File 'lib/coppertone/utils/validated_domain_finder.rb', line 13

def find(target_name)
  ip = @mc.original_ipv6? ? @mc.ip_v6 : @mc.ip_v4
  ptr_names = fetch_ptr_names(ip)
  ip_checker = IPInDomainChecker.new(@mc, @request_context)
  ptr_names.find { |n| ptr_record_matches?(ip_checker, target_name, n) }
end

#ptr_record_matches?(ip_checker, target_name, ptr_name) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
# File 'lib/coppertone/utils/validated_domain_finder.rb', line 30

def ptr_record_matches?(ip_checker,
                        target_name, ptr_name)
  is_candidate = !subdomain_only ||
                 DomainUtils.subdomain_or_same?(ptr_name, target_name)
  is_candidate && ip_checker.check(ptr_name)
rescue DNSAdapter::Error
  # If a DNS error occurs when looking up a domain, treat it
  # as a non match
  false
end