Changeset 6318
- Timestamp:
- 03/04/07 20:59:06 (2 years ago)
- Files:
-
- plugins/open_id_authentication/CHANGELOG (added)
- plugins/open_id_authentication/lib/open_id_authentication.rb (modified) (3 diffs)
- plugins/open_id_authentication/README (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/open_id_authentication/lib/open_id_authentication.rb
r6252 r6318 1 1 module OpenIdAuthentication 2 OPEN_ID_AUTHENTICATION_DIR = RAILS_ROOT + "/tmp/openids" 3 2 4 protected 3 5 # OpenIDs are expected to begin with http:// or https:// … … 22 24 yield :missing, identity_url, nil 23 25 when OpenID::SUCCESS 24 open_id_response.add_extension_arg('sreg','required', [fields[:required]].flatten * ',') if fields[:required] 25 open_id_response.add_extension_arg('sreg','optional', [fields[:optional]].flatten * ',') if fields[:optional] 26 redirect_to(open_id_response.redirect_url( 27 root_url, open_id_response.return_to( 28 "#{request.protocol + request.host_with_port + request.request_uri}?open_id_complete=1" 29 ) 30 )) 26 add_simple_registration_fields(open_id_response, fields) 27 redirect_to(open_id_redirect_url(open_id_response)) 31 28 end 32 29 end … … 47 44 48 45 def open_id_consumer 49 OpenID::Consumer.new(session, OpenID::FilesystemStore.new(RAILS_ROOT + "/tmp/openids")) 46 OpenID::Consumer.new(session, OpenID::FilesystemStore.new(OPEN_ID_AUTHENTICATION_DIR)) 47 end 48 49 50 def add_simple_registration_fields(open_id_response, fields) 51 open_id_response.add_extension_arg('sreg', 'required', [ fields[:required] ].flatten * ',') if fields[:required] 52 open_id_response.add_extension_arg('sreg', 'optional', [ fields[:optional] ].flatten * ',') if fields[:optional] 53 end 54 55 def open_id_redirect_url(open_id_response) 56 open_id_response.redirect_url( 57 request.protocol + request.host, 58 open_id_response.return_to("#{request.url}?open_id_complete=1") 59 ) 50 60 end 51 61 end plugins/open_id_authentication/README
r6252 r6318 19 19 This particular plugin also relies on the fact that the authentication action allows for both POST and GET operations. 20 20 If you're using RESTful authentication, you'll need to explicitly allow for this in your routes.rb. 21 22 This plugin relies on Rails Edge revision 6317 or newer. 21 23 22 24 … … 47 49 protected 48 50 def password_authentication(name, password) 49 if @current_user = @account.users. find_by_name_and_password(params[:name], params[:password])51 if @current_user = @account.users.authenticate(params[:name], params[:password]) 50 52 successful_login 51 53 else … … 84 86 redirect_to(new_session_url) 85 87 end 86 87 # Set #root_url if your root url has a different named route.88 #89 # map.home '', :controller => ..., :action => ...90 #91 # Otherwise, name the route 'root' and leave this method out.92 def root_url93 home_url94 end95 88 end 89 96 90 97 91 Simple Registration OpenID Extension … … 105 99 # Pass optional :required and :optional keys to specify what sreg fields you want. 106 100 # Be sure to yield registration, a third argument in the #authenticate_with_open_id block. 107 authenticate_with_open_id(identity_url, :required => [:nickname, :email], :optional => :fullname) do |status, identity_url, registration| 101 authenticate_with_open_id(identity_url, 102 :required => [ :nickname, :email ], 103 :optional => :fullname) do |status, identity_url, registration| 108 104 case status 109 105 when :missing … … 115 111 when :successful 116 112 if @current_user = @account.users.find_by_identity_url(identity_url) 117 # registration is a hash containing the valid sreg keys given above 118 # use this to map them to fields of your user model 119 {'login=' => 'nickname', 'email=' => 'email', 'display_name=' => 'fullname'}.each do |attr, reg| 120 current_user.send(attr, registration[reg]) unless registration[reg].blank? 113 assign_registration_attributes!(registration) 114 115 if current_user.save 116 successful_login 117 else 118 failed_login "Your OpenID profile registration failed: " + 119 @current_user.errors.full_messages.to_sentence 121 120 end 122 unless current_user.save123 flash[:error] = "Error saving the fields from your OpenID profile: #{current_user.errors.full_messages.to_sentence}"124 end125 successful_login126 121 else 127 122 failed_login "Sorry, no user by that identity URL exists" … … 130 125 end 131 126 end 127 128 # registration is a hash containing the valid sreg keys given above 129 # use this to map them to fields of your user model 130 def assign_registration_attributes!(registration) 131 model_to_registration_mapping.each do |model_attribute, registration_attribute| 132 unless registration[registration_attribute].blank? 133 @current_user.send("#{model_attribute}=", registration[registration_attribute]) 134 end 135 end 136 end 137 138 def model_to_registration_mapping 139 { :login => 'nickname', :email => 'email', :display_name => 'fullname' } 140 end 141 132 142 133 143 Copyright (c) 2007 David Heinemeier Hansson, released under the MIT license