Method: ChatBot#authenticate

Defined in:
lib/chatx/auth.rb

#authenticate(sites = ["stackexchange"]) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/chatx/auth.rb', line 2

def authenticate(sites = ["stackexchange"])
  if sites.is_a? Hash
    sites.each do |site, cookie_str|
      cookie = Mechanize::Cookie.new("acct", cookie_str)
      cookie.domain = ".#{site}.com"
      cookie.path = "/"
      @agent.cookie_jar.add!(cookie)
    end
    true
  else
    sites = [sites] unless sites.is_a?(Array)

    openid = @agent.get "https://openid.stackexchange.com/account/login"
    fkey_input = openid.search "//input[@name='fkey']"
    fkey = fkey_input.empty? ? "" : fkey_input.attribute("value")

    @agent.post("https://openid.stackexchange.com/account/login/submit",
                fkey:     fkey,
                email:    @email,
                password: @password)

    auth_results = sites.map { |s| site_auth(s) }
    failed = auth_results.any?(&:!)
    !failed
  end
end