Class: Base4R::ClientLogin

Inherits:
Object
  • Object
show all
Includes:
HTTPLogger
Defined in:
lib/client_login.rb

Overview

ClientLogin authenticates the user with Google Accounts using the ClientLogin service.

Constant Summary collapse

ServiceURL =
URI.parse('https://www.google.com/accounts/ClientLogin')

Instance Method Summary collapse

Methods included from HTTPLogger

included, #log, #log_request, #log_response, #verbose?

Constructor Details

#initialize(options = {}) ⇒ ClientLogin

Create a ClientLogin. options is optional and by default logs into the Google Base API.



36
37
38
39
40
41
42
43
# File 'lib/client_login.rb', line 36

def initialize(options={})

  defaults = {:accountType => 'GOOGLE',
              :service => 'gbase',
              :source => 'base4r-clientloginapp-v1'}

   @config = defaults.merge(options)
end

Instance Method Details

#authenticate(email, password) ⇒ Object

attempt to authenticate email and password with Google. Returns an authentication token on success, raises Exception on failure. todo: make exception handling more useful



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/client_login.rb', line 48

def authenticate(email, password)

  params = {
    'accountType' => @config[:accountType],
    'Email' => email,
    'Passwd' => password,
    'service' => @config[:service],
    'source' => @config[:source]
  }
  
  req = Net::HTTP::Post.new(ServiceURL.path)
  req.set_form_data(params)

  http = Net::HTTP.new(ServiceURL.host, 443)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  http.ca_file = File.expand_path(File.dirname(__FILE__)+"/../cert/cacert.pem")
  # todo - above is probably reading file for every request, cache certs instead

  log_request req
  resp = http.request(req)
  log_response resp

  unless resp.instance_of? Net::HTTPOK then
    resp.body =~ /^Error=(.+)$/

    raise CaptchaRequiredException.new(email) if 'CaptchaRequired' == $1        
    raise 'unknown exception authenticating with google:'+$1
  end

  # parse the auth key from the body
  resp.body =~ /^Auth=(.+)$/
  return $1

end