slayer-authlogic_rpx 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. data/.gitignore +1 -0
  2. data/CHANGELOG.rdoc +46 -0
  3. data/MIT-LICENSE +20 -0
  4. data/Manifest +37 -0
  5. data/README.rdoc +755 -0
  6. data/Rakefile +55 -0
  7. data/generators/add_authlogic_rpx_migration/USAGE +18 -0
  8. data/generators/add_authlogic_rpx_migration/add_authlogic_rpx_migration_generator.rb +44 -0
  9. data/generators/add_authlogic_rpx_migration/templates/migration_internal_mapping.rb +34 -0
  10. data/generators/add_authlogic_rpx_migration/templates/migration_no_mapping.rb +29 -0
  11. data/init.rb +1 -0
  12. data/lib/authlogic_rpx.rb +9 -0
  13. data/lib/authlogic_rpx/acts_as_authentic.rb +297 -0
  14. data/lib/authlogic_rpx/helper.rb +54 -0
  15. data/lib/authlogic_rpx/rpx_identifier.rb +4 -0
  16. data/lib/authlogic_rpx/session.rb +237 -0
  17. data/lib/authlogic_rpx/version.rb +51 -0
  18. data/rails/init.rb +1 -0
  19. data/slayer-authlogic_rpx.gemspec +102 -0
  20. data/test/fixtures/rpxresponses.yml +20 -0
  21. data/test/fixtures/users.yml +20 -0
  22. data/test/integration/basic_authentication_and_registration_test.rb +53 -0
  23. data/test/integration/internal_mapping/basic_authentication_and_registration_test.rb +3 -0
  24. data/test/integration/internal_mapping/settings_test.rb +10 -0
  25. data/test/integration/no_mapping/basic_authentication_and_registration_test.rb +3 -0
  26. data/test/integration/no_mapping/settings_test.rb +10 -0
  27. data/test/libs/ext_test_unit.rb +30 -0
  28. data/test/libs/mock_rpx_now.rb +34 -0
  29. data/test/libs/rails_trickery.rb +41 -0
  30. data/test/libs/rpxresponse.rb +3 -0
  31. data/test/libs/user.rb +3 -0
  32. data/test/libs/user_session.rb +3 -0
  33. data/test/test_helper.rb +85 -0
  34. data/test/test_internal_mapping_helper.rb +93 -0
  35. data/test/unit/acts_as_authentic_settings_test.rb +42 -0
  36. data/test/unit/session_settings_test.rb +38 -0
  37. data/test/unit/session_validation_test.rb +16 -0
  38. data/test/unit/verify_rpx_mock_test.rb +29 -0
  39. metadata +168 -0
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ pkg
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,46 @@
1
+ == 1.2.0 released 2010-09-29
2
+
3
+ * added rails 3 compatibility (GH#24)
4
+ * updated gem support to authlogic 2.1.6 and rpx_now 0.6.23
5
+ * fix GH#21 internal mapping fails if user model is not called "User"
6
+ * fix GH#18 Add methods to retrieve "all rpx users" and "all non-rpx users"
7
+
8
+ == 1.1.1 released 2010-01-17
9
+
10
+ * updated gem support to rpx_now 0.6.12 (includes url encoding fix)
11
+
12
+ == 1.1.0 released 2010-01-05
13
+
14
+ * added identity mapping and merging support [GH#10,GH#13]
15
+ * new configuration parameters: account_mapping_mode; account_merge_enabled.
16
+ * new callbacks: before_merge_rpx_data; after_merge_rpx_data.
17
+ * updated gem support to authlogic 2.1.3 and rpx_now 0.6.11
18
+ * support all rpx_now options in popup/embed code [GH#6]
19
+ * automated tests are now working [GH#7]
20
+ * documentation updates
21
+
22
+ == 1.0.4 released 2009-10-10
23
+
24
+ * added new hooks for profile mapping (Session.map_rpx_data_each_login, ActsAsAuthentic.map_added_rpx_data) based on suggestion by trosser (github issue #5)
25
+ * now supporting obtrusive (javascript pop-over) and unobtrusive (link) RPX pop-up sign-in forms. See rpx_popup method. (github issue #4)
26
+ * updated support for rpx_now gem version 0.6.6
27
+ * documentation updates
28
+
29
+ == 1.0.3 released 2009-10-07
30
+
31
+ * added general error handler for session validation to give clean 'failure' when underlying errors encountered (e.g. user model database constraint violation)
32
+ * updated documentation
33
+
34
+ == 1.0.2 released 2009-09-27
35
+
36
+ * Fixed issue with rpx_popup that was causing an error on some webkit-based browsers (incl chrome)
37
+
38
+ == 1.0.1 released 2009-09-26
39
+
40
+ * Initial public release
41
+ * RPX profile mappings switched to use indirect Authlogic field naming
42
+ * Documentation updated
43
+
44
+ == 1.0.0 released 2009-09-25
45
+
46
+ * Initial release
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Paul Gallagher (tardate.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 ADDED
@@ -0,0 +1,37 @@
1
+ CHANGELOG.rdoc
2
+ MIT-LICENSE
3
+ Manifest
4
+ README.rdoc
5
+ Rakefile
6
+ authlogic_rpx.gemspec
7
+ generators/add_authlogic_rpx_migration/USAGE
8
+ generators/add_authlogic_rpx_migration/add_authlogic_rpx_migration_generator.rb
9
+ generators/add_authlogic_rpx_migration/templates/migration_internal_mapping.rb
10
+ generators/add_authlogic_rpx_migration/templates/migration_no_mapping.rb
11
+ init.rb
12
+ lib/authlogic_rpx.rb
13
+ lib/authlogic_rpx/acts_as_authentic.rb
14
+ lib/authlogic_rpx/helper.rb
15
+ lib/authlogic_rpx/rpx_identifier.rb
16
+ lib/authlogic_rpx/session.rb
17
+ lib/authlogic_rpx/version.rb
18
+ rails/init.rb
19
+ test/fixtures/rpxresponses.yml
20
+ test/fixtures/users.yml
21
+ test/integration/basic_authentication_and_registration_test.rb
22
+ test/integration/internal_mapping/basic_authentication_and_registration_test.rb
23
+ test/integration/internal_mapping/settings_test.rb
24
+ test/integration/no_mapping/basic_authentication_and_registration_test.rb
25
+ test/integration/no_mapping/settings_test.rb
26
+ test/libs/ext_test_unit.rb
27
+ test/libs/mock_rpx_now.rb
28
+ test/libs/rails_trickery.rb
29
+ test/libs/rpxresponse.rb
30
+ test/libs/user.rb
31
+ test/libs/user_session.rb
32
+ test/test_helper.rb
33
+ test/test_internal_mapping_helper.rb
34
+ test/unit/acts_as_authentic_settings_test.rb
35
+ test/unit/session_settings_test.rb
36
+ test/unit/session_validation_test.rb
37
+ test/unit/verify_rpx_mock_test.rb
data/README.rdoc ADDED
@@ -0,0 +1,755 @@
1
+ = Authlogic_RPX
2
+
3
+ == Purpose
4
+
5
+ Authlogic_RPX is an Authlogic extension library that provides support for authentication using the Janrain Engage (formerly RPX) multi-authentication service offered by JanRain. To use RPX, you must first register your application at {Janrain Engage}[http://rpxnow.com/]. A free "Basic" account is available, in addition to paid enhanced versions. All work with Authlogic_RPX.
6
+
7
+ Key features and capabilities:
8
+ * You can offer your users all the authentication methods supported by RPX (OpenID, Facebook, twitter, Google, Yahoo!, LinkedIn etc)
9
+ * Auto-registration by default following RPX authentication (can be disabled if required)
10
+ * View helpers to assist with inserting login fragments in pages
11
+ * Can co-exist with standard password authentication
12
+ * Allow users to enable RPX authentication for any existing password-enabled accounts
13
+ * Supports identity mapping and merging (allowing users to have multiple logins associated with one member record on your site)
14
+ * Supports Rails 2.3.x and 3.0.x
15
+
16
+
17
+ == Authlogic_RPX References
18
+
19
+ * <b>Authlogic_RPX gem repo:</b> [http://github.com/tardate/authlogic_rpx]
20
+ * <b>Authlogic_RPX issues and feedback:</b> [http://github.com/tardate/authlogic_rpx/issues]
21
+
22
+ The demonstration Rails application is where you can see Authlogic_RPX in action:
23
+
24
+ * <b>Live Demonstration Site:</b> [http://rails-authlogic-rpx-sample.heroku.com]
25
+ * <b>Demonstration site source repository (rails2 branch):</b> [http://github.com/tardate/rails-authlogic-rpx-sample]
26
+ * <b>Demonstration site source repository (rails3 branch):</b> [http://github.com/tardate/rails-authlogic-rpx-sample/tree/rails3]
27
+
28
+ == Authlogic and RPX References
29
+
30
+ * <b>Authlogic documentation:</b> [http://rdoc.info/projects/binarylogic/authlogic]
31
+ * <b>Authlogic repo:</b> [http://github.com/binarylogic/authlogic]
32
+ * <b>RPX documentation:</b> [https://rpxnow.com/docs]
33
+ * <b>RPX_now gem repo:</b> [http://github.com/grosser/rpx_now]
34
+
35
+
36
+ == Installing Authlogic_RPX gem
37
+
38
+ Three gems are required: authlogic, rpx_now, and authlogic_rpx. Install these as appropriate to your environment and preferences.
39
+
40
+ Currently tested versions:
41
+ * authlogic 2.1.6
42
+ * rpx_now 0.6.23
43
+ * authlogic_rpx 1.2.0
44
+
45
+
46
+ === 1. Direct gem installation
47
+
48
+ # installs authlogic and rpx_now gem dependencies
49
+ sudo gem install authlogic_rpx
50
+
51
+ === 2. Using Rails 2.3.x config.gems
52
+
53
+ Include in config/environment.rb:
54
+
55
+ config.gem 'authlogic', :version => '= 2.1.6'
56
+ config.gem 'rpx_now', :version => '= 0.6.23'
57
+ config.gem 'authlogic_rpx', :version => '>= 1.2.0'
58
+
59
+ Then to install, run from the command line:
60
+
61
+ sudo rake gems:install
62
+
63
+
64
+ === 3. Using .gems file (e.g for heroku.com deployments)
65
+
66
+ Include in RAILS_ROOT/.gems:
67
+
68
+ authlogic_rpx --version '>= 1.2.0'
69
+
70
+
71
+ === 4. Using Bundler for Rails 3
72
+
73
+ Include in RAILS_ROOT/Gemfile
74
+
75
+ gem 'authlogic', '= 2.1.6'
76
+ gem 'rpx_now', '= 0.6.23'
77
+ gem 'authlogic_rpx', '= 1.2.0'
78
+
79
+
80
+ == About Authlogic_RPX
81
+
82
+ Using Authlogic_RPX is very similar to using standard authlogic, with the addition of just a few configuration options. So if you already have a project setup with authlogic, adding RPX support will be trivial.
83
+
84
+ An important capability to be aware of is "auto registration". This means that when a user has logged in with RPX, if an account does not already exist in your application, it will be automatically created. That is, there is no separate/special "register" step for users to go through before just signing in. You can disable this if you need, but for most sites that use RPX as a primary authentication mechanism, this is probably what you want to happen.
85
+
86
+ One of the main limitations of Authlogic_RPX versions up to 1.0.4 was that it did not include any specific support for identity mapping. This means that if a user signs in with twitter one day, and facebook the next, then your application would see these as tow distinct users (NB: RPX provides some protection for this by trying to remember the last authentication method your users used. It's not always perfect however).
87
+
88
+ From Authlogic_RPX version 1.1.0 we have added built-in identity mapping and merging support. This is what we call 'internal' mapping. The legacy approach from 1.0.4 and earlier is still supported as an option. This mapping mode is called 'none'.
89
+
90
+ The JanRain RPXnow service has its own identity mapping implementation, but only available for paid accounts. This is still not supported directly by Authlogic_RPX, but is something we'd like to get into a future version.
91
+
92
+ === Chosing Your Mapping Mode
93
+
94
+ When you configure Authlogic_RPX, you will need to decide which mapping mode to use.
95
+
96
+ The options are:
97
+ * :none - will use legacy/1.0.4 identity management with no mapping support. RPX identifiers are stored as a new attribute of your User model
98
+ * :internal - uses Authlogic_RPX-based mapping. RPX identifiers are stored in a new model called RPXIdentifier. This model is completely private to Authlogic_RPX and you will not need to code anything specifically for it.
99
+ * :rpxnow - currently not implemented; reserved for future use
100
+
101
+
102
+ ==== Upgrading from Authlogic_RPX 1.0.4 or earlier
103
+
104
+ In Authlogic_RPX v1.0.4 and earlier, the rpx_identifier was stored in the user model, and identity mapping was not supported.
105
+
106
+ If you are upgrading to 1.1.0 or later and wish to start using internal mapping:
107
+ * Use the add_authlogic_rpx_migration generator to create the migration to support the new RPXIdentifiers model
108
+ * It is NOT necessary to migrate the rpx_identifiers.
109
+ * DO NOT remove the legacy rpx_identifier column from your existing user model. The Authlogic_RPX code will migrate users to the new database structure as and when they login.
110
+ * You may wish to explicitly set account_mapping_mode to :internal in the User model (it saves a few cycles over the default :auto).
111
+
112
+ e.g.
113
+
114
+ class User < ActiveRecord::Base
115
+ acts_as_authentic do |c|
116
+ c.account_mapping_mode :internal
117
+ end
118
+ end
119
+
120
+ If you are upgrading to 1.1.0 or later and wish to continue using the legacy/1.0.4 approach (i.e. no mapping):
121
+ * You will already have the rpx_identifier in your user model. DO NOT create any new migrations.
122
+ * Just upgrade the gem and all should continue to work as before
123
+ * You may wish to explicitly set account_mapping_mode to :none in the User model (it saves a few cycles over the default :auto).
124
+
125
+ e.g.
126
+
127
+ class User < ActiveRecord::Base
128
+ acts_as_authentic do |c|
129
+ c.account_mapping_mode :none
130
+ end
131
+ end
132
+
133
+ == The Step-by-Step Guide to Using Authlogic_RPX
134
+
135
+ <i>Note: in what follows, the user model is called User and the session controller takes the name UserSession (the authlogic convention). You are not restricted to these names - could be Member and MemberSession for example - but for simplicity, this documentation will stick to using the "User" convention.</i>
136
+
137
+ The main steps for enabling Authlogic_RPX:
138
+ * 1. Enable RPX for your user model
139
+ * 2. Add RPX configuration for the Authlogic session model
140
+ * 3. Add custom user profile mapping (optional)
141
+ * 4. Add application controller helpers: current_user, current_user_session
142
+ * 5. Setup the Authlogic session controller
143
+ * 6. Setup the Authlogic user controller
144
+ * 7. Use view helpers to provide login links
145
+ * 8. Allow users to "Add RPX" to existing accounts (optional)
146
+ * 9. Customise Account Merge Behaviour (optional)
147
+
148
+
149
+ === 1. Enable RPX for your user model
150
+
151
+ The user model will have a has_many relationship with a new model, called RPXIdentifier.
152
+ A generator is provider to create the necessary migration:
153
+
154
+ ruby script/generate add_authlogic_rpx_migration [mapping:mapping_mode] [user_model:model_name]
155
+
156
+ The generator takes two optional parameters: mapping and user_model.
157
+
158
+ The mapping_mode parameter indicates which style of Authlogic_RPX-supported identity mapping should be used. The default mapping_mode is 'internal' Allowed values for mapping_mode are:
159
+ * none (disables authlogic_rpx identity mapping. This is the same behaviour as in authlogic_rpx version 1.0.4 and earlier)
160
+ * internal (enables authlogic_rpx internal identity mapping. This behaviour was introduced in version 1.1.0)
161
+
162
+ The user_model parameter specifies the name of the user/member model in your application. The default model_name is 'User'. e.g. to generate the RPX migration where the user model is called 'Member' and you do not want to support identity mapping:
163
+
164
+ ruby script/generate add_authlogic_rpx_migration mapping:none user_model:member
165
+
166
+ You may need to customise the migration file to remove database constraints on other fields if they will be unused in the RPX case
167
+ (e.g. crypted_password and password_salt to make password authentication optional).
168
+
169
+ If you are using auto-registration, you must also remove any database constraints for fields that will be automatically mapped
170
+ (see notes in "3. Add custom user profile mapping during auto-registration")
171
+
172
+ ==== Sample Migration Generated Script (using internal mapping)
173
+
174
+ The following command will generate a migration for the case where you want to use authlogic_rpx internal mapping and your user model is called 'User':
175
+
176
+ ruby script/generate add_authlogic_rpx_migration mapping:internal user_model:user
177
+
178
+ The migration script will appear like this:
179
+
180
+ class AddAuthlogicRpxMigration < ActiveRecord::Migration
181
+ def self.up
182
+ create_table :rpx_identifiers do |t|
183
+ t.string :identifier, :null => false
184
+ t.string :provider_name
185
+ t.integer :user_id, :null => false
186
+ t.timestamps
187
+ end
188
+ add_index :rpx_identifiers, :identifier, :unique => true, :null => false
189
+ add_index :rpx_identifiers, :user_id, :unique => false, :null => false
190
+
191
+ # == Customisation may be required here ==
192
+ # You may need to remove database constraints on other fields if they will be unused in the RPX case
193
+ # (e.g. crypted_password and password_salt to make password authentication optional).
194
+ # If you are using auto-registration, you must also remove any database constraints for fields that will be automatically mapped
195
+ # e.g.:
196
+ #change_column :users, :crypted_password, :string, :default => nil, :null => true
197
+ #change_column :users, :password_salt, :string, :default => nil, :null => true
198
+
199
+ end
200
+
201
+ def self.down
202
+ drop_table :rpx_identifiers
203
+
204
+ # == Customisation may be required here ==
205
+ # Restore user model database constraints as appropriate
206
+ # e.g.:
207
+ #[:crypted_password, :password_salt].each do |field|
208
+ # User.all(:conditions => "#{field} is NULL").each { |user| user.update_attribute(field, "") if user.send(field).nil? }
209
+ # change_column :users, field, :string, :default => "", :null => false
210
+ #end
211
+
212
+ end
213
+ end
214
+
215
+ {See the source for the sample 20091227051253_add_authlogic_rpx_migration.rb}[http://github.com/tardate/rails-authlogic-rpx-sample/blob/master/db/migrate/20091227051253_add_authlogic_rpx_migration.rb].
216
+
217
+ ==== Sample Migration Generated Script (using no mapping)
218
+
219
+ The following command will generate a migration for the case where you don't want to use authlogic_rpx mapping and your user model is called 'Member':
220
+
221
+ ruby script/generate add_authlogic_rpx_migration mapping:none user_model:member
222
+
223
+ The migration script will appear like this:
224
+
225
+ class AddAuthlogicRpxMigration < ActiveRecord::Migration
226
+
227
+ def self.up
228
+ add_column :members, :rpx_identifier, :string
229
+ add_index :members, :rpx_identifier
230
+
231
+ # == Customisation may be required here ==
232
+ # You may need to remove database constraints on other fields if they will be unused in the RPX case
233
+ # (e.g. crypted_password and password_salt to make password authentication optional).
234
+ # If you are using auto-registration, you must also remove any database constraints for fields that will be automatically mapped
235
+
236
+ # e.g.:
237
+ #change_column :members, :crypted_password, :string, :default => nil, :null => true
238
+ #change_column :members, :password_salt, :string, :default => nil, :null => true
239
+
240
+ end
241
+
242
+ def self.down
243
+ remove_column :members, :rpx_identifier
244
+
245
+ # == Customisation may be required here ==
246
+ # Restore user model database constraints as appropriate
247
+ # e.g.:
248
+ #[:crypted_password, :password_salt].each do |field|
249
+ # Member.all(:conditions => "#{field} is NULL").each { |user| user.update_attribute(field, "") if user.send(field).nil? }
250
+ # change_column :members, field, :string, :default => "", :null => false
251
+ #end
252
+
253
+ end
254
+ end
255
+
256
+ ==== Configuring the User model
257
+
258
+ The user model then needs to be tagged with "acts_as_authentic". This is the minimal configuration:
259
+
260
+ class User < ActiveRecord::Base
261
+ acts_as_authentic
262
+ end
263
+
264
+ Two RPX-specific user configuration options are available.
265
+ * account_merge_enabled: true/false. Enables/disables user auto-registration (disabled by default)
266
+ * account_mapping_mode: :auto/:none/:internal. Sets the Authlogic_RPX identity mapping mode (:auto by default)
267
+
268
+ The account_mapping_mode options are defined as follows:
269
+ * :auto - will select the correct mapping mode based on the table structures you have provisioned in the application
270
+ * :none - will use legacy/1.0.4 identity management with no mapping support. RPX identifiers are stored as a new attribute of your User model
271
+ * :internal - uses Authlogic_RPX-based mapping. RPX identifiers are stored in a new model called RPXIdentifier. This model is completely private to Authlogic_RPX and you will not need to code anything specifically for it.
272
+ * :rpxnow - currently not implemented; reserved for future use.
273
+
274
+ For example, the following shows how to set standard Authlogic configurations (validations_scope), enables RPX account merging, and specifies :internal account mapping:
275
+
276
+ class User < ActiveRecord::Base
277
+ acts_as_authentic do |c|
278
+ c.validations_scope = :company_id # for available Authlogic options see documentation in the various Config modules of Authlogic::ActsAsAuthentic
279
+
280
+ # enable Authlogic_RPX account merging (false by default, if this statement is not present)
281
+ c.account_merge_enabled true
282
+
283
+ # set Authlogic_RPX account mapping mode
284
+ c.account_mapping_mode :internal
285
+
286
+ end # block optional
287
+
288
+ end
289
+
290
+ {See the source for the sample user.rb}[http://github.com/tardate/rails-authlogic-rpx-sample/blob/master/app/models/user.rb].
291
+
292
+ NB: The RPXIdentifier model is included in the authlogic_rpx gem and does not need to be added to your project.
293
+
294
+ === 2. Add RPX configuration for the Authlogic session model
295
+
296
+ Authlogic provides a helper to create the session model:
297
+
298
+ script/generate session user_session
299
+
300
+ The minimum configuration required is to add your RPX_API_KEY:
301
+
302
+ class UserSession < Authlogic::Session::Base
303
+ rpx_key RPX_API_KEY
304
+ end
305
+
306
+ Get an API key by registering your application at {RPX}[http://rpxnow.com/]. A free "Basic" account is available, in addition to paid enhanced versions. All work with Authlogic_RPX.
307
+
308
+ You probably don't want to put your API key in directly. A recommended approach is to set the key as an environment variable, and then set it as a constant in config/environment.rb:
309
+
310
+ RPX_API_KEY = ENV['RPX_API_KEY']
311
+
312
+ Two additional RPX-specific session configuration options are available.
313
+ * auto_register: enable/disable user auto-registration (enabled by default)
314
+ * rpx_extended_info: enable/disable extended profile information in the RPX authentication (disabled by default)
315
+
316
+ For example, to disable auto-registration and enable extended info:
317
+
318
+ class UserSession < Authlogic::Session::Base
319
+ rpx_key RPX_API_KEY
320
+ auto_register false
321
+ rpx_extended_info
322
+ end
323
+
324
+ {See the source for the sample user_session.rb}[http://github.com/tardate/rails-authlogic-rpx-sample/blob/master/app/models/user_session.rb].
325
+
326
+ === 3. Add custom user profile mapping (optional)
327
+
328
+ Authlogic_rpx provides three hooks for mapping information from the RPX profile into your application's user model:
329
+
330
+ * map_rpx_data: user profile mapping during auto-registration
331
+ * map_rpx_data_each_login: user profile mapping during login
332
+ * map_added_rpx_data: user profile mapping when adding RPX to an existing account
333
+
334
+ See https://rpxnow.com/docs#profile_data for the definition of available attributes in the RPX profile.
335
+
336
+ ==== 3a. map_rpx_data: user profile mapping during auto-registration
337
+
338
+ When users auto-register, profile data from RPX is available to be inserted in the user's record on your site. By default, authlogic_rpx will map the username and email fields.
339
+
340
+ If you have other fields you want to map, you can provide your own implementation of the map_rpx_data method in the UserSession model. In that method, you will be updating the "self.attempted_record" object, with information from the "@rpx_data" object. See the {RPX documentation}[https://rpxnow.com/docs#profile_data] to find out about the set of information that is available.
341
+
342
+ class UserSession < Authlogic::Session::Base
343
+ rpx_key RPX_API_KEY
344
+ rpx_extended_info
345
+
346
+ private
347
+
348
+ # map_rpx_data maps additional fields from the RPX response into the user object
349
+ # override this in your session controller to change the field mapping
350
+ # see https://rpxnow.com/docs#profile_data for the definition of available attributes
351
+ #
352
+ def map_rpx_data
353
+ # map core profile data using authlogic indirect column names
354
+ self.attempted_record.send("#{klass.login_field}=", @rpx_data['profile']['preferredUsername'] ) if attempted_record.send(klass.login_field).blank?
355
+ self.attempted_record.send("#{klass.email_field}=", @rpx_data['profile']['email'] ) if attempted_record.send(klass.email_field).blank?
356
+
357
+ # map some other columns explicitly
358
+ self.attempted_record.fullname = @rpx_data['profile']['displayName'] if attempted_record.fullname.blank?
359
+
360
+ if rpx_extended_info?
361
+ # map some extended attributes
362
+ end
363
+ end
364
+
365
+ end
366
+
367
+
368
+ WARNING: if you are using auto-registration, any fields you map should NOT have constraints enforced at the database level.
369
+ Authlogic_rpx will optimistically attempt to save the user record during registration, and violating a database constraint will cause the authentication/registration to fail.
370
+
371
+ You can/should enforce any required validations at the model level e.g.
372
+
373
+ validates_uniqueness_of :username, :case_sensitive => false
374
+
375
+ This will allow the auto-registration to proceed, and the user can be given a chance to rectify the validation errors on your user profile page.
376
+
377
+ If it is not acceptable in your application to have user records created with potential validation errors in auto-populated fields, you will need to override map_rpx_data and implement whatever special handling makes sense in your case. For example:
378
+
379
+ * directly check for uniqueness and other validation requirements
380
+ * automatically "uniquify" certain fields like username
381
+ * save conflicting profile information to "pending user review" columns or a seperate table
382
+
383
+ {See the source for the sample user_session.rb}[http://github.com/tardate/rails-authlogic-rpx-sample/blob/master/app/models/user_session.rb].
384
+
385
+ ==== 3b. map_rpx_data_each_login: user profile mapping during login
386
+
387
+ map_rpx_data_each_login provides a hook to allow you to map RPX profile information every time the user logs in.
388
+
389
+ By default, nothing is mapped. If you have other fields you want to map, you can provide your own implementation of the map_rpx_data_each_login method in the UserSession model.
390
+
391
+ This would mainly be used to update relatively volatile information that you are maintaining in the user model (such as profile image url)
392
+
393
+ In the map_rpx_data_each_login procedure, you will be writing to fields of the "self.attempted_record" object, pulling data from the @rpx_data object. For example:
394
+
395
+ def map_rpx_data_each_login
396
+ # we'll always update photo_url
397
+ self.attempted_record.photo_url = @rpx_data['profile']['photo']
398
+ end
399
+
400
+ {See the source for the sample user_session.rb}[http://github.com/tardate/rails-authlogic-rpx-sample/blob/master/app/models/user_session.rb].
401
+
402
+
403
+ ==== 3c. map_added_rpx_data: user profile mapping when adding RPX to an existing account
404
+
405
+ map_added_rpx_data maps additional fields from the RPX response into the user object during the "add RPX to existing account" process.
406
+
407
+ Override this in your user model to perform field mapping as may be desired.
408
+ Provide your own implementation of the map_added_rpx_data method in the User model (NOT UserSession, unlike for map_rpx_data and map_rpx_data_each_login).
409
+
410
+ In the map_added_rpx_data procedure, you will be writing to fields of the "self" object, pulling data from the rpx_data parameter. For example:
411
+
412
+ def map_added_rpx_data( rpx_data )
413
+ # map some additional fields, e.g. photo_url
414
+ self.photo_url = rpx_data['profile']['photo'] if photo_url.blank?
415
+ end
416
+
417
+ {See the source for the sample user.rb}[http://github.com/tardate/rails-authlogic-rpx-sample/blob/master/app/models/user.rb].
418
+
419
+
420
+ === 4. Add application controller helpers: current_user, current_user_session
421
+
422
+ We'll add current_user and current_user_session helpers. These can then be used in controllers and views to get a handle on the "current" logged in user.
423
+
424
+ class ApplicationController < ActionController::Base
425
+ helper :all # include all helpers, all the time
426
+ protect_from_forgery # See ActionController::RequestForgeryProtection for details
427
+
428
+ # Scrub sensitive parameters from your log
429
+ filter_parameter_logging :password, :password_confirmation
430
+
431
+ helper_method :current_user, :current_user_session
432
+
433
+ private
434
+
435
+ def current_user_session
436
+ return @current_user_session if defined?(@current_user_session)
437
+ @current_user_session = UserSession.find
438
+ end
439
+
440
+ def current_user
441
+ return @current_user if defined?(@current_user)
442
+ @current_user = current_user_session && current_user_session.record
443
+ end
444
+ end
445
+
446
+ {See the source for the sample user_session_controller.rb}[http://github.com/tardate/rails-authlogic-rpx-sample/blob/master/app/controllers/application_controller.rb].
447
+
448
+
449
+ === 5. Setup the Authlogic session controller
450
+
451
+ If you don't already have a user session controller, create one. There are four actions of significance for authlogic_rpx:
452
+
453
+ $ script/generate controller user_sessions index new create destroy
454
+
455
+ {See the source for the sample user_session_controller.rb}[http://github.com/tardate/rails-authlogic-rpx-sample/blob/master/app/controllers/user_sessions_controller.rb].
456
+
457
+ In config/routes.rb we can define the standard routes for this controller and two named routes for the main login/out (or singin/out if you prefer that terminology):
458
+
459
+ map.signin "signin", :controller => "user_sessions", :action => "new"
460
+ map.signout "signout", :controller => "user_sessions", :action => "destroy"
461
+ map.resources :user_sessions
462
+
463
+ ==== index
464
+ This is where RPX will return to if the user cancelled the login process, so it needs to be handled. You probably just want to redirect the user to an appropriate alternative:
465
+
466
+ def index
467
+ redirect_to current_user ? root_url : new_user_session_url
468
+ end
469
+
470
+ ==== new
471
+ Typically used to render a login form
472
+
473
+ def new
474
+ @user_session = UserSession.new
475
+ end
476
+
477
+ ==== create
478
+ This is where the magic happens for authentication. Authlogic hides all the underlying wiring, and you just need to "save" the session!
479
+
480
+ Authlogic_rpx provides two additional methods that you might want to use to tailor you application behaviour:
481
+ * new_registration? - if a new registration, e.g. force them to go via a registration follow-up page
482
+ * registration_complete? - if registration details not complete, e.g. bounce the user over the profile editing page
483
+
484
+ def create
485
+ @user_session = UserSession.new(params[:user_session])
486
+ if @user_session.save
487
+ if @user_session.new_registration?
488
+ flash[:notice] = "Welcome! As a new user, please review your registration details before continuing.."
489
+ redirect_to edit_user_path( :current )
490
+ else
491
+ if @user_session.registration_complete?
492
+ flash[:notice] = "Successfully signed in."
493
+ redirect_back_or_default articles_path
494
+ else
495
+ flash[:notice] = "Welcome back! Please complete required registration details before continuing.."
496
+ redirect_to edit_user_path( :current )
497
+ end
498
+ end
499
+ else
500
+ flash[:error] = "Failed to login or register."
501
+ redirect_to new_user_session_path
502
+ end
503
+ end
504
+
505
+ ==== destroy
506
+ The logout action..
507
+
508
+ def destroy
509
+ @user_session = current_user_session
510
+ @user_session.destroy if @user_session
511
+ flash[:notice] = "Successfully signed out."
512
+ redirect_to articles_path
513
+ end
514
+
515
+
516
+ === 6. Setup the Authlogic user controller
517
+
518
+ The users controller handles the actual user creation and editing actions. In it's standard form, it looks like any other controller with an underlying ActiveRecord model.
519
+
520
+ There are five basic actions to consider. If you don't already have a controller, create it:
521
+
522
+ $ script/generate controller users new create edit show update
523
+
524
+ {See the source for the sample users_controller.rb}[http://github.com/tardate/rails-authlogic-rpx-sample/blob/master/app/controllers/users_controller.rb].
525
+
526
+ The users controller just needs standard routes defined in config/routes.rb:
527
+
528
+ map.resources :users
529
+
530
+ ==== new
531
+ Stock standard form for a user to register on the site. Only required if you will allow users to register without using RPX auto-registration (using standard password authentication).
532
+
533
+ def new
534
+ @user = User.new
535
+ end
536
+
537
+ ==== create
538
+ As for new, stock standard and only required if you will allow users to register without using RPX auto-registration.
539
+
540
+ def create
541
+ @user = User.new(params[:user])
542
+ if @user.save
543
+ flash[:notice] = "Successfully registered user."
544
+ redirect_to articles_path
545
+ else
546
+ render :action => 'new'
547
+ end
548
+ end
549
+
550
+ ==== show
551
+ Display's the user's profile. Uses the current_user helper that we'll include in the application controller.
552
+
553
+ def show
554
+ @user = current_user
555
+ end
556
+
557
+ ==== edit
558
+ Allows the user to edit their profile. Calling valid? will ensure any validation errors are highlighted. This can be relevant with RPX since auto-registration may not include all the profile data you want to make "mandatory" for normal users.
559
+
560
+ def edit
561
+ @user = current_user
562
+ @user.valid?
563
+ end
564
+
565
+ ==== update
566
+ Handles the submission of the edit form. Again, uses the current_user helper that we'll include in the application controller.
567
+
568
+ def update
569
+ @user = current_user
570
+ @user.attributes = params[:user]
571
+ if @user.save
572
+ flash[:notice] = "Successfully updated user."
573
+ redirect_back_or_default articles_path
574
+ else
575
+ render :action => 'edit'
576
+ end
577
+ end
578
+
579
+
580
+ === 7. Use view helpers to provide login links
581
+
582
+ So how to put a "login" link on your page? Two helper methods are provided:
583
+ * <b>rpx_popup</b> helper to insert a link to pop-up RPX login
584
+ * <b>rpx_embed</b> helper to insert an embedded iframe RPX login form
585
+
586
+ Each takes an options hash:
587
+ * <tt>link_text:</tt> text to use in the link (only used by rpx_popup)
588
+ * <tt>app_name:</tt> name of the application you set when registering your service at rpxnow.com (will be prepended to RPX domain and used in RPX dialogues)
589
+ * <tt>return_url:</tt> url for the RPX callback (e.g. user_sessions_url)
590
+ * <tt>add_rpx:</tt> Optional. If true, requests RPX callback to add to current session. Else runs normal authentication process (default). See "7. Allow users to "Add RPX" to existing accounts"
591
+ * <tt>unobtrusive:</tt> true/false; sets javascript style for link. unobtrusive=true links directly to rpxnow site, whereas unobtrusive=false does a javascript pop-over. Default: true (only used by rpx_popup)
592
+
593
+ For example, to insert a login link in a navigation bar is as simple as this:
594
+
595
+ <div id="user_nav">
596
+ <%= link_to "Home", root_path %> |
597
+ <% if current_user %>
598
+ <%= link_to "Profile", user_path(:current) %> |
599
+ <%= link_to "Sign out", signout_path %>
600
+ <% else %>
601
+ <%= rpx_popup( :link_text => "Register/Sign in with RPX..", :app_name => RPX_APP_NAME, :return_url => user_sessions_url, :unobtrusive => false ) %>>
602
+ <% end %>
603
+ </div>
604
+
605
+ <b>NOTE:</b> One of the most common problems people encounter in testing out authlogic_rpx is to <b>not set the correct :app_name</b>.
606
+
607
+ <b>NOTE2:</b> Make sure the application name is entered all in lowercase. If you do not, it can cause SSL certificate errors to be displayed when logging in with certain browsers (notably Android 2.1 webkit).
608
+
609
+ === 8. Allow users to "Add RPX" to existing accounts (optional)
610
+
611
+ If you got this far and have a working application, you are ready to go, especially if you only plan to support RPX authentication.
612
+
613
+ However, if you support other authentication methods (e.g. by password), you probably want the ability to let user's add RPX to an existing account. This is not possible by default, however adding it is simply a matter of providing another method on your user controller.
614
+
615
+ The route may be called anything you like. Let's use "addrpxauth" for example.
616
+
617
+ # This action has the special purpose of receiving an update of the RPX identity information
618
+ # for current user - to add RPX authentication to an existing non-RPX account.
619
+ # RPX only supports :post, so this cannot simply go to update method (:put)
620
+ def addrpxauth
621
+ @user = current_user
622
+ if @user.save
623
+ flash[:notice] = "Successfully added RPX authentication for this account."
624
+ render :action => 'show'
625
+ else
626
+ render :action => 'edit'
627
+ end
628
+ end
629
+
630
+ {This is demonstrated in the sample users_controller.rb}[http://github.com/tardate/rails-authlogic-rpx-sample/blob/master/app/controllers/users_controller.rb].
631
+
632
+ You'll note this is almost identical to the "update". The main difference is that it needs to be enabled for :post by RPX. In config/routes.rb:
633
+
634
+ map.addrpxauth "addrpxauth", :controller => "users", :action => "addrpxauth", :method => :post
635
+
636
+ To make an "Add RPX authentication for this account.." link, use rpx_popup as for normal RPX login, but set the return_url to the "addrpxauth" callback you have provided, and set the option :add_rpx to tru:
637
+
638
+ <%= rpx_popup( :link_text => "Add RPX authentication for this account..", :app_name => RPX_APP_NAME, :return_url => addrpxauth_url, :add_rpx => true, :unobtrusive => false ) %>
639
+
640
+
641
+ === 9. Customise Account Merge Behaviour (optional)
642
+
643
+ Account merging is disabled by default. It is enabled by setting account_merge_enabled to true in the User model:
644
+
645
+ class User < ActiveRecord::Base
646
+ acts_as_authentic do |c|
647
+ c.account_merge_enabled true
648
+ end
649
+ end
650
+
651
+
652
+ Account merging is applicable if you have allowed users to add RPX to an existing accounts (see 8. Allow users to "Add RPX" to existing accounts). When merging is enabled, Authlogic_RPX will migrate the RPX login identifier(s) from other users who had previously claimed the identifiers now being used.
653
+
654
+ For example, take the following scenario:
655
+ * Joe registers and creates an account using RPX identifier A (say, a twitter account)
656
+ * Joseph registers and creates an account using RPX identifier B (say, an OpenID account)
657
+ * It so happens that Joe and Joseph are the same person...
658
+ * Joseph signs in with RPX identifier B, and uses the "Add RPX" feature to attempt to add RPX identifier A to his account
659
+ * At this point, if you have account_merge_enabled disabled (the default), it will fail since the id is already used by Joe
660
+ * If you have account_merge_enabled enabled, Authlogic_RPX will transfer the RPX identifier A to Joseph's account
661
+ * If you are using account mapping = :none, RPX identifier A will just replace RPX identifier B for Joseph
662
+ * If you are using account mapping = :internal, RPX identifier A will be added to Joseph's account (he can now login with both A and B)
663
+ * The default behaviour of account mapping will leave Joe's account in place (but with no way to login via RPX). Authlogic_RPX does not merge any other details (e.g. application data ownership)
664
+
665
+ Authlogic_rpx provides two hooks for customising the account merge behaviour to handle things like migration of application objects and cleaning up old accounts:
666
+
667
+ * before_merge_rpx_data: called before the RPX identifiers are transfered. It provides a hook for application developers to perform data migration prior to the merging of user accounts.
668
+ * after_merge_rpx_data: called after the RPX identifiers are transfered. It provides a hook for application developers to perform account clean-up after (perhaps delete or disable to account merged from)
669
+
670
+
671
+ The Authlogic_RPX sample application provides an example of migrating application objects and cleaning up obsolete accounts. From the user model:
672
+
673
+ # before_merge_rpx_data provides a hook for application developers to perform data migration prior to the merging of user accounts.
674
+ # This method is called just before authlogic_rpx merges the user registration for 'from_user' into 'to_user'
675
+ # Authlogic_RPX is responsible for merging registration data.
676
+ #
677
+ # By default, it does not merge any other details (e.g. application data ownership)
678
+ #
679
+ def before_merge_rpx_data( from_user, to_user )
680
+ to_user.articles << from_user.articles
681
+ to_user.comments << from_user.comments
682
+ end
683
+
684
+ # after_merge_rpx_data provides a hook for application developers to perform account clean-up after authlogic_rpx has
685
+ # migrated registration details.
686
+ #
687
+ # By default, does nothing. It could, for example, be used to delete or disable the 'from_user' account
688
+ #
689
+ def after_merge_rpx_data( from_user, to_user )
690
+ from_user.destroy
691
+ end
692
+
693
+ {See the sample user.rb}[http://github.com/tardate/rails-authlogic-rpx-sample/blob/master/app/models/user.rb].
694
+
695
+ === Ready to try it?
696
+
697
+ That's all there is. To see Authlogic_RPX in action, check out the demonstration Rails application:
698
+ * <b>Live Demonstration Site:</b> [http://rails-authlogic-rpx-sample.heroku.com]
699
+ * <b>Demonstration site source repository:</b> [http://github.com/tardate/rails-authlogic-rpx-sample]
700
+
701
+
702
+ == Improving Authlogic_RPX: next steps; how to help
703
+
704
+ Authlogic_RPX is open source and hosted on {github}[http://github.com/tardate/authlogic_rpx]. Developer's are welcome to fork and play - if you have improvements or bug fixes, just send a request to pull from your fork.
705
+
706
+ If you have issues or feedback, please log them in the {issues list on github}[http://github.com/tardate/authlogic_rpx/issues]
707
+
708
+ Some of the improvements currently on the radar:
709
+ * Implement/verify support for RPX "paid" service features of their "Plus" and "Pro" accounts (to date, only tested with free RPX "Basic" account)
710
+
711
+
712
+ == Note on programmatically grabbing an authenticated session
713
+
714
+ If you need to programmatically perform proxy authentication as a specific user (e.g. to run a batch process on behalf of the user), authlogic provides the necessary capability and this can be used with RPX-authenticated users too:
715
+
716
+ app.get "/" # force Authlogic::Session::Base.controller activation
717
+ user = User.find(:first)
718
+ session = UserSession.create(user, true) # skip authentication and log the user in directly, the true means "remember me"
719
+ session.valid?
720
+ => true
721
+
722
+
723
+ == Internals
724
+
725
+ Some design principles:
726
+ * Attempted to stay as close to binarylogic's "unobtrusive authentication" sensibility in Authlogic design
727
+ * All direct RPX processing is handled in the AuthlogicRpx::Session class (not in the ActiveRecord model)
728
+ * It uses the plug-in architecture introduced in Authlogic v2.0.
729
+
730
+ ==== building the gem
731
+
732
+ * increment the version in lib/authlogic_rpx/version.rb
733
+ * update gem version refs in README.rdoc
734
+ * update CHANGELOG.rdoc
735
+
736
+ Build and distribute (the gemcutter way):
737
+
738
+ # update gemspec
739
+ $ rake gemspec
740
+ # build the gem
741
+ $ rake build
742
+ # push the gem to gemcutter (e.g. for version 1.0.3)
743
+ gem push authlogic_rpx-1.0.3.gem
744
+
745
+
746
+ == Kudos and Kopywrite
747
+
748
+ Thanks to {binarylogic}[http://github.com/binarylogic] for cleaning up authentication in rails by creating Authlogic in the first place and offering it to the community.
749
+
750
+ The idea of adding RPX support to authlogic is not new. Some early ideas were found in the following projects, although it was decided not to base this implementation on a fork of these, since the approaches varied considerably:
751
+ * <b>http://github.com/hunter/authlogic_rpx</b> an initial start, based on authlogic_openid and using rpx_now
752
+ * <b>http://github.com/gampleman/authlogic_rpx/</b> similar, but including an implementation of the RPX api
753
+
754
+ authlogic_rpx was created by Paul Gallagher (tardate.com) and released under the MIT license.
755
+ Big thanks for contributions from {Joris}[http://github.com/trooster], {John}[http://gitub.com/jjb], {Damir}[http://gitub.com/sidonath], {Ben}[http://github.com/Empact]