Empact-authlogic_rpx 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. data/.gitignore +1 -0
  2. data/CHANGELOG.rdoc +39 -0
  3. data/Empact-authlogic_rpx.gemspec +100 -0
  4. data/MIT-LICENSE +20 -0
  5. data/README.rdoc +747 -0
  6. data/Rakefile +47 -0
  7. data/VERSION +1 -0
  8. data/generators/add_authlogic_rpx_migration/USAGE +18 -0
  9. data/generators/add_authlogic_rpx_migration/add_authlogic_rpx_migration_generator.rb +44 -0
  10. data/generators/add_authlogic_rpx_migration/templates/migration_internal_mapping.rb +34 -0
  11. data/generators/add_authlogic_rpx_migration/templates/migration_no_mapping.rb +29 -0
  12. data/init.rb +1 -0
  13. data/lib/authlogic_rpx.rb +13 -0
  14. data/lib/authlogic_rpx/acts_as_authentic.rb +274 -0
  15. data/lib/authlogic_rpx/helper.rb +44 -0
  16. data/lib/authlogic_rpx/rpx_identifier.rb +5 -0
  17. data/lib/authlogic_rpx/session.rb +241 -0
  18. data/lib/authlogic_rpx/session/validation.rb +30 -0
  19. data/lib/authlogic_rpx/version.rb +51 -0
  20. data/rails/init.rb +1 -0
  21. data/test/fixtures/rpxresponses.yml +20 -0
  22. data/test/fixtures/users.yml +20 -0
  23. data/test/integration/basic_authentication_and_registration_test.rb +73 -0
  24. data/test/integration/internal_mapping/basic_authentication_and_registration_test.rb +3 -0
  25. data/test/integration/internal_mapping/settings_test.rb +10 -0
  26. data/test/integration/no_mapping/basic_authentication_and_registration_test.rb +3 -0
  27. data/test/integration/no_mapping/settings_test.rb +10 -0
  28. data/test/libs/ext_test_unit.rb +30 -0
  29. data/test/libs/mock_rpx_now.rb +34 -0
  30. data/test/libs/rails_trickery.rb +41 -0
  31. data/test/libs/rpxresponse.rb +3 -0
  32. data/test/libs/user.rb +3 -0
  33. data/test/libs/user_session.rb +3 -0
  34. data/test/test_helper.rb +85 -0
  35. data/test/test_internal_mapping_helper.rb +93 -0
  36. data/test/unit/acts_as_authentic_settings_test.rb +42 -0
  37. data/test/unit/session_settings_test.rb +38 -0
  38. data/test/unit/session_validation_test.rb +16 -0
  39. data/test/unit/verify_rpx_mock_test.rb +29 -0
  40. metadata +143 -0
