class OmniAuth::Strategies::GoogleOauth2AccessToken

Constants

BASE_SCOPE_URL
DEFAULT_SCOPE

Attributes

access_token[RW]

Public Instance Methods

authorize_params() click to toggle source
Calls superclass method
# File lib/omniauth/strategies/google-oauth2-access-token.rb, line 32
def authorize_params
  super.tap do |params|
    options[:authorize_options].each do |k|
      params[k] = request.params[k.to_s] unless [nil, ''].include?(request.params[k.to_s])
    end

    raw_scope = params[:scope] || DEFAULT_SCOPE
    scope_list = raw_scope.split(" ").map {|item| item.split(",")}.flatten
    scope_list.map! { |s| s =~ /^https?:\/\// ? s : "#{BASE_SCOPE_URL}#{s}" }
    params[:scope] = scope_list.join(" ")
    params[:access_type] = 'offline' if params[:access_type].nil?

    session['omniauth.state'] = params[:state] if params['state']
  end
end
callback_phase() click to toggle source
# File lib/omniauth/strategies/google-oauth2-access-token.rb, line 84
      def callback_phase
        if !request.params['access_token'] || request.params['access_token'].to_s.empty?
          raise ArgumentError.new("No access token provided.")
        end

        self.access_token = build_access_token
        self.access_token = self.access_token.refresh! if self.access_token.expired?

        # TODO: Validate the token

        # Validate that the token belong to the application
#         Rails.logger.info "---------------bef"
#         Rails.logger.info self.access_token.get('/app')
#         Rails.logger.info "---------------af"
#         app_raw = self.access_token.get('/app').parsed
#         Rails.logger.info "---------------2nd"
#         Rails.logger.info app_raw
#         if app_raw["id"] != options.client_id.to_s
#           Rails.logger.info "client_id=#{options.client_id}"
#           raise ArgumentError.new("Access token doesn't belong to the client.")
#         end

        # Preserve compatibility with the google provider in normal case
        hash = auth_hash
        hash[:provider] = "google"
        self.env['omniauth.auth'] = hash
        call_app!

       rescue ::OAuth2::Error => e
         fail!(:invalid_credentials, e)
       rescue ::MultiJson::DecodeError => e
         fail!(:invalid_response, e)
       rescue ::Timeout::Error, ::Errno::ETIMEDOUT => e
         fail!(:timeout, e)
       rescue ::SocketError => e
         fail!(:failed_to_connect, e)
      end
client() click to toggle source
# File lib/omniauth/strategies/google-oauth2-access-token.rb, line 73
def client
  ::OAuth2::Client.new(options.client_id, options.client_secret, deep_symbolize(options.client_options))
end
raw_info() click to toggle source
# File lib/omniauth/strategies/google-oauth2-access-token.rb, line 69
def raw_info
  @raw_info ||= access_token.get('https://www.googleapis.com/oauth2/v1/userinfo').parsed
end
request_phase() click to toggle source
# File lib/omniauth/strategies/google-oauth2-access-token.rb, line 77
def request_phase
  form = OmniAuth::Form.new(:title => "User Token", :url => callback_path)
  form.text_field "Access Token", "access_token"
  form.button "Sign In"
  form.to_response
end

Protected Instance Methods

build_access_token() click to toggle source
# File lib/omniauth/strategies/google-oauth2-access-token.rb, line 131
def build_access_token
  hash = request.params.slice("access_token", "refresh_token", "expires_in", "token_type")
  ::OAuth2::AccessToken.from_hash(
    client, 
    hash.update(options.access_token_options)
  )
end
deep_symbolize(hash) click to toggle source
# File lib/omniauth/strategies/google-oauth2-access-token.rb, line 124
def deep_symbolize(hash)
  hash.inject({}) do |h, (k,v)|
    h[k.to_sym] = v.is_a?(Hash) ? deep_symbolize(v) : v
    h
  end
end

Private Instance Methods

image_url(options) click to toggle source
# File lib/omniauth/strategies/google-oauth2-access-token.rb, line 152
def image_url(options)
  original_url = raw_info['picture']
  return original_url if original_url.nil? || (!options[:image_size] && !options[:image_aspect_ratio])

  image_params = []
  if options[:image_size].is_a?(Integer)
    image_params << "s#{options[:image_size]}"
  elsif options[:image_size].is_a?(Hash)
    image_params << "w#{options[:image_size][:width]}" if options[:image_size][:width]
    image_params << "h#{options[:image_size][:height]}" if options[:image_size][:height]
  end
  image_params << 'c' if options[:image_aspect_ratio] == 'square'

  params_index = original_url.index('/photo.jpg')
  original_url.insert(params_index, ('/' + image_params.join('-')))
end
prune!(hash) click to toggle source
# File lib/omniauth/strategies/google-oauth2-access-token.rb, line 141
def prune!(hash)
  hash.delete_if do |_, v|
    prune!(v) if v.is_a?(Hash)
    v.nil? || (v.respond_to?(:empty?) && v.empty?)
  end
end
verified_email() click to toggle source
# File lib/omniauth/strategies/google-oauth2-access-token.rb, line 148
def verified_email
  raw_info['verified_email'] ? raw_info['email'] : nil
end
verify_token(id_token, access_token) click to toggle source
# File lib/omniauth/strategies/google-oauth2-access-token.rb, line 169
def verify_token(id_token, access_token)
  return false unless (id_token && access_token)

  raw_response = client.request(:get, 'https://www.googleapis.com/oauth2/v2/tokeninfo', :params => {
    :id_token => id_token,
    :access_token => access_token
  }).parsed
  raw_response['issued_to'] == options.client_id
end