Permalink
Browse files

Moved bits of Session around to make way for abstracting the adapter

  • Loading branch information...
1 parent b51535f commit 01500f9accc77d8da932bbf026022fa86494fb7d @james2m committed Jan 5, 2011
@@ -2,16 +2,13 @@
require 'ostruct'
require 'singleton'
-require 'authlogic_facebook_koala/config'
-require 'authlogic_facebook_koala/adapter'
-require 'authlogic_facebook_koala/acts_as_authentic'
-require 'authlogic_facebook_koala/session'
-require 'authlogic_facebook_koala/helper'
if ActiveRecord::Base.respond_to?(:add_acts_as_authentic_module)
+ require 'authlogic_facebook_koala/acts_as_authentic'
+ require 'authlogic_facebook_koala/session/config'
+ require 'authlogic_facebook_koala/session/adapter'
+ require 'authlogic_facebook_koala/session/facebook'
+ require 'authlogic_facebook_koala/session/base'
+ require 'authlogic_facebook_koala/helper'
ActiveRecord::Base.send :include, AuthlogicFacebookKoala::ActsAsAuthentic
-
- Authlogic::Session::Base.send :extend, AuthlogicFacebookKoala::Config
- Authlogic::Session::Base.send :include, AuthlogicFacebookKoala::Session
- Authlogic::Session::Base.send :include, AuthlogicFacebookKoala::Adapter
end
@@ -1,35 +0,0 @@
-module AuthlogicFacebookKoala
-
- module Adapter
-
- def facebook_session
- @facebook_session ||= begin
- if controller.cookies.has_key?("fbs_#{facebook_app_id}")
- oauth = Koala::Facebook::OAuth.new(facebook_app_id, facebook_secret_key)
- if oauth.respond_to?(:get_user_info_from_cookie)
- user_info = oauth.get_user_info_from_cookie(controller.cookies)
- else
- user_info = oauth.get_user_from_cookie(controller.cookies)
- end
- OpenStruct.new( user_info )
- end
- end
- end
-
- def facebook_session?
- !facebook_session.nil?
- end
-
- def facebook_user
- @facebook_user ||= begin
- facebook_graph = Koala::Facebook::GraphAPI.new(facebook_session.access_token)
- user = facebook_graph.get_object('me')
- user[:uid] = user.delete('id')
- OpenStruct.new( user )
- end if facebook_session?
- end
-
- end
-
-end
-
@@ -1,80 +0,0 @@
-module AuthlogicFacebookKoala
-
- module Config
-
- def self.extended(klass)
- (class << klass; self end).send(:define_method, :default_config) do
- @default_config ||= begin
- config_file = File.join(Rails.root, 'config', facebook_config_file)
- OpenStruct.new(File.exist?(config_file) ? YAML.load_file(config_file)[Rails.env] : {})
- end
- end
- end
-
- # Specify your config file if using one. If not then you need to specify
- # facebook_app_id, facebook_secret_key & facebook_api_key
- #
- # * <tt>Default:</tt> facebook.yml
- # * <tt>Accepts:</tt> String
- def facebook_config_file(value=nil)
- rw_config(:facebook_config_file, value, 'facebook.yml')
- end
- alias_method :facebook_config_file=, :facebook_config_file
-
- # Specify your app_id.
- #
- # * <tt>Default:</tt> app_id in config/facebook.yml
- # * <tt>Accepts:</tt> String
- def facebook_app_id(value=nil)
- rw_config(:facebook_app_id, value, default_config.app_id)
- end
- alias_method :facebook_app_id=, :facebook_app_id
-
- # Specify your secret_key.
- #
- # * <tt>Default:</tt> secret_key in config/facebook.yml
- # * <tt>Accepts:</tt> String
- def facebook_secret_key(value=nil)
- rw_config(:facebook_secret_key, value, default_config.secret_key)
- end
- alias_method :facebook_secret_key=, :facebook_secret_key
-
- # Specify your api_key.
- #
- # * <tt>Default:</tt> api_key in config/facebook.yml
- # * <tt>Accepts:</tt> String
- def facebook_api_key(value=nil)
- rw_config(:facebook_api_key, value, default_config.api_key)
- end
- alias_method :facebook_api_key=, :facebook_api_key
-
- # What user field should be used for the facebook UID?
- #
- # * <tt>Default:</tt> :facebook_uid
- # * <tt>Accepts:</tt> Symbol
- def facebook_uid_field(value=nil)
- rw_config(:facebook_uid_field, value, :facebook_uid)
- end
- alias_method :facebook_uid_field=, :facebook_uid_field
-
- # What method should be used to find the facebook account?
- #
- # * <tt>Default:</tt> :find_by_#{facebook_uid_field}
- # * <tt>Accepts:</tt> Symbol or String
- def facebook_finder(value=nil)
- rw_config(:facebook_finder, value, nil)
- end
- alias_method :facebook_finder=, :facebook_finder
-
- # Should a new user be automatically created if there is no user with
- # given facebook uid?
- #
- # * <tt>Default:</tt> false
- # * <tt>Accepts:</tt> Boolean
- def facebook_auto_register(value=nil)
- rw_config(:facebook_auto_register, value, false)
- end
- alias_method :facebook_auto_register=, :facebook_auto_register
- end
-
-end
@@ -1,68 +0,0 @@
-module AuthlogicFacebookKoala
- module Session
-
- def self.included(klass)
-
- klass.class_eval do
- attr_accessor :skip_facebook_authentication
- validate :validate_by_facebook, :if => :authenticating_with_facebook?
- end
-
- end
-
- def logged_in_with_facebook?
- @logged_in_with_facebook
- end
-
- protected
- # Override this if you want only some requests to use facebook
- def authenticating_with_facebook?
- !skip_facebook_authentication && !authenticating_with_unauthorized_record? && facebook_session?
- end
-
- private
-
- def validate_by_facebook
- facebook_uid = facebook_session.uid
- self.attempted_record = klass.send(facebook_finder, facebook_uid)
-
- if self.attempted_record || !facebook_auto_register?
- return @logged_in_with_facebook = !!self.attempted_record
- else
- self.attempted_record = klass.new
- self.attempted_record.send(:"#{facebook_uid_field}=", facebook_uid)
- if self.attempted_record.respond_to?(:before_connect)
- self.attempted_record.send(:before_connect, facebook_session)
- end
-
- @logged_in_with_facebook = true
- return self.attempted_record.save(false)
- end
- end
-
- def facebook_app_id
- self.class.facebook_app_id
- end
-
- def facebook_api_key
- self.class.facebook_api_key
- end
-
- def facebook_secret_key
- self.class.facebook_secret_key
- end
-
- def facebook_auto_register?
- self.class.facebook_auto_register
- end
-
- def facebook_uid_field
- self.class.facebook_uid_field
- end
-
- def facebook_finder
- self.class.facebook_finder || "find_by_#{facebook_uid_field}"
- end
-
- end
-end
@@ -0,0 +1,35 @@
+module AuthlogicFacebookKoala
+ module Session
+ module Adapter
+
+ def facebook_session
+ @facebook_session ||= begin
+ if controller.cookies.has_key?("fbs_#{facebook_app_id}")
+ oauth = Koala::Facebook::OAuth.new(facebook_app_id, facebook_secret_key)
+ if oauth.respond_to?(:get_user_info_from_cookie)
+ user_info = oauth.get_user_info_from_cookie(controller.cookies)
+ else
+ user_info = oauth.get_user_from_cookie(controller.cookies)
+ end
+ OpenStruct.new( user_info )
+ end
+ end
+ end
+
+ def facebook_session?
+ !facebook_session.nil?
+ end
+
+ def facebook_user
+ @facebook_user ||= begin
+ facebook_graph = Koala::Facebook::GraphAPI.new(facebook_session.access_token)
+ user = facebook_graph.get_object('me')
+ user[:uid] = user.delete('id')
+ OpenStruct.new( user )
+ end if facebook_session?
+ end
+
+ end
+ end
+end
+
@@ -0,0 +1,10 @@
+module Authlogic
+ module Session
+ class Base
+ extend AuthlogicFacebookKoala::Session::Config
+ include AuthlogicFacebookKoala::Session::Adapter
+ include AuthlogicFacebookKoala::Session::Facebook
+ end
+ end
+end
+
@@ -0,0 +1,82 @@
+module AuthlogicFacebookKoala
+ module Session
+
+ module Config
+
+ def self.extended(klass)
+ (class << klass; self end).send(:define_method, :default_config) do
+ @default_config ||= begin
+ config_file = File.join(Rails.root, 'config', facebook_config_file)
+ OpenStruct.new(File.exist?(config_file) ? YAML.load_file(config_file)[Rails.env] : {})
+ end
+ end
+ end
+
+ # Specify your config file if using one. If not then you need to specify
+ # facebook_app_id, facebook_secret_key & facebook_api_key
+ #
+ # * <tt>Default:</tt> facebook.yml
+ # * <tt>Accepts:</tt> String
+ def facebook_config_file(value=nil)
+ rw_config(:facebook_config_file, value, 'facebook.yml')
+ end
+ alias_method :facebook_config_file=, :facebook_config_file
+
+ # Specify your app_id.
+ #
+ # * <tt>Default:</tt> app_id in config/facebook.yml
+ # * <tt>Accepts:</tt> String
+ def facebook_app_id(value=nil)
+ rw_config(:facebook_app_id, value, default_config.app_id)
+ end
+ alias_method :facebook_app_id=, :facebook_app_id
+
+ # Specify your secret_key.
+ #
+ # * <tt>Default:</tt> secret_key in config/facebook.yml
+ # * <tt>Accepts:</tt> String
+ def facebook_secret_key(value=nil)
+ rw_config(:facebook_secret_key, value, default_config.secret_key)
+ end
+ alias_method :facebook_secret_key=, :facebook_secret_key
+
+ # Specify your api_key.
+ #
+ # * <tt>Default:</tt> api_key in config/facebook.yml
+ # * <tt>Accepts:</tt> String
+ def facebook_api_key(value=nil)
+ rw_config(:facebook_api_key, value, default_config.api_key)
+ end
+ alias_method :facebook_api_key=, :facebook_api_key
+
+ # What user field should be used for the facebook UID?
+ #
+ # * <tt>Default:</tt> :facebook_uid
+ # * <tt>Accepts:</tt> Symbol
+ def facebook_uid_field(value=nil)
+ rw_config(:facebook_uid_field, value, :facebook_uid)
+ end
+ alias_method :facebook_uid_field=, :facebook_uid_field
+
+ # What method should be used to find the facebook account?
+ #
+ # * <tt>Default:</tt> :find_by_#{facebook_uid_field}
+ # * <tt>Accepts:</tt> Symbol or String
+ def facebook_finder(value=nil)
+ rw_config(:facebook_finder, value, nil)
+ end
+ alias_method :facebook_finder=, :facebook_finder
+
+ # Should a new user be automatically created if there is no user with
+ # given facebook uid?
+ #
+ # * <tt>Default:</tt> false
+ # * <tt>Accepts:</tt> Boolean
+ def facebook_auto_register(value=nil)
+ rw_config(:facebook_auto_register, value, false)
+ end
+ alias_method :facebook_auto_register=, :facebook_auto_register
+ end
+
+ end
+end
Oops, something went wrong.

0 comments on commit 01500f9

Please sign in to comment.