heyzap-authlogic-oid 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. data/CHANGELOG.rdoc +25 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Manifest.txt +38 -0
  4. data/README.rdoc +93 -0
  5. data/Rakefile +21 -0
  6. data/init.rb +1 -0
  7. data/lib/authlogic_openid.rb +6 -0
  8. data/lib/authlogic_openid/acts_as_authentic.rb +173 -0
  9. data/lib/authlogic_openid/session.rb +131 -0
  10. data/lib/authlogic_openid/version.rb +51 -0
  11. data/test/acts_as_authentic_test.rb +105 -0
  12. data/test/fixtures/users.yml +9 -0
  13. data/test/libs/open_id_authentication/CHANGELOG +35 -0
  14. data/test/libs/open_id_authentication/README +231 -0
  15. data/test/libs/open_id_authentication/Rakefile +22 -0
  16. data/test/libs/open_id_authentication/generators/open_id_authentication_tables/open_id_authentication_tables_generator.rb +11 -0
  17. data/test/libs/open_id_authentication/generators/open_id_authentication_tables/templates/migration.rb +20 -0
  18. data/test/libs/open_id_authentication/generators/upgrade_open_id_authentication_tables/templates/migration.rb +26 -0
  19. data/test/libs/open_id_authentication/generators/upgrade_open_id_authentication_tables/upgrade_open_id_authentication_tables_generator.rb +11 -0
  20. data/test/libs/open_id_authentication/init.rb +18 -0
  21. data/test/libs/open_id_authentication/lib/open_id_authentication.rb +244 -0
  22. data/test/libs/open_id_authentication/lib/open_id_authentication/association.rb +9 -0
  23. data/test/libs/open_id_authentication/lib/open_id_authentication/db_store.rb +55 -0
  24. data/test/libs/open_id_authentication/lib/open_id_authentication/mem_cache_store.rb +73 -0
  25. data/test/libs/open_id_authentication/lib/open_id_authentication/nonce.rb +5 -0
  26. data/test/libs/open_id_authentication/lib/open_id_authentication/request.rb +23 -0
  27. data/test/libs/open_id_authentication/lib/open_id_authentication/timeout_fixes.rb +20 -0
  28. data/test/libs/open_id_authentication/tasks/open_id_authentication_tasks.rake +30 -0
  29. data/test/libs/open_id_authentication/test/mem_cache_store_test.rb +151 -0
  30. data/test/libs/open_id_authentication/test/normalize_test.rb +32 -0
  31. data/test/libs/open_id_authentication/test/open_id_authentication_test.rb +46 -0
  32. data/test/libs/open_id_authentication/test/status_test.rb +14 -0
  33. data/test/libs/open_id_authentication/test/test_helper.rb +17 -0
  34. data/test/libs/rails_trickery.rb +41 -0
  35. data/test/libs/user.rb +3 -0
  36. data/test/libs/user_session.rb +2 -0
  37. data/test/session_test.rb +32 -0
  38. data/test/test_helper.rb +78 -0
  39. metadata +115 -0
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,25 @@
1
+ == 1.0.4 released 2009-5-14
2
+
3
+ * Only authenticate with OpenID for models when a block is passed.
4
+ * Check for the existence of an openid_identifier field before including the model. Allowing this library to only be activated when present.
5
+ * Change required_field and optional_fields to openid_required_field and openid_optional_fields
6
+
7
+ == 1.0.3 released 2009-4-3
8
+
9
+ * Added find_by_openid_identifier config option for AuthlogicOpenid::Session.
10
+ * Set the openid_identifier by the one passed back by the provider in AuthlogicOpenid::ActsAsAuthentic.
11
+ * Added required_fields and optional_fields config options for AuthlogicOpenid::ActsAsAuthentic.
12
+ * Added map_openid_registration, attributes_to_save, and map_saved_attributes methods to customize how attributes are set for AuthlogicOpenid::ActsAsAuthentic.
13
+ * Make authenticating_with_openid? method a little more stringent to avoid trying to double authenticate. Ex: finding a session in the save block during a successful save.
14
+
15
+ == 1.0.2 released 2009-3-30
16
+
17
+ * Remove config block in initializer.
18
+
19
+ == 1.0.1 released 2009-3-30
20
+
21
+ * Change password validation option when included, and prepend the OpenID module.
22
+
23
+ == 1.0.0 released 2009-3-30
24
+
25
+ * Initial release
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Ben Johnson of Binary Logic (binarylogic.com)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Manifest.txt ADDED
@@ -0,0 +1,38 @@
1
+ CHANGELOG.rdoc
2
+ MIT-LICENSE
3
+ Manifest.txt
4
+ README.rdoc
5
+ Rakefile
6
+ init.rb
7
+ lib/authlogic_openid.rb
8
+ lib/authlogic_openid/acts_as_authentic.rb
9
+ lib/authlogic_openid/session.rb
10
+ lib/authlogic_openid/version.rb
11
+ test/acts_as_authentic_test.rb
12
+ test/fixtures/users.yml
13
+ test/libs/open_id_authentication/CHANGELOG
14
+ test/libs/open_id_authentication/README
15
+ test/libs/open_id_authentication/Rakefile
16
+ test/libs/open_id_authentication/generators/open_id_authentication_tables/open_id_authentication_tables_generator.rb
17
+ test/libs/open_id_authentication/generators/open_id_authentication_tables/templates/migration.rb
18
+ test/libs/open_id_authentication/generators/upgrade_open_id_authentication_tables/templates/migration.rb
19
+ test/libs/open_id_authentication/generators/upgrade_open_id_authentication_tables/upgrade_open_id_authentication_tables_generator.rb
20
+ test/libs/open_id_authentication/init.rb
21
+ test/libs/open_id_authentication/lib/open_id_authentication.rb
22
+ test/libs/open_id_authentication/lib/open_id_authentication/association.rb
23
+ test/libs/open_id_authentication/lib/open_id_authentication/db_store.rb
24
+ test/libs/open_id_authentication/lib/open_id_authentication/mem_cache_store.rb
25
+ test/libs/open_id_authentication/lib/open_id_authentication/nonce.rb
26
+ test/libs/open_id_authentication/lib/open_id_authentication/request.rb
27
+ test/libs/open_id_authentication/lib/open_id_authentication/timeout_fixes.rb
28
+ test/libs/open_id_authentication/tasks/open_id_authentication_tasks.rake
29
+ test/libs/open_id_authentication/test/mem_cache_store_test.rb
30
+ test/libs/open_id_authentication/test/normalize_test.rb
31
+ test/libs/open_id_authentication/test/open_id_authentication_test.rb
32
+ test/libs/open_id_authentication/test/status_test.rb
33
+ test/libs/open_id_authentication/test/test_helper.rb
34
+ test/libs/rails_trickery.rb
35
+ test/libs/user.rb
36
+ test/libs/user_session.rb
37
+ test/session_test.rb
38
+ test/test_helper.rb
data/README.rdoc ADDED
@@ -0,0 +1,93 @@
1
+ = Authlogic OpenID
2
+
3
+ Authlogic OpenID is an extension of the Authlogic library to add OpenID support. Authlogic v2.0 introduced an enhanced API that makes "plugging in" alternate authentication methods as easy as installing a gem.
4
+
5
+ == Helpful links
6
+
7
+ * <b>Documentation:</b> http://authlogic-oid.rubyforge.org
8
+ * <b>Authlogic:</b> http://github.com/binarylogic/authlogic
9
+ * <b>Live example:</b> http://authlogicexample.binarylogic.com
10
+
11
+ == Install and use
12
+
13
+ === 1. Make some simple changes to your database:
14
+
15
+ class AddUsersOpenidField < ActiveRecord::Migration
16
+ def self.up
17
+ add_column :users, :openid_identifier, :string
18
+ add_index :users, :openid_identifier
19
+
20
+ change_column :users, :login, :string, :default => nil, :null => true
21
+ change_column :users, :crypted_password, :string, :default => nil, :null => true
22
+ change_column :users, :password_salt, :string, :default => nil, :null => true
23
+ end
24
+
25
+ def self.down
26
+ remove_column :users, :openid_identifier
27
+
28
+ [:login, :crypted_password, :password_salt].each do |field|
29
+ User.all(:conditions => "#{field} is NULL").each { |user| user.update_attribute(field, "") if user.send(field).nil? }
30
+ change_column :users, field, :string, :default => "", :null => false
31
+ end
32
+ end
33
+ end
34
+
35
+ === 2. Install the openid_authentication plugin
36
+
37
+ $ script/plugin install git://github.com/rails/open_id_authentication.git
38
+ $ rake open_id_authentication:db:create
39
+
40
+ For more information on how to configure the plugin, checkout it's README: http://github.com/rails/open_id_authentication/tree/master
41
+
42
+ === 3. Install the Authlogic Openid gem
43
+
44
+ $ sudo gem install authlogic-oid
45
+
46
+ Now add the gem dependency in your config:
47
+
48
+ config.gem "authlogic-oid", :lib => "authlogic_openid"
49
+
50
+ Or for older version of rails, install it as a plugin:
51
+
52
+ $ script/plugin install git://github.com/binarylogic/authlogic_openid.git
53
+
54
+ === 4. Make sure you save your objects properly
55
+
56
+ You only need to save your objects this way if you want the user to authenticate with their OpenID provider.
57
+
58
+ That being said, you probably want to do this in your controllers. You should do this for BOTH your User objects and UserSession objects (assuming you are authenticating users). It should look something like this:
59
+
60
+ @user_session.save do |result|
61
+ if result
62
+ flash[:notice] = "Login successful!"
63
+ redirect_back_or_default account_url
64
+ else
65
+ render :action => :new
66
+ end
67
+ end
68
+
69
+ You should save your @user objects this way as well, because you also want the user to verify that they own the OpenID identifier that they supplied.
70
+
71
+ Notice we are saving with a block. Why? Because we need to redirect the user to their OpenID provider so that they can authenticate. When we do this, we don't want to execute that block of code, because if we do, we will get a DoubleRender error. This lets us skip that entire block and send the user along their way without any problems.
72
+
73
+ That's it! The rest is taken care of for you.
74
+
75
+ == Redirecting from the models?
76
+
77
+ If you are interested, I explain myself below. Regardless, if you don't feel comfortable with the organization of the logic,you can easily do this using the traditional method. As you saw in the setup instructions, this library leverages the open_id_authentication rails plugin. After the user has been authenticated just do this:
78
+
79
+ UserSession.create(@user)
80
+
81
+ It's that simple. For more information there is a great OpenID tutorial at: http://railscasts.com/episodes/68-openid-authentication
82
+
83
+ Now, here are my thoughts on the subject:
84
+
85
+ You are probably thinking: "Ben, you can't handle controller responsibilities in models". I agree with you on that comment, but my personal opinion is that these are not controller responsibilities. The fact that OpenID authentication requires a redirect should not effect the location of the logic / code. It's all part of the authentication process, which is the entire purpose of this library. This library is not one big module of code, its a collection of modules that all deal with OpenID authentication. These modules get included wherever it makes sense. That's the whole idea behind modules. To group common logic.
86
+
87
+ Let's take a step back and look at the traditional method of OpenID authentication in rails. What if you wanted to authenticate with OpenID in multiple controllers in your application (Ex: registration and loggin in)? You would probably pull out the common code into a module and include it in the respective controllers. Even better, you might create a class that elegantly handles this process and then place it in your lib directory. Then, if you really wanted to be slick, you might take it another step further and have your models trigger this class during certain actions. Then what do we have? This exact library, that's exactly what this is.
88
+
89
+ The last thing I will leave you with, to get you thinking, is... where do sweepers lie in the MVC pattern? Without this, things like caching would be extremely difficult. There is a big difference between misplacing code / logic, and organizing logic into a separate module and hooking it in using the API provided by your models. Especially when the logic needs to be triggered by actions invoked on models.
90
+
91
+ Regardless, if I still haven't convinced you, I hope this library is of some benefit to you. At the very least an example of how to extend Authlogic.
92
+
93
+ Copyright (c) 2009 Ben Johnson of [Binary Logic](http://www.binarylogic.com), released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ ENV['RDOCOPT'] = "-S -f html -T hanna"
2
+
3
+ require "rubygems"
4
+ require "hoe"
5
+ require File.dirname(__FILE__) << "/lib/authlogic_openid/version"
6
+
7
+ Hoe.new("Heyzap Authlogic OpenID", AuthlogicOpenid::Version::STRING) do |p|
8
+ p.name = "heyzap-authlogic-oid"
9
+ p.rubyforge_name = "authlogic-oid"
10
+ p.author = "Ben Johnson of Binary Logic"
11
+ p.email = 'bjohnson@binarylogic.com'
12
+ p.summary = "Extension of the Authlogic library to add OpenID support."
13
+ p.description = "Extension of the Authlogic library to add OpenID support."
14
+ p.url = "http://github.com/binarylogic/authlogic_openid"
15
+ p.history_file = "CHANGELOG.rdoc"
16
+ p.readme_file = "README.rdoc"
17
+ p.extra_rdoc_files = ["CHANGELOG.rdoc", "README.rdoc"]
18
+ p.remote_rdoc_dir = ''
19
+ p.test_globs = ["test/*/test_*.rb", "test/*_test.rb", "test/*/*_test.rb"]
20
+ p.extra_deps = %w(authlogic)
21
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/rails/init.rb"
@@ -0,0 +1,6 @@
1
+ require "authlogic_openid/version"
2
+ require "authlogic_openid/acts_as_authentic"
3
+ require "authlogic_openid/session"
4
+
5
+ ActiveRecord::Base.send(:include, AuthlogicOpenid::ActsAsAuthentic)
6
+ Authlogic::Session::Base.send(:include, AuthlogicOpenid::Session)
@@ -0,0 +1,173 @@
1
+ # This module is responsible for adding OpenID functionality to Authlogic. Checkout the README for more info and please
2
+ # see the sub modules for detailed documentation.
3
+ module AuthlogicOpenid
4
+ # This module is responsible for adding in the OpenID functionality to your models. It hooks itself into the
5
+ # acts_as_authentic method provided by Authlogic.
6
+ module ActsAsAuthentic
7
+ # Adds in the neccesary modules for acts_as_authentic to include and also disabled password validation if
8
+ # OpenID is being used.
9
+ def self.included(klass)
10
+ klass.class_eval do
11
+ extend Config
12
+ add_acts_as_authentic_module(Methods, :prepend)
13
+ end
14
+ end
15
+
16
+ module Config
17
+ # Some OpenID providers support a lightweight profile exchange protocol, for those that do, you can require
18
+ # certain fields. This is convenient for new registrations, as it will basically fill out the fields in the
19
+ # form for them, so they don't have to re-type information already stored with their OpenID account.
20
+ #
21
+ # For more info and what fields you can use see: http://openid.net/specs/openid-simple-registration-extension-1_0.html
22
+ #
23
+ # * <tt>Default:</tt> []
24
+ # * <tt>Accepts:</tt> Array of symbols
25
+ def openid_required_fields(value = nil)
26
+ rw_config(:openid_required_fields, value, [])
27
+ end
28
+ alias_method :openid_required_fields=, :openid_required_fields
29
+
30
+ # Same as required_fields, but optional instead.
31
+ #
32
+ # * <tt>Default:</tt> []
33
+ # * <tt>Accepts:</tt> Array of symbols
34
+ def openid_optional_fields(value = nil)
35
+ rw_config(:openid_optional_fields, value, [])
36
+ end
37
+ alias_method :openid_optional_fields=, :openid_optional_fields
38
+ end
39
+
40
+ module Methods
41
+ # Set up some simple validations
42
+ def self.included(klass)
43
+ return if !klass.column_names.include?("openid_identifier")
44
+
45
+ klass.class_eval do
46
+ validates_uniqueness_of :openid_identifier, :scope => validations_scope, :if => :using_openid?
47
+ validate :validate_openid
48
+ validates_length_of_password_field_options validates_length_of_password_field_options.merge(:if => :validate_password_with_openid?)
49
+ validates_confirmation_of_password_field_options validates_confirmation_of_password_field_options.merge(:if => :validate_password_with_openid?)
50
+ validates_length_of_password_confirmation_field_options validates_length_of_password_confirmation_field_options.merge(:if => :validate_password_with_openid?)
51
+ end
52
+ end
53
+
54
+ # Set the openid_identifier field and also resets the persistence_token if this value changes.
55
+ def openid_identifier=(value)
56
+ write_attribute(:openid_identifier, value.blank? ? nil : OpenIdAuthentication.normalize_identifier(value))
57
+ reset_persistence_token if openid_identifier_changed?
58
+ rescue OpenIdAuthentication::InvalidOpenId => e
59
+ @openid_error = e.message
60
+ end
61
+
62
+ # This is where all of the magic happens. This is where we hook in and add all of the OpenID sweetness.
63
+ #
64
+ # I had to take this approach because when authenticating with OpenID nonces and what not are stored in database
65
+ # tables. That being said, the whole save process for ActiveRecord is wrapped in a transaction. Trying to authenticate
66
+ # with OpenID in a transaction is not good because that transaction be get rolled back, thus reversing all of the OpenID
67
+ # inserts and making OpenID authentication fail every time. So We need to step outside of the transaction and do our OpenID
68
+ # madness.
69
+ #
70
+ # Another advantage of taking this approach is that we can set fields from their OpenID profile before we save the record,
71
+ # if their OpenID provider supports it.
72
+ def save(perform_validation = true, &block)
73
+ return false if perform_validation && block_given? && authenticate_with_openid? && !authenticate_with_openid
74
+
75
+ result = super
76
+ yield(result) if block_given?
77
+ result
78
+ end
79
+
80
+ private
81
+ def authenticate_with_openid
82
+ @openid_error = nil
83
+
84
+ if !openid_complete?
85
+ session_class.controller.session[:openid_attributes] = attributes_to_save
86
+ else
87
+ map_saved_attributes(session_class.controller.session[:openid_attributes])
88
+ session_class.controller.session[:openid_attributes] = nil
89
+ end
90
+
91
+ options = {}
92
+ options[:required] = self.class.openid_required_fields
93
+ options[:optional] = self.class.openid_optional_fields
94
+ options[:return_to] = session_class.controller.url_for(:for_model => "1")
95
+
96
+ session_class.controller.send(:authenticate_with_open_id, openid_identifier, options) do |result, openid_identifier, registration|
97
+ if result.unsuccessful?
98
+ @openid_error = result.message
99
+ else
100
+ self.openid_identifier = openid_identifier
101
+ map_openid_registration(registration)
102
+ end
103
+
104
+ return true
105
+ end
106
+ return false
107
+ end
108
+
109
+ # Override this method to map the OpenID registration fields with fields in your model. See the required_fields and
110
+ # optional_fields configuration options to enable this feature.
111
+ #
112
+ # Basically you will get a hash of values passed as a single argument. Then just map them as you see fit. Check out
113
+ # the source of this method for an example.
114
+ def map_openid_registration(registration) # :doc:
115
+ registration.symbolize_keys!
116
+ [self.class.openid_required_fields+self.class.openid_optional_fields].flatten.each do |field|
117
+ setter="#{field.to_s}=".to_sym
118
+ if respond_to?(setter)
119
+ send setter,registration[field]
120
+ end
121
+ end
122
+ end
123
+
124
+ # This method works in conjunction with map_saved_attributes.
125
+ #
126
+ # Let's say a user fills out a registration form, provides an OpenID and submits the form. They are then redirected to their
127
+ # OpenID provider. All is good and they are redirected back. All of those fields they spent time filling out are forgetten
128
+ # and they have to retype them all. To avoid this, AuthlogicOpenid saves all of these attributes in the session and then
129
+ # attempts to restore them. See the source for what attributes it saves. If you need to block more attributes, or save
130
+ # more just override this method and do whatever you want.
131
+ def attributes_to_save # :doc:
132
+ attrs_to_save = attributes.clone.delete_if do |k, v|
133
+ [:id, :password, crypted_password_field, password_salt_field, :persistence_token, :perishable_token, :single_access_token, :login_count,
134
+ :failed_login_count, :last_request_at, :current_login_at, :last_login_at, :current_login_ip, :last_login_ip, :created_at,
135
+ :updated_at, :lock_version].include?(k.to_sym)
136
+ end
137
+ attrs_to_save.merge!(:password => password, :password_confirmation => password_confirmation)
138
+ end
139
+
140
+ # This method works in conjunction with attributes_to_save. See that method for a description of the why these methods exist.
141
+ #
142
+ # If the default behavior of this method is not sufficient for you because you have attr_protected or attr_accessible then
143
+ # override this method and set them individually. Maybe something like this would be good:
144
+ #
145
+ # attrs.each do |key, value|
146
+ # send("#{key}=", value)
147
+ # end
148
+ def map_saved_attributes(attrs) # :doc:
149
+ self.attributes = attrs
150
+ end
151
+
152
+ def validate_openid
153
+ errors.add(:openid_identifier, "had the following error: #{@openid_error}") if @openid_error
154
+ end
155
+
156
+ def using_openid?
157
+ respond_to?(:openid_identifier) && !openid_identifier.blank?
158
+ end
159
+
160
+ def openid_complete?
161
+ session_class.controller.params[:open_id_complete]
162
+ end
163
+
164
+ def authenticate_with_openid?
165
+ session_class.activated? && ((using_openid? && openid_identifier_changed?) || openid_complete?)
166
+ end
167
+
168
+ def validate_password_with_openid?
169
+ !using_openid? && require_password?
170
+ end
171
+ end
172
+ end
173
+ end
@@ -0,0 +1,131 @@
1
+ module AuthlogicOpenid
2
+ # This module is responsible for adding all of the OpenID goodness to the Authlogic::Session::Base class.
3
+ module Session
4
+ # Add a simple openid_identifier attribute and some validations for the field.
5
+ def self.included(klass)
6
+ klass.class_eval do
7
+ extend Config
8
+ include Methods
9
+ end
10
+ end
11
+
12
+ module Config
13
+ # What method should we call to find a record by the openid_identifier?
14
+ # This is useful if you want to store multiple openid_identifiers for a single record.
15
+ # You could do something like:
16
+ #
17
+ # class User < ActiveRecord::Base
18
+ # def self.find_by_openid_identifier(identifier)
19
+ # user.first(:conditions => {:openid_identifiers => {:identifier => identifier}})
20
+ # end
21
+ # end
22
+ #
23
+ # Obviously the above depends on what you are calling your assocition, etc. But you get the point.
24
+ #
25
+ # * <tt>Default:</tt> :find_by_openid_identifier
26
+ # * <tt>Accepts:</tt> Symbol
27
+ def find_by_openid_identifier_method(value = nil)
28
+ rw_config(:find_by_openid_identifier_method, value, :find_by_openid_identifier)
29
+ end
30
+ alias_method :find_by_openid_identifier_method=, :find_by_openid_identifier_method
31
+
32
+ # Add this in your Session object to Auto Register a new user using openid via sreg
33
+ def auto_register(value=true)
34
+ auto_register_value(value)
35
+ end
36
+
37
+ def auto_register_value(value=nil)
38
+ rw_config(:auto_register,value,false)
39
+ end
40
+
41
+ alias_method :auto_register=,:auto_register
42
+ end
43
+
44
+ module Methods
45
+ def self.included(klass)
46
+ klass.class_eval do
47
+ attr_reader :openid_identifier
48
+ validate :validate_openid_error
49
+ validate :validate_by_openid, :if => :authenticating_with_openid?
50
+ end
51
+ end
52
+
53
+ # Hooks into credentials so that you can pass an :openid_identifier key.
54
+ def credentials=(value)
55
+ super
56
+ values = value.is_a?(Array) ? value : [value]
57
+ hash = values.first.is_a?(Hash) ? values.first.with_indifferent_access : nil
58
+ self.openid_identifier = hash[:openid_identifier] if !hash.nil? && hash.key?(:openid_identifier)
59
+ end
60
+
61
+ def openid_identifier=(value)
62
+ @openid_identifier = value.blank? ? nil : OpenIdAuthentication.normalize_identifier(value)
63
+ @openid_error = nil
64
+ rescue OpenIdAuthentication::InvalidOpenId => e
65
+ @openid_identifier = nil
66
+ @openid_error = e.message
67
+ end
68
+
69
+ # Cleaers out the block if we are authenticating with OpenID, so that we can redirect without a DoubleRender
70
+ # error.
71
+ def save(&block)
72
+ block = nil if !openid_identifier.blank?
73
+ super(&block)
74
+ end
75
+
76
+ private
77
+ def authenticating_with_openid?
78
+ attempted_record.nil? && errors.empty? && (!openid_identifier.blank? || (controller.params[:open_id_complete] && controller.params[:for_session]))
79
+ end
80
+
81
+ def find_by_openid_identifier_method
82
+ self.class.find_by_openid_identifier_method
83
+ end
84
+
85
+ def find_by_openid_identifier_method
86
+ self.class.find_by_openid_identifier_method
87
+ end
88
+
89
+ def auto_register?
90
+ self.class.auto_register_value
91
+ end
92
+
93
+ def validate_by_openid
94
+ self.remember_me = controller.params[:remember_me] == "true" if controller.params.key?(:remember_me)
95
+
96
+ options = {}
97
+ options[:required] = klass.openid_required_fields
98
+ options[:optional] = klass.openid_optional_fields
99
+ options[:return_to] = controller.url_for(:for_session => "1", :remember_me => remember_me?)
100
+
101
+ controller.send(:authenticate_with_open_id, openid_identifier, options) do |result, openid_identifier, registration|
102
+ if result.unsuccessful?
103
+ errors.add_to_base(result.message)
104
+ return
105
+ end
106
+
107
+ self.attempted_record = klass.send(find_by_openid_identifier_method, openid_identifier)
108
+
109
+ if !attempted_record
110
+ if auto_register?
111
+ self.attempted_record = klass.new :openid_identifier=>openid_identifier
112
+ self.attempted_record.send(:map_openid_registration, registration)
113
+
114
+ if ! attempted_record.save
115
+ errors.add(:openid_identifier, "error auto-registering new openid account")
116
+ end
117
+ return
118
+ else
119
+ errors.add(:openid_identifier, "did not match any users in our database, have you set up your account to use OpenID?")
120
+ return
121
+ end
122
+ end
123
+ end
124
+ end
125
+
126
+ def validate_openid_error
127
+ errors.add(:openid_identifier, @openid_error) if @openid_error
128
+ end
129
+ end
130
+ end
131
+ end