Skip to content
Browse files

oauth call back details

  • Loading branch information...
1 parent 545897e commit 64af9896455372c024ae1151d080806ac7e89223 bhbryant committed Sep 18, 2010
View
11 lib/devise_oauth2_authenticatable.rb
@@ -9,6 +9,7 @@
require 'devise_oauth2_authenticatable/routes'
#require 'devise_oauth2_authenticatable/controller_filters'
require 'devise_oauth2_authenticatable/view_helpers'
+require 'devise_oauth2_authenticatable/session_controller_extension.rb'
module Devise
@@ -29,15 +30,19 @@ module Devise
mattr_accessor :oauth2_auto_create_account
@@oauth2_auto_create_account = true
+
+ mattr_accessor :oauth2_callback_path
+ @@oauth2_callback_path = '/oauth_callback'
+
def self.oauth2_client
@@oauth2_client ||= OAuth2::Client.new(OAUTH2_CONFIG['client_id'], OAUTH2_CONFIG['client_secret'], :site => OAUTH2_CONFIG['authorization_server'])
end
- def self.session_sign_in_url(request, mapping)
- url = URI.parse(request.url)
+ def self.session_sign_in_url(request, mapping, params = nil)
+ url = URI.parse(URI.escape(request.url,/\|/))
url.path = "#{mapping.parsed_path}/#{mapping.path_names[:sign_in]}"
- url.query = nil
+ url.query = params
url.to_s
end
View
2 lib/devise_oauth2_authenticatable/locales/en.yml
@@ -3,7 +3,7 @@ en:
sessions:
oauth2_invalid: "Could not login. Invalid account."
oauth2_timeout: "OAuth2 session expired., please sign in again to continue."
- oauth2_authenticity_token: "Something went wrong. For security reasons, please sign in again." # Revise this message =)
+ oauth2_authenticity_token: "Unable to sign in."
oauth2_actions:
sign_in: "Sign in" # NOTE: Not used for the default Facebook Connect button.
sign_out: "Sign out"
View
30 lib/devise_oauth2_authenticatable/model.rb
@@ -83,7 +83,7 @@ def oauth2_connected?
def on_before_oauth2_auto_create(oauth2_user_attributes)
if self.respond_to?(:before_oauth2_auto_create)
- self.send(:before_oauth2_auto_create, oauth2_user_attributes) rescue nil
+ self.send(:before_oauth2_auto_create, oauth2_user_attributes)
end
end
@@ -103,18 +103,37 @@ def on_before_oauth2_auto_create(oauth2_user_attributes)
def on_after_oauth2_connect(oauth2_user_attributes)
if self.respond_to?(:after_oauth2_auto_create)
- self.send(:after_oauth2_auto_create, oauth2_user_attributes) rescue nil
+ self.send(:after_oauth2_auto_create, oauth2_user_attributes)
end
+ on_after_oauth2_connect
end
-
+ # Hook that gets called *after* a connection (each time). Useful for
+ # fetching additional user info (etc.) from OAuth2.
+ #
+ # Default: Do nothing.
+ #
+ # == Example:
+ #
+ # # Overridden in OAuth2 Connect:able model, e.g. "User".
+ # #
+ # def after_oauth2_connect(oauth2_user_attributes)
+ # # See "on_before_oauth2_connect" example.
+ # end
+ #
+ def on_after_oauth2_connect(oauth2_user_attributes)
+ if self.respond_to?(:after_oauth2_connect)
+ self.send(:after_oauth2_connect, oauth2_user_attributes)
+ end
+ end
+=begin
# Optional: Store session key.
#
def store_session(using_token)
if self.token != using_token
self.update_attribute(self.send(:"#{self.class.oauth2_token_field}"), using_token)
end
end
-
+=end
protected
# Passwords are always required if it's a new rechord and no oauth_id exists, or if the password
@@ -177,7 +196,8 @@ def authenticate_with_oauth2(oauth2_id, oauth2_token)
#
def find_for_oauth2(uid, conditions = {})
- self.find_by_oauth2_uid(uid, :conditions => conditions)
+ self.send(:"find_by_#{self.oauth2_uid_field}", uid, :conditions => conditions)
+ #self.find_by_oauth2_uid(uid, :conditions => conditions)
end
View
11 lib/devise_oauth2_authenticatable/routes.rb
@@ -6,6 +6,15 @@
# Setup routes for +OAuth2SessionsController+.
#
- alias :oauth2_authenticatable :database_authenticatable
+ # alias :oauth2_authenticatable :database_authenticatable
+
+
+ # Setup routes for +OAuth2SessionsController+.
+ #
+ def oauth2_authenticatable(routes, mapping)
+
+ database_authenticatable(routes, mapping)
+ routes.oauth_callback ::Devise.oauth2_callback_path, :controller => 'sessions', :action => 'oauth_callback'
+ end
end
View
20 lib/devise_oauth2_authenticatable/strategy.rb
@@ -28,11 +28,29 @@ def authenticate!
begin
+ #query_params = params.has_key?(:popup) ? "popup=#{params[:popup]}" : nil
+ callback_url = URI.parse(URI.escape(request.url, /\|/))
+ callback_url.query = params.has_key?(:popup) ? "popup=#{params[:popup]}" : nil
+
+ # Verify User Auth code and get access token from auth server: will error on failue
+ access_token = Devise::oauth2_client.web_server.get_access_token(
+ params[:code],
+ :redirect_uri => callback_url.to_s
+ )
+
+
+=begin
+
+ query_params = params.has_key?(:popup) ? "popup=#{params[:popup]}" : nil
+ sign_in_url = Devise::session_sign_in_url(request, mapping, query_params)
# Verify User Auth code and get access token from auth server: will error on failue
access_token = Devise::oauth2_client.web_server.get_access_token(
- params[:code], :redirect_uri => Devise::session_sign_in_url(request,mapping)
+ params[:code], :redirect_uri => sign_in_url
)
+=end
+
+
# retrieve user attributes
# Get user details from OAuth2 Service
View
12 lib/devise_oauth2_authenticatable/view_helpers.rb
@@ -9,15 +9,19 @@ module Oauth2Authenticatable #:nodoc:
module Helpers
# Creates the link to
- def link_to_oauth2(link_text, options={})
+ def link_to_oauth2(scope, link_text, options={})
+
+ callback_url = send("#{scope.to_s}_oauth_callback_url", options[:params] || {})
+
+ # callback_url = URI.parse(URI.escape(request.url, /\|/))
+ # callback_url.query = params.has_key?(:popup) ? "popup=#{params[:popup]}" : nil
- session_sign_in_url = Devise::session_sign_in_url(request,::Devise.mappings[:user])
link_to link_text, Devise::oauth2_client.web_server.authorize_url(
- :redirect_uri => session_sign_in_url,
+ :redirect_uri => callback_url ,
:scope => Devise::requested_scope
- ), options
+ ) , options
end
View
3 rails/init.rb
@@ -1,4 +1,5 @@
# encoding: utf-8
-Devise::OAUTH2_CONFIG = YAML.load_file(Rails.root.join('config', 'oauth2_config.yml'))[Rails.env]
+
+
require 'devise_oauth2_authenticatable'

0 comments on commit 64af989

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