Changeset 6331
- Timestamp:
- 03/05/07 01:10:16 (1 year ago)
- Files:
-
- plugins/open_id_authentication/CHANGELOG (modified) (1 diff)
- plugins/open_id_authentication/lib/open_id_authentication.rb (modified) (5 diffs)
- plugins/open_id_authentication/README (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/open_id_authentication/CHANGELOG
r6324 r6331 1 * Added normalize_url and applied it to all operations going through the plugin [DHH] 2 3 * Removed open_id? as the idea of using the same input box for both OpenID and username has died -- use using_open_id? instead (which checks for the presence of params[:openid_url] by default) [DHH] 4 1 5 * Added OpenIdAuthentication::Result to make it easier to deal with default situations where you don't care to do something particular for each error state [DHH] 2 6 plugins/open_id_authentication/lib/open_id_authentication.rb
r6324 r6331 3 3 4 4 class Result 5 MESSAGES = {5 ERROR_MESSAGES = { 6 6 :missing => "Sorry, the OpenID server couldn't be found", 7 7 :canceled => "OpenID verification was canceled", 8 :failed => "Sorry, the OpenID verification failed", 9 :successful => "OpenID authentication successful" 8 :failed => "Sorry, the OpenID verification failed" 10 9 } 11 12 ERROR_STATES = [ :missing, :canceled, :failed ]13 10 14 11 def self.[](code) … … 28 25 end 29 26 30 MESSAGES.keys.each { |state| define_method("#{state}?") { @code == state } } 27 ERROR_MESSAGES.keys.each { |state| define_method("#{state}?") { @code == state } } 28 29 def successful? 30 @code == :successful 31 end 31 32 32 33 def unsuccessful? 33 ERROR_ STATES.include?(@code)34 ERROR_MESSAGES.keys.include?(@code) 34 35 end 35 36 36 37 def message 37 MESSAGES[@code]38 ERROR_MESSAGES[@code] 38 39 end 39 40 end … … 41 42 42 43 protected 43 # OpenIDs are expected to begin with http:// or https:// 44 def open_id?(user_name) #:doc: 45 (Object.const_defined?(:OpenID) && user_name =~ /^https?:\/\//i) || params[:open_id_complete] 44 def normalize_url(url) 45 url = url.downcase 46 47 case url 48 when %r{^https?://[^/]+/[^/]*} 49 url # already normalized 50 when %r{^https?://[^/]+$} 51 url + "/" 52 when %r{^[.\d\w]+/.*$} 53 "http://" + url 54 when %r{^[.\d\w]+$} 55 "http://" + url + "/" 56 else 57 raise "Unable to normalize: #{url}" 58 end 46 59 end 47 60 48 def authenticate_with_open_id(identity_url, fields = {}, &block) #:doc: 61 # The parameter name of "openid_url" is used rather than the Rails convention "open_id_url" 62 # because that's what the specification dictates in order to get browser auto-complete working across sites 63 def using_open_id?(identity_url = params[:openid_url]) #:doc: 64 !identity_url.blank? || params[:open_id_complete] 65 end 66 67 def authenticate_with_open_id(identity_url = params[:openid_url], fields = {}, &block) #:doc: 49 68 if params[:open_id_complete].nil? 50 begin_open_id_authentication( identity_url, fields, &block)69 begin_open_id_authentication(normalize_url(identity_url), fields, &block) 51 70 else 52 71 complete_open_id_authentication(&block) 53 72 end 54 73 end 74 55 75 56 76 private … … 69 89 def complete_open_id_authentication 70 90 open_id_response = timeout_protection_from_identity_server { open_id_consumer.complete(params) } 91 identity_url = normalize_url(open_id_response.identity_url) 71 92 72 93 case open_id_response.status 73 94 when OpenID::CANCEL 74 yield Result[:canceled], open_id_response.identity_url, nil95 yield Result[:canceled], identity_url, nil 75 96 when OpenID::FAILURE 76 97 logger.info "OpenID authentication failed: #{open_id_response.msg}" 77 yield Result[:failed], open_id_response.identity_url, nil98 yield Result[:failed], identity_url, nil 78 99 when OpenID::SUCCESS 79 yield Result[:successful], open_id_response.identity_url, open_id_response.extension_response('sreg')100 yield Result[:successful], identity_url, open_id_response.extension_response('sreg') 80 101 end 81 102 end … … 98 119 end 99 120 100 101 121 def timeout_protection_from_identity_server 102 122 yield plugins/open_id_authentication/README
r6324 r6331 8 8 To understand what OpenID is about and how it works, it helps to read the documentation for lib/openid/consumer.rb 9 9 from that gem. 10 11 The specification used is http://openid.net/specs/openid-authentication-1_1.html (not the 2.0 draft). 10 12 11 13 … … 36 38 37 39 40 app/views/sessions/new.erb 41 42 <% form_tag(session_url) do %> 43 <p> 44 <label for="name">Username:</label> 45 <%= text_field_tag "name" %> 46 </p> 47 48 <p> 49 <label for="password">Password:</label> 50 <%= password_field_tag %> 51 </p> 52 53 <p> 54 ...or use: 55 </p> 56 57 <p> 58 <label for="openid_url">OpenID:</label> 59 <%= text_field_tag "openid_url" %> 60 </p> 61 62 <p> 63 <%= submit_tag 'Sign in', :disable_with => "Signing in…" %> 64 </p> 65 <% end %> 66 38 67 app/controllers/session_controller.rb 39 68 class SessionController < ApplicationController 40 69 def create 41 if open_id?(params[:name])42 open_id_authentication (params[:name])70 if using_open_id? 71 open_id_authentication 43 72 else 44 73 password_authentication(params[:name], params[:password]) … … 56 85 end 57 86 58 def open_id_authentication (identity_url)59 authenticate_with_open_id (identity_url)do |result, identity_url|87 def open_id_authentication 88 authenticate_with_open_id do |result, identity_url| 60 89 case result 61 90 when :missing … … 69 98 successful_login 70 99 else 71 failed_login "Sorry, no user by that identity URL exists "100 failed_login "Sorry, no user by that identity URL exists (#{identity_url})") 72 101 end 73 102 end … … 93 122 you can collapse the case into a mere boolean: 94 123 95 def open_id_authentication(identity_url) 96 authenticate_with_open_id(identity_url) do |result, identity_url| 97 if result.successful? 98 if @current_user = @account.users.find_by_identity_url(identity_url) 99 successful_login 100 else 101 failed_login "Sorry, no user by that identity URL exists" 102 end 124 def open_id_authentication 125 authenticate_with_open_id do |result, identity_url| 126 if result.successful? && @current_user = @account.users.find_by_identity_url(identity_url) 127 successful_login 103 128 else 104 failed_login(result.message )129 failed_login(result.message || "Sorry, no user by that identity URL exists (#{identity_url})") 105 130 end 106 131 end