Created
August 9, 2010 18:19
-
-
Save RSpace/515846 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
# Require our custom warden strategy for Google oauth | |
require 'warden/o_auth/strategy/google' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Warden::AuthenticationFailureApp < Devise::FailureApp | |
# We have a failed login attempt, perhaps it's a Google login with a new user that must be created | |
def respond | |
# HACK: We set @scope to user here to make devise happy | |
@scope = :user | |
# Do we have a Google Access token? | |
if access_token = (((env['warden.options'] || {})[:oauth] || {})[:google] || {})[:access_token] | |
# Get email of authenticated user | |
authenticated_email = ActiveSupport::JSON.decode(access_token.get('https://www.googleapis.com/userinfo/email?alt=json').body)['data']['email'] | |
# Find or create user | |
user = User.find_or_create_by_email(authenticated_email) | |
# Sign in the user and redirect | |
env['warden'].set_user(user) | |
redirect_to root_path | |
else | |
super | |
end | |
else | |
super | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Devise.setup do |config| | |
config.warden do |manager| | |
manager.oauth(:google) do |google| | |
google.consumer_key = ApplicationSettings::GoogleApps::DOMAIN | |
google.consumer_secret = ApplicationSettings::GoogleApps::CONSUMER_SECRET | |
google.options( | |
:site => "https://www.google.com", | |
:request_token_path => "/accounts/OAuthGetRequestToken?hd=#{ApplicationSettings::GoogleApps::DOMAIN}", | |
:access_token_path => "/accounts/OAuthGetAccessToken?hd=#{ApplicationSettings::GoogleApps::DOMAIN}", | |
:authorize_path => "/accounts/OAuthAuthorizeToken?hd=#{ApplicationSettings::GoogleApps::DOMAIN}" | |
) | |
end | |
manager.default_strategies(:scope => :user).unshift :google_oauth | |
manager.failure_app = Warden::AuthenticationFailureApp | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
gem 'devise' | |
gem 'warden_oauth' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Warden::OAuth::Strategy::Google < Warden::OAuth::Strategy | |
# Override request token, passing in the scopes we need, in this case the email of the authenticated user | |
# http://code.google.com/intl/da/apis/accounts/docs/OAuth_ref.html#RequestToken | |
# http://code.google.com/intl/da/apis/apps/profiles/developers_guide_protocol.html | |
# http://code.google.com/intl/da/apis/gdata/faq.html#AuthScopes | |
def request_token | |
host_with_port = Warden::OAuth::Utils.host_with_port(request) | |
@request_token ||= consumer.get_request_token({:oauth_callback => host_with_port}, {:scope => "https://www.googleapis.com/auth/userinfo#email"}) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment