Class: CASServer::Authenticators::Google

Inherits:
Base
  • Object
show all
Defined in:
lib/casserver/authenticators/google.rb

Overview

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.

Instance Attribute Summary

Attributes inherited from Base

#options, #username

Instance Method Summary collapse

Methods inherited from Base

#configure, #extra_attributes, setup

Instance Method Details

#validate(credentials) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/casserver/authenticators/google.rb', line 11

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')
  if @options[:proxy]
    http = Net::HTTP.Proxy(@options[:proxy][:host], @options[:proxy][:port], @options[:proxy][:username], @options[:proxy][:password]).new(url.host, url.port)
  else
    http = Net::HTTP.new(url.host, url.port)
  end
  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