class OmniAuth::Strategies::GoogleOauth2
Main class for Google OAuth2 strategy.
Constants
- ALLOWED_ISSUERS
- AUTHORIZE_OPTIONS
- BASE_SCOPES
- BASE_SCOPE_URL
- DEFAULT_SCOPE
- IMAGE_SIZE_REGEXP
- USER_INFO_URL
Public Instance Methods
custom_build_access_token()
click to toggle source
# File lib/omniauth/strategies/google_oauth2.rb, line 101 def custom_build_access_token access_token = get_access_token(request) verify_hd(access_token) access_token end
Also aliased as: build_access_token
raw_info()
click to toggle source
# File lib/omniauth/strategies/google_oauth2.rb, line 97 def raw_info @raw_info ||= access_token.get(USER_INFO_URL).parsed end
Private Instance Methods
callback_url()
click to toggle source
# File lib/omniauth/strategies/google_oauth2.rb, line 116 def callback_url options[:redirect_uri] || (full_host + callback_path) end
client_get_token(verifier, redirect_uri)
click to toggle source
# File lib/omniauth/strategies/google_oauth2.rb, line 148 def client_get_token(verifier, redirect_uri) client.auth_code.get_token(verifier, get_token_options(redirect_uri), get_token_params) end
get_access_token(request)
click to toggle source
# File lib/omniauth/strategies/google_oauth2.rb, line 120 def get_access_token(request) verifier = request.params['code'] redirect_uri = request.params['redirect_uri'] access_token = request.params['access_token'] if verifier && request.xhr? client_get_token(verifier, redirect_uri || 'postmessage') elsif verifier client_get_token(verifier, redirect_uri || callback_url) elsif access_token && verify_token(access_token) ::OAuth2::AccessToken.from_hash(client, request.params.dup) elsif request.content_type =~ /json/i begin body = JSON.parse(request.body.read) request.body.rewind # rewind request body for downstream middlewares verifier = body && body['code'] access_token = body && body['access_token'] redirect_uri ||= body && body['redirect_uri'] if verifier client_get_token(verifier, redirect_uri || 'postmessage') elsif verify_token(access_token) ::OAuth2::AccessToken.from_hash(client, body.dup) end rescue JSON::ParserError => e warn "[omniauth google-oauth2] JSON parse error=#{e}" end end end
get_scope(params)
click to toggle source
# File lib/omniauth/strategies/google_oauth2.rb, line 156 def get_scope(params) raw_scope = params[:scope] || DEFAULT_SCOPE scope_list = raw_scope.split(' ').map { |item| item.split(',') }.flatten scope_list.map! { |s| s =~ %r{^https?://} || BASE_SCOPES.include?(s) ? s : "#{BASE_SCOPE_URL}#{s}" } scope_list.join(' ') end
get_token_options(redirect_uri = '')
click to toggle source
# File lib/omniauth/strategies/google_oauth2.rb, line 167 def get_token_options(redirect_uri = '') { redirect_uri: redirect_uri }.merge(token_params.to_hash(symbolize_keys: true)) end
get_token_params()
click to toggle source
# File lib/omniauth/strategies/google_oauth2.rb, line 152 def get_token_params deep_symbolize(options.auth_token_params || {}) end
image_params()
click to toggle source
# File lib/omniauth/strategies/google_oauth2.rb, line 203 def image_params 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' '/' + image_params.join('-') end
image_size_opts_passed?()
click to toggle source
# File lib/omniauth/strategies/google_oauth2.rb, line 199 def image_size_opts_passed? options[:image_size] || options[:image_aspect_ratio] end
image_url()
click to toggle source
# File lib/omniauth/strategies/google_oauth2.rb, line 178 def image_url return nil unless raw_info['picture'] u = URI.parse(raw_info['picture'].gsub('https:https', 'https')) path_index = u.path.to_s.index('/photo.jpg') if path_index && image_size_opts_passed? u.path.insert(path_index, image_params) u.path = u.path.gsub('//', '/') # Check if the image is already sized! split_path = u.path.split('/') u.path = u.path.sub("/#{split_path[-3]}", '') if split_path[-3] =~ IMAGE_SIZE_REGEXP end u.query = strip_unnecessary_query_parameters(u.query) u.to_s end
nil_or_empty?(obj)
click to toggle source
# File lib/omniauth/strategies/google_oauth2.rb, line 112 def nil_or_empty?(obj) obj.is_a?(String) ? obj.empty? : obj.nil? end
prune!(hash)
click to toggle source
# File lib/omniauth/strategies/google_oauth2.rb, line 171 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
strip_unnecessary_query_parameters(query_parameters)
click to toggle source
# File lib/omniauth/strategies/google_oauth2.rb, line 216 def strip_unnecessary_query_parameters(query_parameters) # strip `sz` parameter (defaults to sz=50) which overrides `image_size` options return nil if query_parameters.nil? params = CGI.parse(query_parameters) stripped_params = params.delete_if { |key| key == 'sz' } # don't return an empty Hash since that would result # in URLs with a trailing ? character: http://image.url? return nil if stripped_params.empty? URI.encode_www_form(stripped_params) end
token_info(access_token)
click to toggle source
# File lib/omniauth/strategies/google_oauth2.rb, line 230 def token_info(access_token) return nil unless access_token @token_info ||= Hash.new do |h, k| h[k] = client.request(:get, 'https://www.googleapis.com/oauth2/v3/tokeninfo', params: { access_token: access_token }).parsed end @token_info[access_token] end
verified_email()
click to toggle source
# File lib/omniauth/strategies/google_oauth2.rb, line 163 def verified_email raw_info['email_verified'] ? raw_info['email'] : nil end
verify_hd(access_token)
click to toggle source
# File lib/omniauth/strategies/google_oauth2.rb, line 247 def verify_hd(access_token) return true unless options.hd @raw_info ||= access_token.get(USER_INFO_URL).parsed options.hd = options.hd.call if options.hd.is_a? Proc allowed_hosted_domains = Array(options.hd) raise CallbackError.new(:invalid_hd, 'Invalid Hosted Domain') unless allowed_hosted_domains.include?(@raw_info['hd']) || options.hd == '*' true end
verify_token(access_token)
click to toggle source
# File lib/omniauth/strategies/google_oauth2.rb, line 240 def verify_token(access_token) return false unless access_token token_info = token_info(access_token) token_info['aud'] == options.client_id || options.authorized_client_ids.include?(token_info['aud']) end