data/Rakefile ADDED
@@ -0,0 +1,47 @@
1
+ ENV['RDOCOPT'] = "-S -f html -T hanna"
2
+
3
+ require 'rubygems'
4
+ require File.dirname(__FILE__) << "/lib/authlogic_rpx/version"
5
+
6
+ begin
7
+ require 'jeweler'
8
+ Jeweler::Tasks.new do |p|
9
+ p.name = "Empact-authlogic_rpx"
10
+ p.summary = "Authlogic plug-in for RPX support"
11
+ p.description = "Authlogic extension/plugin that provides RPX (rpxnow.com) authentication support"
12
+ p.email = "gallagher.paul@gmail.com"
13
+ p.homepage = "http://github.com/tardate/authlogic_rpx"
14
+ p.authors = ["Paul Gallagher / tardate"]
15
+ p
16
+ p.add_dependency("authlogic", ">=2.1.3")
17
+ p.add_dependency("rpx_now", ">=0.6.12")
18
+ end
19
+ rescue LoadError
20
+ puts "Jeweler not available. Install it with: gem install jeweler"
21
+ end
22
+
23
+ require 'rake/testtask'
24
+ Rake::TestTask.new(:unit) do |t|
25
+ t.libs << "test/libs"
26
+ t.pattern = 'test/unit/*test.rb'
27
+ t.verbose = true
28
+ end
29
+
30
+ Rake::TestTask.new(:no_mapping) do |t|
31
+ t.libs << "test/libs"
32
+ t.test_files = FileList.new('test/unit/*test.rb', 'test/integration/no_mapping/*test.rb')
33
+ t.verbose = true
34
+ end
35
+
36
+ Rake::TestTask.new(:internal_mapping) do |t|
37
+ t.libs << "test/libs"
38
+ t.test_files = FileList.new('test/integration/internal_mapping/*test.rb')
39
+ t.verbose = true
40
+ end
41
+
42
+ task :test do
43
+ Rake::Task[:no_mapping].invoke
44
+ Rake::Task[:internal_mapping].invoke
45
+ end
46
+
47
+ task :default => :test
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.1.2
@@ -0,0 +1,18 @@
1
+ Description:
2
+ ruby script/generate add_authlogic_rpx_migration [mapping:mapping_mode] [user_model:model_name]
3
+
4
+ Creates an add_authlogic_rpx_migration file in db/migrate.
5
+
6
+ The mapping_mode parameter indicates which style of Authlogic_RPX-supported identity
7
+ mapping should be used. Allowed values for mapping_mode are:
8
+ none
9
+ internal
10
+ Default mapping_mode is 'internal'
11
+
12
+ The user_model parameter specifies the name of the user/member model in your application.
13
+ Default model_name is 'User'
14
+
15
+ e.g. to generate the RPX migration where the user model is called 'Member' and you do not
16
+ want to support identity mapping:
17
+
18
+ ruby script/generate add_authlogic_rpx_migration mapping:none user_model:member
@@ -0,0 +1,44 @@
1
+ class AddAuthlogicRpxMigrationGenerator < Rails::Generator::Base
2
+ def manifest
3
+
4
+ record do |m|
5
+
6
+ m.migration_template template_name, 'db/migrate', :assigns => {
7
+ :user_model_base => user_model_base,
8
+ :user_model => user_model,
9
+ :user_model_collection => user_model_collection
10
+ }
11
+ end
12
+ end
13
+
14
+ def file_name
15
+ "add_authlogic_rpx_migration"
16
+ end
17
+
18
+ protected
19
+ # Override with your own usage banner.
20
+ def banner
21
+ "Usage: #{$0} #{spec.name} [options] [mapping:mapping_mode] [user_model:model_name]"
22
+ end
23
+
24
+ attr_writer :params
25
+ def params
26
+ @params ||= {"mapping" => "internal", "user_model" => "User"}.merge( Hash[*(@args.collect { |arg| arg.split(":") }.flatten)] )
27
+ end
28
+
29
+ def user_model_base
30
+ params['user_model'].singularize.downcase
31
+ end
32
+ def user_model
33
+ params['user_model'].singularize.capitalize
34
+ end
35
+ def user_model_collection
36
+ params['user_model'].pluralize.downcase
37
+ end
38
+ def mapping
39
+ params['mapping']
40
+ end
41
+ def template_name
42
+ mapping == 'none' ? 'migration_no_mapping.rb' : 'migration_internal_mapping.rb'
43
+ end
44
+ end
@@ -0,0 +1,34 @@
1
+ class AddAuthlogicRpxMigration < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :rpx_identifiers do |t|
4
+ t.string :identifier, :null => false
5
+ t.string :provider_name
6
+ t.integer :<%= user_model_base %>_id, :null => false
7
+ t.timestamps
8
+ end
9
+ add_index :rpx_identifiers, :identifier, :unique => true, :null => false
10
+ add_index :rpx_identifiers, :<%= user_model_base %>_id, :unique => false, :null => false
11
+
12
+ # == Customisation may be required here ==
13
+ # You may need to remove database constraints on other fields if they will be unused in the RPX case
14
+ # (e.g. crypted_password and password_salt to make password authentication optional).
15
+ # If you are using auto-registration, you must also remove any database constraints for fields that will be automatically mapped
16
+ # e.g.:
17
+ #change_column :<%= user_model_collection %>, :crypted_password, :string, :default => nil, :null => true
18
+ #change_column :<%= user_model_collection %>, :password_salt, :string, :default => nil, :null => true
19
+
20
+ end
21
+
22
+ def self.down
23
+ drop_table :rpx_identifiers
24
+
25
+ # == Customisation may be required here ==
26
+ # Restore user model database constraints as appropriate
27
+ # e.g.:
28
+ #[:crypted_password, :password_salt].each do |field|
29
+ # <%= user_model %>.all(:conditions => "#{field} is NULL").each { |user| user.update_attribute(field, "") if user.send(field).nil? }
30
+ # change_column :<%= user_model_collection %>, field, :string, :default => "", :null => false
31
+ #end
32
+
33
+ end
34
+ end
@@ -0,0 +1,29 @@
1
+ class AddAuthlogicRpxMigration < ActiveRecord::Migration
2
+
3
+ def self.up
4
+ add_column :<%= user_model_collection %>, :rpx_identifier, :string
5
+ add_index :<%= user_model_collection %>, :rpx_identifier
6
+
7
+ # == Customisation may be required here ==
8
+ # You may need to remove database constraints on other fields if they will be unused in the RPX case
9
+ # (e.g. crypted_password and password_salt to make password authentication optional).
10
+ # If you are using auto-registration, you must also remove any database constraints for fields that will be automatically mapped
11
+ # e.g.:
12
+ #change_column :<%= user_model_collection %>, :crypted_password, :string, :default => nil, :null => true
13
+ #change_column :<%= user_model_collection %>, :password_salt, :string, :default => nil, :null => true
14
+
15
+ end
16
+
17
+ def self.down
18
+ remove_column :<%= user_model_collection %>, :rpx_identifier
19
+
20
+ # == Customisation may be required here ==
21
+ # Restore user model database constraints as appropriate
22
+ # e.g.:
23
+ #[:crypted_password, :password_salt].each do |field|
24
+ # <%= user_model %>.all(:conditions => "#{field} is NULL").each { |user| user.update_attribute(field, "") if user.send(field).nil? }
25
+ # change_column :<%= user_model_collection %>, field, :string, :default => "", :null => false
26
+ #end
27
+
28
+ end
29
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/rails/init.rb"
@@ -0,0 +1,13 @@
1
+ require "authlogic_rpx/version"
2
+ require "authlogic_rpx/acts_as_authentic"
3
+ require "authlogic_rpx/session"
4
+ require "authlogic_rpx/session/validation"
5
+ require "authlogic_rpx/helper"
6
+ require "authlogic_rpx/rpx_identifier"
7
+
8
+ ActiveRecord::Base.send(:include, AuthlogicRpx::ActsAsAuthentic)
9
+ Authlogic::Session::Base.class_eval do
10
+ include AuthlogicRpx::Session::Validation
11
+ include AuthlogicRpx::Session
12
+ end
13
+ ActionController::Base.helper AuthlogicRpx::Helper
@@ -0,0 +1,274 @@
1
+ # This module is responsible for adding RPX functionality to Authlogic. Checkout the README for more info and please
2
+ # see the sub modules for detailed documentation.
3
+ module AuthlogicRpx
4
+ # This module is responsible for adding in the RPX 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
+ # RPX 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
+ class GeneralError < StandardError
17
+ end
18
+ class ConfigurationError < StandardError
19
+ end
20
+
21
+ module Config
22
+
23
+ # account_merge_enabled is used to enable merging of accounts.
24
+ #
25
+ # * <tt>Default:</tt> false
26
+ # * <tt>Accepts:</tt> boolean
27
+ def account_merge_enabled(value=false)
28
+ account_merge_enabled_value(value)
29
+ end
30
+ def account_merge_enabled_value(value=nil)
31
+ rw_config(:account_merge_enabled,value,false)
32
+ end
33
+ alias_method :account_merge_enabled=,:account_merge_enabled
34
+
35
+
36
+ # account_mapping_mode is used to explicitly set/override the mapping behaviour.
37
+ #
38
+ # * <tt>Default:</tt> :auto
39
+ # * <tt>Accepts:</tt> :auto, :none, :internal, :rpxnow
40
+ def account_mapping_mode(value=:auto)
41
+ account_mapping_mode_value(value)
42
+ end
43
+ def account_mapping_mode_value(value=nil)
44
+ raise AuthlogicRpx::ActsAsAuthentic::ConfigurationError.new unless value.nil? || [:auto,:none,:internal].include?( value )
45
+ rw_config(:account_mapping_mode,value,:auto)
46
+ end
47
+ alias_method :account_mapping_mode=,:account_mapping_mode
48
+
49
+ # returns the actual account mapping mode in use - resolves :auto to actual mechanism
50
+ #
51
+ attr_writer :account_mapping_mode_used
52
+ def account_mapping_mode_used
53
+ @account_mapping_mode_used ||= (
54
+ account_mapping_mode_value == :auto ?
55
+ ( RPXIdentifier.table_exists? ?
56
+ :internal :
57
+ ( self.column_names.include?("rpx_identifier") ? :none : AuthlogicRpx::ActsAsAuthentic::ConfigurationError.new )
58
+ ) :
59
+ account_mapping_mode_value
60
+ )
61
+ end
62
+
63
+
64
+ # determines if no account mapping is supported (the only behaviour in authlogic_rpx v1.0.4)
65
+ def using_no_mapping?
66
+ account_mapping_mode_used == :none
67
+ end
68
+ # determines if internal account mapping is enabled (behaviour added in authlogic_rpx v1.1.0)
69
+ def using_internal_mapping?
70
+ account_mapping_mode_used == :internal
71
+ end
72
+ # determines if rpxnow account mapping is enabled (currently not implemented)
73
+ def using_rpx_mapping?
74
+ account_mapping_mode_used == :rpxnow
75
+ end
76
+
77
+ end
78
+
79
+ module Methods
80
+
81
+ # Set up some simple validations
82
+ def self.included(klass)
83
+ klass.class_eval do
84
+
85
+ case
86
+ when using_no_mapping?
87
+ alias_method :using_rpx?, :using_rpx__nomap?
88
+ alias_method :add_rpx_identifier, :add_rpx_identifier__nomap
89
+ alias_method :identified_by?, :identified_by__nomap?
90
+ alias_method :merge_user_id, :merge_user_id__nomap
91
+
92
+ # Uses default find_by_rpx_identifier class method
93
+
94
+ # Add an rpx_identifier collection method
95
+ def rpx_identifiers
96
+ [{ :identifier => rpx_identifier, :provider_name => "Unknown" }]
97
+ end
98
+
99
+ when using_internal_mapping?
100
+ alias_method :using_rpx?, :using_rpx__internal?
101
+ alias_method :add_rpx_identifier, :add_rpx_identifier__internal
102
+ alias_method :identified_by?, :identified_by__internal?
103
+ alias_method :merge_user_id, :merge_user_id__internal
104
+ has_many :rpx_identifiers, :class_name => 'RPXIdentifier', :validate => false, :dependent => :destroy
105
+
106
+ # Add custom find_by_rpx_identifier class method
107
+ def self.find_by_rpx_identifier(id)
108
+ identifier = RPXIdentifier.find_by_identifier(id)
109
+ if identifier.nil?
110
+ if self.column_names.include? 'rpx_identifier'
111
+ # check for authentication using <=1.0.4, migrate identifier to rpx_identifiers table
112
+ user = self.find( :first, :conditions => [ "rpx_identifier = ?", id ] )
113
+ unless user.nil?
114
+ user.add_rpx_identifier( id, 'Unknown' )
115
+ end
116
+ return user
117
+ else
118
+ return nil
119
+ end
120
+ else
121
+ identifier.user
122
+ end
123
+ end
124
+
125
+ else
126
+ raise AuthlogicRpx::ActsAsAuthentic::ConfigurationError.new( "invalid or unsupported account_mapping_mode" )
127
+ end
128
+
129
+ validates_length_of_password_field_options validates_length_of_password_field_options.merge(:if => :validate_password_with_rpx?)
130
+ validates_confirmation_of_password_field_options validates_confirmation_of_password_field_options.merge(:if => :validate_password_with_rpx?)
131
+ validates_length_of_password_confirmation_field_options validates_length_of_password_confirmation_field_options.merge(:if => :validate_password_with_rpx?)
132
+
133
+ before_validation :adding_rpx_identifier
134
+ end
135
+
136
+ RPXIdentifier.class_eval do
137
+ belongs_to klass.name.downcase.to_sym
138
+ end
139
+ end
140
+
141
+ # test if account it using RPX authentication
142
+ # aliased to using_rpx based on authlogic_rpx configuration mode
143
+ def using_rpx__nomap?
144
+ !rpx_identifier.blank?
145
+ end
146
+ def using_rpx__internal?
147
+ !rpx_identifiers.empty?
148
+ end
149
+
150
+ # test if account it using normal password authentication
151
+ def using_password?
152
+ !send(crypted_password_field).blank?
153
+ end
154
+
155
+ # adds RPX identification to the instance.
156
+ # Abstracts how the RPX identifier is added to allow for multiplicity of underlying implementations
157
+ # aliased to add_rpx_identifier based on authlogic_rpx configuration mode
158
+ def add_rpx_identifier__nomap( rpx_id, rpx_provider_name )
159
+ self.rpx_identifier = rpx_id
160
+ #TODO: make rpx_provider_name a std param?
161
+ end
162
+ def add_rpx_identifier__internal( rpx_id, rpx_provider_name )
163
+ self.rpx_identifiers.build(:identifier => rpx_id, :provider_name => rpx_provider_name )
164
+ end
165
+
166
+ # Checks if given identifier is an identity for this account
167
+ # aliased to identified_by based on authlogic_rpx configuration mode
168
+ def identified_by__nomap?( id )
169
+ self.rpx_identifier == id
170
+ end
171
+ def identified_by__internal?( id )
172
+ self.rpx_identifiers.find_by_identifier( id )
173
+ end
174
+
175
+ private
176
+
177
+ # tests if password authentication should be checked: if rpx is enabled (but not used by this user)
178
+ def validate_password_with_rpx?
179
+ !using_rpx? && require_password?
180
+ end
181
+
182
+ # determines if account merging is enabled; delegates to class method
183
+ def account_merge_enabled?
184
+ self.class.account_merge_enabled_value
185
+ end
186
+
187
+ # hook for adding RPX identifier to an existing account. This is invoked prior to model validation.
188
+ # RPX information is plucked from the controller session object (where it was placed by the session model as a result
189
+ # of the RPX callback)
190
+ # The minimal action taken is to add an RPXIdentifier object to the user.
191
+ #
192
+ # This procedure chains to the map_added_rpx_data, which may be over-ridden in your project to perform
193
+ # additional mapping of RPX information to the user model as may be desired.
194
+ #
195
+ def adding_rpx_identifier
196
+ return true unless session_class && session_class.controller
197
+
198
+ added_rpx_data = session_class.controller.session['added_rpx_data']
199
+ unless added_rpx_data.blank?
200
+ session_class.controller.session['added_rpx_data'] = nil
201
+ rpx_id = added_rpx_data['profile']['identifier']
202
+ rpx_provider_name = added_rpx_data['profile']['providerName']
203
+
204
+ unless self.identified_by?( rpx_id )
205
+ # identifier not already set for this user..
206
+
207
+ another_user = self.class.find_by_rpx_identifier( rpx_id )
208
+ if another_user
209
+ return false unless account_merge_enabled?
210
+ # another user already has this id registered..
211
+
212
+ # merge all IDs from another_user to self, with application callbacks before/after
213
+ before_merge_rpx_data( another_user, self )
214
+ merge_user_id another_user
215
+ after_merge_rpx_data( another_user, self )
216
+
217
+ else
218
+ self.add_rpx_identifier( rpx_id, rpx_provider_name )
219
+ end
220
+ end
221
+
222
+ map_added_rpx_data( added_rpx_data )
223
+ end
224
+
225
+ return true
226
+ end
227
+
228
+ # merge_user_id is an internal method used to merge the actual RPX identifiers
229
+ # aliased to merge_user_id based on authlogic_rpx configuration mode
230
+ def merge_user_id__nomap( from_user )
231
+ self.rpx_identifier = from_user.rpx_identifier
232
+ from_user.rpx_identifier = nil
233
+ from_user.save
234
+ from_user.reload
235
+ end
236
+ def merge_user_id__internal( from_user )
237
+ self.rpx_identifiers << from_user.rpx_identifiers
238
+ from_user.reload
239
+ end
240
+
241
+
242
+ # map_added_rpx_data maps additional fields from the RPX response into the user object during the "add RPX to existing account" process.
243
+ # Override this in your user model to perform field mapping as may be desired
244
+ # See https://rpxnow.com/docs#profile_data for the definition of available attributes
245
+ #
246
+ # "self" at this point is the user model. Map details as appropriate from the rpx_data structure provided.
247
+ #
248
+ def map_added_rpx_data( rpx_data )
249
+
250
+ end
251
+
252
+ # before_merge_rpx_data provides a hook for application developers to perform data migration prior to the merging of user accounts.
253
+ # This method is called just before authlogic_rpx merges the user registration for 'from_user' into 'to_user'
254
+ # Authlogic_RPX is responsible for merging registration data.
255
+ #
256
+ # By default, it does not merge any other details (e.g. application data ownership)
257
+ #
258
+ def before_merge_rpx_data( from_user, to_user )
259
+
260
+ end
261
+
262
+ # after_merge_rpx_data provides a hook for application developers to perform account clean-up after authlogic_rpx has
263
+ # migrated registration details.
264
+ #
265
+ # By default, does nothing. It could, for example, be used to delete or disable the 'from_user' account
266
+ #
267
+ def after_merge_rpx_data( from_user, to_user )
268
+
269
+ end
270
+
271
+
272
+ end
273
+ end
274
+ end