Class: OmniAuth::Strategies::GoogleOAuth2

Inherits:
OAuth2
  • Object
show all
Defined in:
lib/omniauth/strategies/google_oauth2.rb

Overview

OAuth 2.0 based authentication with Google.

Instance Attribute Summary

Attributes inherited from OAuth2

#client_id, #client_options, #client_secret, #options

Instance Method Summary collapse

Methods inherited from OAuth2

#callback_url, #client

Constructor Details

#initialize(app, client_id = nil, client_secret = nil, options = {}, &block) ⇒ GoogleOAuth2

Returns a new instance of GoogleOAuth2.

Parameters:

  • app (Rack Application)

    standard middleware application argument

  • client_id (String) (defaults to: nil)

    the application ID for your client

  • client_secret (String) (defaults to: nil)

    the application secret

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :scope (String) — default: 'https://www.googleapis.com/auth/userinfo.email'

    space-separated services that you need.



12
13
14
15
16
17
18
19
20
# File 'lib/omniauth/strategies/google_oauth2.rb', line 12

def initialize(app, client_id = nil, client_secret = nil, options = {}, &block)
  client_options = {
    :site => 'https://accounts.google.com',
    :authorize_url => '/o/oauth2/auth',
    :token_url => '/o/oauth2/token'
  }

  super(app, (options[:name] || :google_oauth2), client_id, client_secret, client_options, options, &block)
end

Instance Method Details

#auth_hashObject



30
31
32
33
34
35
36
37
# File 'lib/omniauth/strategies/google_oauth2.rb', line 30

def auth_hash
  OmniAuth::Utils.deep_merge(super, {
    'uid' => ['uid'],
    'user_info' => ,
    'credentials' => {'expires_at' => @access_token.expires_at},
    'extra' => {'user_hash' => user_data}
  })
end

#request_phaseObject



22
23
24
25
26
27
28
# File 'lib/omniauth/strategies/google_oauth2.rb', line 22

def request_phase
  google_email_scope = "www.googleapis.com/auth/userinfo.email"
  options[:scope] ||= "https://#{google_email_scope}"
  options[:scope] << " https://#{google_email_scope}" unless options[:scope] =~ %r[http[s]?:\/\/#{google_email_scope}]
  redirect client.auth_code.authorize_url(
    {:redirect_uri => callback_url, :response_type => "code"}.merge(options))
end

#user_dataObject



52
53
54
55
# File 'lib/omniauth/strategies/google_oauth2.rb', line 52

def user_data
  @data ||=
    @access_token.get("https://www.googleapis.com/userinfo/email?alt=json").parsed
end

#user_infoObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/omniauth/strategies/google_oauth2.rb', line 39

def 
  if user_data['data']['isVerified']
    email = user_data['data']['email']
  else
    email = nil
  end
  {
    'email' => email,
    'uid' => email,
    'name' => email
  }
end