Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

* Set the openid_identifier by the one passed back by the provider.

  • Loading branch information...
commit bb54f097a1a8108400bcbd49c56d12a535693985 1 parent f7ca8cb
@binarylogic binarylogic authored
View
5 CHANGELOG.rdoc
@@ -1,3 +1,8 @@
+== 1.0.3
+
+* Added find_by_openid_identifier config option for the session.
+* Set the openid_identifier by the one passed back by the provider.
+
== 1.0.2 released 2009-3-30
* Remove config block in initializer.
View
3  lib/authlogic_openid/acts_as_authentic.rb
@@ -78,6 +78,7 @@ def authenticate_with_openid
if result.unsuccessful?
@openid_error = result.message
else
+ self.openid_identifier = openid_identifier
map_openid_registration(registration)
end
@@ -90,7 +91,7 @@ def authenticate_with_openid
def map_openid_registration(registration)
self.name ||= registration[:fullname] if respond_to?(:name) && !registration[:fullname].blank?
self.first_name ||= registration[:fullname].split(" ").first if respond_to?(:first_name) && !registration[:fullname].blank?
- self.first_name ||= registration[:fullname].split(" ").last if respond_to?(:last_name) && !registration[:last_name].blank?
+ self.last_name ||= registration[:fullname].split(" ").last if respond_to?(:last_name) && !registration[:last_name].blank?
end
def validate_openid
View
122 lib/authlogic_openid/session.rb
@@ -4,58 +4,92 @@ module Session
# Add a simple openid_identifier attribute and some validations for the field.
def self.included(klass)
klass.class_eval do
- attr_reader :openid_identifier
- validate :validate_openid_error
- validate :validate_by_openid, :if => :authenticating_with_openid?
+ extend Config
+ include Methods
end
end
- # Hooks into credentials so that you can pass an :openid_identifier key.
- def credentials=(value)
- super
- values = value.is_a?(Array) ? value : [value]
- hash = values.first.is_a?(Hash) ? values.first.with_indifferent_access : nil
- self.openid_identifier = hash[:openid_identifier] if !hash.nil? && hash.key?(:openid_identifier)
- end
-
- def openid_identifier=(value)
- @openid_identifier = value.blank? ? nil : OpenIdAuthentication.normalize_identifier(value)
- @openid_error = nil
- rescue OpenIdAuthentication::InvalidOpenId => e
- @openid_identifier = nil
- @openid_error = e.message
- end
-
- # Cleaers out the block if we are authenticating with OpenID, so that we can redirect without a DoubleRender
- # error.
- def save(&block)
- block = nil if !openid_identifier.blank?
- super(&block)
- end
-
- private
- def authenticating_with_openid?
- !openid_identifier.blank? || (controller.params[:open_id_complete] && controller.params[:for_session])
+ module Config
+ # What method should we call to find a record by the openid_identifier?
+ # This is useful if you want to store multiple openid_identifiers for a single record.
+ # You could do something like:
+ #
+ # class User < ActiveRecord::Base
+ # def self.find_by_openid_identifier(identifier)
+ # user.first(:conditions => {:openid_identifiers => {:identifier => identifier}})
+ # end
+ # end
+ #
+ # Obviously the above depends on what you are calling your assocition, etc. But you get the point.
+ #
+ # * <tt>Default:</tt> :find_by_openid_identifier
+ # * <tt>Accepts:</tt> Symbol
+ def find_by_openid_identifier_method(value = nil)
+ config(:find_by_openid_identifier_method, value, :find_by_openid_identifier)
end
+ alias_method :find_by_openid_identifier_method=, :find_by_openid_identifier_method
+ end
- def validate_by_openid
- controller.send(:authenticate_with_open_id, openid_identifier, :return_to => controller.url_for(:for_session => "1")) do |result, openid_identifier|
- if result.unsuccessful?
- errors.add_to_base(result.message)
- return
- end
-
- self.attempted_record = klass.find_by_openid_identifier(openid_identifier)
-
- if !attempted_record
- errors.add(:openid_identifier, "did not match any users in our database, have you set up your account to use OpenID?")
- return
- end
+ module Methods
+ def self.included(klass)
+ klass.class_eval do
+ attr_reader :openid_identifier
+ validate :validate_openid_error
+ validate :validate_by_openid, :if => :authenticating_with_openid?
end
end
- def validate_openid_error
- errors.add(:openid_identifier, @openid_error) if @openid_error
+ # Hooks into credentials so that you can pass an :openid_identifier key.
+ def credentials=(value)
+ super
+ values = value.is_a?(Array) ? value : [value]
+ hash = values.first.is_a?(Hash) ? values.first.with_indifferent_access : nil
+ self.openid_identifier = hash[:openid_identifier] if !hash.nil? && hash.key?(:openid_identifier)
+ end
+
+ def openid_identifier=(value)
+ @openid_identifier = value.blank? ? nil : OpenIdAuthentication.normalize_identifier(value)
+ @openid_error = nil
+ rescue OpenIdAuthentication::InvalidOpenId => e
+ @openid_identifier = nil
+ @openid_error = e.message
end
+
+ # Cleaers out the block if we are authenticating with OpenID, so that we can redirect without a DoubleRender
+ # error.
+ def save(&block)
+ block = nil if !openid_identifier.blank?
+ super(&block)
+ end
+
+ private
+ def authenticating_with_openid?
+ !openid_identifier.blank? || (controller.params[:open_id_complete] && controller.params[:for_session])
+ end
+
+ def find_by_openid_identifier_method
+ self.class.find_by_openid_identifier_method
+ end
+
+ def validate_by_openid
+ controller.send(:authenticate_with_open_id, openid_identifier, :return_to => controller.url_for(:for_session => "1")) do |result, openid_identifier|
+ if result.unsuccessful?
+ errors.add_to_base(result.message)
+ return
+ end
+
+ self.attempted_record = klass.send(find_by_openid_identifier_method, openid_identifier)
+
+ if !attempted_record
+ errors.add(:openid_identifier, "did not match any users in our database, have you set up your account to use OpenID?")
+ return
+ end
+ end
+ end
+
+ def validate_openid_error
+ errors.add(:openid_identifier, @openid_error) if @openid_error
+ end
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.