Sha256: 46d65b27a59ee442acd6fcc95415f0afaaef553acaf901f2eb3978c9a734884e

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

class ChatBot
  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

  def site_auth(site)
    # Get fkey
    login_page = @agent.get "https://#{site}.com/users/login"
    fkey_input = login_page.search "//input[@name='fkey']"
    fkey = fkey_input.attribute('value')

    @agent.post("https://#{site}.com/users/authenticate",
                fkey: fkey,
                openid_identifier: 'https://openid.stackexchange.com')

    home = @agent.get "https://chat.#{site}.com"
    if home.search(".topbar-links span.topbar-menu-links a").first.text.casecmp('log in').zero?
      @logger.warn "Login to #{site} failed :("
      false
    else
      @logger.info "Login to #{site} successful!"
      true
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chatx-0.0.1 lib/chatx/auth.rb