Class: OmniAuth::Strategies::Google

Inherits:
Object
  • Object
show all
Includes:
OmniAuth::Strategy
Defined in:
lib/omniauth/strategies/google.rb

Overview

Authentication strategy for connecting with Google

Defined Under Namespace

Classes: CallbackError

Constant Summary collapse

PLUS_PROFILE_SCOPE =
'https://www.googleapis.com/auth/plus.me'
USERINFO_SCOPE =
'https://www.googleapis.com/auth/userinfo.profile'
DEFAULT_SCOPE =
PLUS_PROFILE_SCOPE

Instance Method Summary collapse

Instance Method Details

#callback_phaseObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/omniauth/strategies/google.rb', line 128

def callback_phase
  if request.params['error'] || request.params['error_reason']
    raise CallbackError.new(
      request.params['error'],
      request.params['error_description'] ||
        request.params['error_reason'],
      request.params['error_uri']
    )
  end
  if request.params['code']
    client.authorization.code = request.params['code']
  end
  client.authorization.fetch_access_token!
  super
rescue ::Signet::UnsafeOperationError => e
  fail!(:unsafe_operation, e)
rescue ::Signet::AuthorizationError, CallbackError => e
  fail!(:invalid_credentials, e)
rescue ::MultiJson::DecodeError, ::Signet::ParseError => e
  fail!(:invalid_response, e)
rescue ::Timeout::Error, ::Errno::ETIMEDOUT => e
  fail!(:timeout, e)
end

#clientObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/omniauth/strategies/google.rb', line 25

def client
  @client ||= (begin
    client = ::Google::APIClient.new(options.client_options)
    client.key = options.api_key
    if request.ip != '127.0.0.1'
      client.user_ip = request.ip
    end
    if client.authorization.kind_of?(::Signet::OAuth2::Client)
      client.authorization.client_id = options.client_id
      client.authorization.client_secret = options.client_secret
      client.authorization.scope = options.scope
      client.authorization.refresh_token = options.refresh_token
      client.authorization.redirect_uri = redirect_uri
    end
    client
  end)
end

#profile_dataObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/omniauth/strategies/google.rb', line 47

def profile_data
  if skip_info?
    raise ArgumentError,
      "Profile data is not available when the :skip_info option is set."
  end
  @profile_data ||= (begin
    if client.authorization.scope.include?(PLUS_PROFILE_SCOPE)
      plus = client.discovered_api('plus', 'v1')
      result = client.execute(plus.people.get, {'userId' => 'me'})
      result.data
    elsif client.authorization.scope.include?(USERINFO_SCOPE)
      result = client.execute(
        :uri => 'https://www.googleapis.com/oauth2/v1/userinfo'
      )
      result.data
    else
      raise ArgumentError,
        "Profile data is not available without requesting " +
        "the '#{USERINFO_SCOPE}' scope."
    end
  end)
end

#redirect_uriObject



43
44
45
# File 'lib/omniauth/strategies/google.rb', line 43

def redirect_uri
  full_host + script_name + callback_path
end

#request_phaseObject



124
125
126
# File 'lib/omniauth/strategies/google.rb', line 124

def request_phase
  redirect client.authorization.authorization_uri.to_s
end