Skip to content
Browse files

Merge branch 'master' of https://github.com/sleekr/omniauth

Conflicts:
	oa-oauth/lib/omniauth/oauth.rb
  • Loading branch information...
2 parents 9ddad05 + 82c6ee7 commit e3a08a781fbffafeadd51259ed0a9b870ca872e8 @sferik sferik committed May 25, 2011
View
2 oa-oauth/lib/omniauth/oauth.rb
@@ -18,6 +18,8 @@ module Strategies
autoload :GitHub, 'omniauth/strategies/github'
autoload :Goodreads, 'omniauth/strategies/goodreads'
autoload :Google, 'omniauth/strategies/google'
+ autoload :GoogleHealth, 'omniauth/strategies/google_health'
+ autoload :GoogleHealthSandbox,'omniauth/strategies/google_health_sandbox'
autoload :Gowalla, 'omniauth/strategies/gowalla'
autoload :Hyves, 'omniauth/strategies/hyves'
autoload :Identica, 'omniauth/strategies/identica'
View
76 oa-oauth/lib/omniauth/strategies/google_health.rb
@@ -0,0 +1,76 @@
+require 'omniauth/oauth'
+require 'multi_json'
+
+module OmniAuth
+ module Strategies
+ #
+ # Authenticate to Google via OAuth and retrieve basic
+ # user information.
+ #
+ # Usage:
+ #
+ # use OmniAuth::Strategies::Google, 'consumerkey', 'consumersecret'
+ #
+ class GoogleHealth < OmniAuth::Strategies::OAuth
+ def initialize(app, consumer_key = nil, consumer_secret = nil, options = {}, &block)
+ client_options = {
+ :site => 'https://www.google.com',
+ :request_token_path => '/accounts/OAuthGetRequestToken',
+ :access_token_path => '/accounts/OAuthGetAccessToken',
+ :authorize_path => '/accounts/OAuthAuthorizeToken'
+ }
+
+ super(app, :google_health, consumer_key, consumer_secret, client_options, options)
+ end
+
+ def auth_hash
+ ui = user_info
+ OmniAuth::Utils.deep_merge(super, {
+ 'uid' => ui['uid'],
+ 'user_info' => ui,
+ 'extra' => {'user_hash' => user_hash}
+ })
+ end
+
+ def user_info
+ email = user_hash['feed']['id']['$t']
+
+ name = user_hash['feed']['author'].first['name']['$t']
+ name = email if name.strip == '(unknown)'
+
+ {
+ 'email' => email,
+ 'uid' => email,
+ 'name' => name
+ }
+ end
+
+ def user_hash
+ # Google is very strict about keeping authorization and
+ # authentication separated.
+ # They give no endpoint to get a user's profile directly that I can
+ # find. We *can* get their name and email out of the contacts feed,
+ # however. It will fail in the extremely rare case of a user who has
+ # a Google Account but has never even signed up for Gmail. This has
+ # not been seen in the field.
+ @user_hash ||= MultiJson.decode(@access_token.get("http://www.google.com/health/feeds/profile/default/default?digest=true&alt=json").body)
+ end
+
+ # Monkeypatch OmniAuth to pass the scope in the consumer.get_request_token call
+ def request_phase
+ request_token = consumer.get_request_token({:oauth_callback => callback_url}, {:scope => "http://www.google.com/health/feeds"})
+
+ (session['oauth']||={})[name.to_s] = {'callback_confirmed' => request_token.callback_confirmed?, 'request_token' => request_token.token, 'request_secret' => request_token.secret}
+ r = Rack::Response.new
+
+ if request_token.callback_confirmed?
+ r.redirect(request_token.authorize_url)
+ else
+ r.redirect(request_token.authorize_url(:oauth_callback => callback_url))
+ end
+
+ r.finish
+ end
+ end
+ end
+end
View
76 oa-oauth/lib/omniauth/strategies/google_health_sandbox.rb
@@ -0,0 +1,76 @@
+require 'omniauth/oauth'
+require 'multi_json'
+
+module OmniAuth
+ module Strategies
+ #
+ # Authenticate to Google via OAuth and retrieve basic
+ # user information.
+ #
+ # Usage:
+ #
+ # use OmniAuth::Strategies::Google, 'consumerkey', 'consumersecret'
+ #
+ class GoogleHealthSandbox < OmniAuth::Strategies::OAuth
+ def initialize(app, consumer_key = nil, consumer_secret = nil, options = {}, &block)
+ client_options = {
+ :site => 'https://www.google.com',
+ :request_token_path => '/accounts/OAuthGetRequestToken',
+ :access_token_path => '/accounts/OAuthGetAccessToken',
+ :authorize_path => '/accounts/OAuthAuthorizeToken'
+ }
+
+ super(app, :google_health_sandbox, consumer_key, consumer_secret, client_options, options)
+ end
+
+ def auth_hash
+ ui = user_info
+ OmniAuth::Utils.deep_merge(super, {
+ 'uid' => ui['uid'],
+ 'user_info' => ui,
+ 'extra' => {'user_hash' => user_hash}
+ })
+ end
+
+ def user_info
+ email = user_hash['feed']['id']['$t']
+
+ name = user_hash['feed']['author'].first['name']['$t']
+ name = email if name.strip == '(unknown)'
+
+ {
+ 'email' => email,
+ 'uid' => email,
+ 'name' => name
+ }
+ end
+
+ def user_hash
+ # Google is very strict about keeping authorization and
+ # authentication separated.
+ # They give no endpoint to get a user's profile directly that I can
+ # find. We *can* get their name and email out of the contacts feed,
+ # however. It will fail in the extremely rare case of a user who has
+ # a Google Account but has never even signed up for Gmail. This has
+ # not been seen in the field.
+ @user_hash ||= MultiJson.decode(@access_token.get("http://www.google.com/h9/feeds/profile/default/default?digest=true&oauth_signature_method=RSA-SHA1&oauth_version=1.0").body)
+ end
+
+ # Monkeypatch OmniAuth to pass the scope in the consumer.get_request_token call
+ def request_phase
+ request_token = consumer.get_request_token({:oauth_callback => callback_url}, {:scope => "http://www.google.com/h9/feeds"})
+
+ (session['oauth']||={})[name.to_s] = {'callback_confirmed' => request_token.callback_confirmed?, 'request_token' => request_token.token, 'request_secret' => request_token.secret}
+ r = Rack::Response.new
+
+ if request_token.callback_confirmed?
+ r.redirect(request_token.authorize_url)
+ else
+ r.redirect(request_token.authorize_url(:oauth_callback => callback_url))
+ end
+
+ r.finish
+ end
+ end
+ end
+end
View
5 oa-oauth/spec/omniauth/strategies/google_health_sandbox_spec.rb
@@ -0,0 +1,5 @@
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
+
+describe OmniAuth::Strategies::GoogleHealthSandbox do
+ it_should_behave_like 'an oauth strategy'
+end
View
5 oa-oauth/spec/omniauth/strategies/google_health_spec.rb
@@ -0,0 +1,5 @@
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
+
+describe OmniAuth::Strategies::GoogleHealth do
+ it_should_behave_like 'an oauth strategy'
+end

0 comments on commit e3a08a7

Please sign in to comment.
Something went wrong with that request. Please try again.