lib/authlogic_connect/openid/user.rb in authlogic-connect-0.0.3.4 vs lib/authlogic_connect/openid/user.rb in authlogic-connect-0.0.3.6
- old
+ new
@@ -1,92 +1,62 @@
module AuthlogicConnect::Openid
module User
def self.included(base)
base.class_eval do
- add_acts_as_authentic_module(InstanceMethods, :prepend)
+ add_acts_as_authentic_module(AuthlogicConnect::Openid::Process, :prepend)
+ add_acts_as_authentic_module(InstanceMethods, :append)
end
end
module InstanceMethods
-
- def self.included(base)
- return if !base.column_names.include?("openid_identifier")
-
+
+ def self.included(base)
base.class_eval do
- validates_uniqueness_of :openid_identifier, :scope => validations_scope, :if => :using_openid?
- validate :validate_openid
+ validate :validate_by_openid, :if => :authenticating_with_openid?
+
validates_length_of_password_field_options validates_length_of_password_field_options.merge(:if => :validate_password_with_openid?)
validates_confirmation_of_password_field_options validates_confirmation_of_password_field_options.merge(:if => :validate_password_with_openid?)
validates_length_of_password_confirmation_field_options validates_length_of_password_confirmation_field_options.merge(:if => :validate_password_with_openid?)
validates_length_of_login_field_options validates_length_of_login_field_options.merge(:if => :validate_password_with_openid?)
validates_format_of_login_field_options validates_format_of_login_field_options.merge(:if => :validate_password_with_openid?)
end
end
- def openid_identifier=(value)
- write_attribute(:openid_identifier, value.blank? ? nil : value.to_s.normalize_identifier)
- reset_persistence_token if openid_identifier_changed?
- rescue Exception => e
- @openid_error = e.message
+ def authenticate_with_openid
+ @openid_error = nil
+ if !openid_response?
+ save_openid_session
+ else
+ restore_attributes
+ end
+ options = {}
+ options[:return_to] = auth_callback_url(:for_model => "1", :action => "create")
+ auth_controller.send(:authenticate_with_open_id, openid_identifier, options) do |result, openid_identifier|
+ create_openid_token(result, openid_identifier)
+ return true
+ end
+ return false
end
- def save_with_openid(perform_validation = true, &block)
- return false if perform_validation && block_given? && authenticating_with_openid? && !authenticating_with_openid
- return true
+ def create_openid_token(result, openid_identifier)
+ if result.unsuccessful?
+ @openid_error = result.message
+ elsif Token.find_by_key(openid_identifier.normalize_identifier)
+ else
+ token = OpenidToken.new(:key => openid_identifier)
+ self.tokens << token
+ self.active_token = token
+ end
end
- protected
-
- def validate_openid
- errors.add(:openid_identifier, "had the following error: #{@openid_error}") if @openid_error
+ def attributes_to_save
+ attr_list = [:id, :password, crypted_password_field, password_salt_field, :persistence_token, :perishable_token, :single_access_token, :login_count,
+ :failed_login_count, :last_request_at, :current_login_at, :last_login_at, :current_login_ip, :last_login_ip, :created_at,
+ :updated_at, :lock_version]
+ attrs_to_save = attributes.clone.delete_if do |k, v|
+ attr_list.include?(k.to_sym)
end
-
- def using_openid?
- respond_to?(:openid_identifier) && !auth_params[:openid_identifier].blank?
- end
-
- def openid_complete?
- auth_session[:openid_attributes]
- end
-
- def authenticating_with_openid?
- session_class.activated? && ((using_openid?) || openid_complete?)
- end
-
- def validate_password_with_openid?
- !using_openid? && require_password?
- end
-
- def authenticating_with_openid
- @openid_error = nil
- if !openid_complete?
- # Tell our rack callback filter what method the current request is using
- auth_session[:auth_callback_method] = auth_controller.request.method
- auth_session[:openid_attributes] = attributes_to_save
- else
- self.attributes = auth_session.delete(:openid_attributes)
- end
-
- options = {}
- options[:return_to] = auth_controller.url_for(:for_model => "1", :controller => "users", :action => "create")
- auth_controller.send(:authenticate_with_open_id, openid_identifier, options) do |result, openid_identifier, registration|
- if result.unsuccessful?
- @openid_error = result.message
- else
- self.openid_identifier = openid_identifier
- end
- return true
- end
- return false
- end
-
- def attributes_to_save
- attrs_to_save = attributes.clone.delete_if do |k, v|
- [:id, :password, crypted_password_field, password_salt_field, :persistence_token, :perishable_token, :single_access_token, :login_count,
- :failed_login_count, :last_request_at, :current_login_at, :last_login_at, :current_login_ip, :last_login_ip, :created_at,
- :updated_at, :lock_version].include?(k.to_sym)
- end
- attrs_to_save.merge!(:password => password, :password_confirmation => password_confirmation)
- end
+ attrs_to_save.merge!(:password => password, :password_confirmation => password_confirmation)
+ end
end
end
end
\ No newline at end of file