Module: AuthlogicOpenid::ActsAsAuthentic::Methods

Defined in:
lib/authlogic_openid/acts_as_authentic.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

Set up some simple validations



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/authlogic_openid/acts_as_authentic.rb', line 42

def self.included(klass)
  return if !klass.column_names.include?("openid_identifier")
  
  klass.class_eval do
    validates_uniqueness_of :openid_identifier, :scope => validations_scope, :if => :using_openid?
    validate :validate_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?)
  end
end

Instance Method Details

#openid_identifier=(value) ⇒ Object

Set the openid_identifier field and also resets the persistence_token if this value changes.



55
56
57
58
59
60
# File 'lib/authlogic_openid/acts_as_authentic.rb', line 55

def openid_identifier=(value)
  write_attribute(:openid_identifier, value.blank? ? nil : OpenIdAuthentication.normalize_identifier(value))
  reset_persistence_token if openid_identifier_changed?
rescue OpenIdAuthentication::InvalidOpenId => e
  @openid_error = e.message
end

#save(perform_validation = true) {|result| ... } ⇒ Object

This is where all of the magic happens. This is where we hook in and add all of the OpenID sweetness.

I had to take this approach because when authenticating with OpenID nonces and what not are stored in database tables. That being said, the whole save process for ActiveRecord is wrapped in a transaction. Trying to authenticate with OpenID in a transaction is not good because that transaction be get rolled back, thus reversing all of the OpenID inserts and making OpenID authentication fail every time. So We need to step outside of the transaction and do our OpenID madness.

Another advantage of taking this approach is that we can set fields from their OpenID profile before we save the record, if their OpenID provider supports it.

Yields:

  • (result)


72
73
74
75
76
77
# File 'lib/authlogic_openid/acts_as_authentic.rb', line 72

def save(perform_validation = true, &block)
  return false if perform_validation && block_given? && authenticate_with_openid? && !authenticate_with_openid
  result = super
  yield(result) if block_given?
  result
end