Sha256: a19fbdc610159a8a9cbdce04e8d4a8d0e0789ba9c251f21c5f6764ba9b93e393

Contents?: true

Size: 1.75 KB

Versions: 19

Compression:

Stored size: 1.75 KB

Contents

require 'casserver/authenticators/base'
require 'uri'
require 'net/http'
require 'net/https'
require 'timeout'

# Validates Google accounts against Google's authentication service -- in other 
# words, this authenticator allows users to log in to CAS using their
# Gmail/Google accounts.
class CASServer::Authenticators::Google < CASServer::Authenticators::Base
  def validate(credentials)
    read_standard_credentials(credentials)

    return false if @username.blank? || @password.blank?
   
    auth_data = {
      'Email'   => @username, 
      'Passwd'  => @password, 
      'service' => 'xapi', 
      'source'  => 'RubyCAS-Server',
      'accountType' => 'HOSTED_OR_GOOGLE'
    }
   
    url = URI.parse('https://www.google.com/accounts/ClientLogin')
    http = Net::HTTP.new(url.host, url.port)
    http.use_ssl = true
    
    # TODO: make the timeout configurable
    wait_seconds = 10
    begin
      timeout(wait_seconds) do
        res = http.start do |conn|
          req = Net::HTTP::Post.new(url.path)
          req.set_form_data(auth_data,'&')
          conn.request(req)
        end
        
        case res
        when Net::HTTPSuccess
          true
        when Net::HTTPForbidden
          false
        else
          $LOG.error("Unexpected response from Google while validating credentials: #{res.inspect} ==> #{res.body}.")
          raise CASServer::AuthenticatorError, "Unexpected response received from Google while validating credentials."
        end
      end
    rescue Timeout::Error
      $LOG.error("Google did not respond to the credential validation request. We waited for #{wait_seconds.inspect} seconds before giving up.")
      raise CASServer::AuthenticatorError, "Timeout while waiting for Google to validate credentials."
    end

  end
end

Version data entries

19 entries across 19 versions & 5 rubygems

Version Path
arpitjain11-rubycas-server-0.8.0.20090612 lib/casserver/authenticators/google.rb
arpitjain11-rubycas-server-0.8.0.20090613 lib/casserver/authenticators/google.rb
gunark-rubycas-server-0.7.999.20090212 lib/casserver/authenticators/google.rb
gunark-rubycas-server-0.7.9999.20090212 lib/casserver/authenticators/google.rb
gunark-rubycas-server-0.8.0.20090213 lib/casserver/authenticators/google.rb
gunark-rubycas-server-0.8.0.20090225 lib/casserver/authenticators/google.rb
gunark-rubycas-server-0.8.0.20090227 lib/casserver/authenticators/google.rb
gunark-rubycas-server-0.8.0.20090427 lib/casserver/authenticators/google.rb
gunark-rubycas-server-0.8.0.20090430 lib/casserver/authenticators/google.rb
gunark-rubycas-server-0.8.0.20090506 lib/casserver/authenticators/google.rb
gunark-rubycas-server-0.8.0.20090715 lib/casserver/authenticators/google.rb
gunark-rubycas-server-0.8.0.20090812 lib/casserver/authenticators/google.rb
kch-rubycas-server-0.8.0.20090715 lib/casserver/authenticators/google.rb
brontes3d-rubycas-server-0.8.0.20100111 lib/casserver/authenticators/google.rb
brontes3d-rubycas-server-0.8.0.20091208 lib/casserver/authenticators/google.rb
brontes3d-rubycas-server-0.8.0.20090812 lib/casserver/authenticators/google.rb
rubycas-server-0.7.1 lib/casserver/authenticators/google.rb
rubycas-server-0.7.1.1 lib/casserver/authenticators/google.rb
rubycas-server-0.7.0 lib/casserver/authenticators/google.rb