Class: AzureEnum::Federation

Inherits:
Object
  • Object
show all
Defined in:
lib/azure_enum.rb

Overview

Class initializes with a domain name, and provides methods to interact with MS Autodiscover

Defined Under Namespace

Classes: Discovery

Instance Method Summary collapse

Constructor Details

#initialize(domain) ⇒ Federation

Returns a new instance of Federation.



10
11
12
13
14
# File 'lib/azure_enum.rb', line 10

def initialize(domain)
  @domain = domain
  @xml_text = nil
  @redirect = nil
end

Instance Method Details

#autodiscObject



28
29
30
31
32
33
34
# File 'lib/azure_enum.rb', line 28

def autodisc
  # These are the default autodiscover domains.
  [
    "https://autodiscover.#{@domain}/autodiscover/autodiscover.svc",
    "https://#{@domain}/autodiscover/autodiscover.svc"
  ]
end

#check_redirectObject

This will identify if the http:// redirect exists for the domain, usually per Office 365



17
18
19
20
21
22
23
24
25
26
# File 'lib/azure_enum.rb', line 17

def check_redirect
  url = "http://autodiscover.#{@domain}/autodiscover/autodiscover.svc"
  begin
    res = HTTPClient.head(url)
  rescue
    return nil
  end
  return nil unless res.status_code == 302
  @redirect = res.header["Location"][0]
end

#enumerate_autodisc(httpsdomains = autodisc) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/azure_enum.rb', line 36

def enumerate_autodisc(httpsdomains = autodisc)
  httpsdomains.unshift @redirect if @redirect
  @redirect = nil
  httpsdomains.each do |url|
    xml = get_xml(@domain, url)
    begin
      http = HTTPClient.new
      content = { "Content-Type" => "text/xml; charset=utf-8" }
      res = http.post(url, xml, content)
      # In the event of a second redirect we are on the right path
      # Recurse this function for the correct result
      if res.status_code == 302
        return enumerate_autodisc [res.header['Location'][0]]
      end
      @xml_text = res.content
      return true
      # It is bad style to rescue "all" errors. However, it turns out there is a practically
      # never ending list of ways this can fail. And "any" failure is reason to rule out the address
    rescue
      next
    end
  end
  return false
end

#getdomainsObject



61
62
63
64
65
66
67
68
# File 'lib/azure_enum.rb', line 61

def getdomains
  raise "enumumerate_autodisc not called yet" unless @xml_text
  tree = Nokogiri.parse(@xml_text)
  tree.xpath(
    "//ad:GetFederationInformationResponseMessage/ad:Response/ad:Domains/ad:Domain",
    ad: "http://schemas.microsoft.com/exchange/2010/Autodiscover"
  ).map(&:text)
end