Sha256: 8de3f33ce698ad8318dbcf43ea42f5348faaa13528dae9942fa9b3df68b4afc4

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

class ChatBot
  def authenticate(sites = ["stackexchange"])
    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

  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.0.pre.pre3 lib/chatx/auth.rb