Changeset 6324
- Timestamp:
- 03/04/07 22:02:36 (2 years ago)
- Files:
-
- plugins/open_id_authentication/CHANGELOG (modified) (1 diff)
- plugins/open_id_authentication/lib/open_id_authentication.rb (modified) (4 diffs)
- plugins/open_id_authentication/README (modified) (2 diffs)
- plugins/open_id_authentication/test/open_id_authentication_test.rb (modified) (1 diff)
- plugins/open_id_authentication/test/status_test.rb (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/open_id_authentication/CHANGELOG
r6318 r6324 1 * 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 1 3 * Stop relying on root_url being defined, we can just grab the current url instead [DHH] plugins/open_id_authentication/lib/open_id_authentication.rb
r6318 r6324 1 1 module OpenIdAuthentication 2 2 OPEN_ID_AUTHENTICATION_DIR = RAILS_ROOT + "/tmp/openids" 3 4 class Result 5 MESSAGES = { 6 :missing => "Sorry, the OpenID server couldn't be found", 7 :canceled => "OpenID verification was canceled", 8 :failed => "Sorry, the OpenID verification failed", 9 :successful => "OpenID authentication successful" 10 } 11 12 ERROR_STATES = [ :missing, :canceled, :failed ] 13 14 def self.[](code) 15 new(code) 16 end 17 18 def initialize(code) 19 @code = code 20 end 21 22 def ===(code) 23 if code == :unsuccessful && unsuccessful? 24 true 25 else 26 @code == code 27 end 28 end 29 30 MESSAGES.keys.each { |state| define_method("#{state}?") { @code == state } } 31 32 def unsuccessful? 33 ERROR_STATES.include?(@code) 34 end 35 36 def message 37 MESSAGES[@code] 38 end 39 end 40 3 41 4 42 protected … … 18 56 private 19 57 def begin_open_id_authentication(identity_url, fields = {}) 20 open_id_response = open_id_consumer.begin(identity_url)58 open_id_response = timeout_protection_from_identity_server { open_id_consumer.begin(identity_url) } 21 59 22 60 case open_id_response.status 23 61 when OpenID::FAILURE 24 yield :missing, identity_url, nil62 yield Result[:missing], identity_url, nil 25 63 when OpenID::SUCCESS 26 64 add_simple_registration_fields(open_id_response, fields) … … 30 68 31 69 def complete_open_id_authentication 32 open_id_response = open_id_consumer.complete(params)70 open_id_response = timeout_protection_from_identity_server { open_id_consumer.complete(params) } 33 71 34 72 case open_id_response.status 35 73 when OpenID::CANCEL 36 yield :canceled, open_id_response.identity_url, nil74 yield Result[:canceled], open_id_response.identity_url, nil 37 75 when OpenID::FAILURE 38 76 logger.info "OpenID authentication failed: #{open_id_response.msg}" 39 yield :failed, open_id_response.identity_url, nil77 yield Result[:failed], open_id_response.identity_url, nil 40 78 when OpenID::SUCCESS 41 yield :successful, open_id_response.identity_url, open_id_response.extension_response('sreg')79 yield Result[:successful], open_id_response.identity_url, open_id_response.extension_response('sreg') 42 80 end 43 81 end … … 59 97 ) 60 98 end 99 100 101 def timeout_protection_from_identity_server 102 yield 103 rescue Timeout::Error 104 Class.new do 105 def status 106 OpenID::FAILURE 107 end 108 109 def msg 110 "Identity server timed out" 111 end 112 end.new 113 end 61 114 end plugins/open_id_authentication/README
r6318 r6324 57 57 58 58 def open_id_authentication(identity_url) 59 authenticate_with_open_id(identity_url) do | status, identity_url|60 case status59 authenticate_with_open_id(identity_url) do |result, identity_url| 60 case result 61 61 when :missing 62 62 failed_login "Sorry, the OpenID server couldn't be found" … … 87 87 end 88 88 end 89 90 91 92 If you're fine with the result messages above and don't need individual logic on a per-failure basis, 93 you can collapse the case into a mere boolean: 94 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 103 else 104 failed_login(result.message) 105 end 106 end 107 end 89 108 90 109 plugins/open_id_authentication/test/open_id_authentication_test.rb
r6245 r6324 1 1 require 'test/unit' 2 2 3 require 'rubygems' 4 gem 'mocha' 5 require 'mocha' 6 7 gem 'ruby-openid' 8 require 'openid' 9 10 RAILS_ROOT = File.dirname(__FILE__) 11 require File.dirname(__FILE__) + "/../lib/open_id_authentication" 12 3 13 class OpenIdAuthenticationTest < Test::Unit::TestCase 4 # Replace this with your real tests. 5 def test_this_plugin 6 flunk 14 def setup 15 @controller = Class.new do 16 include OpenIdAuthentication 17 def params() {} end 18 end.new 19 end 20 21 def test_authentication_should_fail_when_the_identity_server_is_missing 22 @controller.stubs(:open_id_consumer).returns(stub(:begin => stub(:status => OpenID::FAILURE))) 23 24 @controller.send(:authenticate_with_open_id, "http://someone.example.com") do |result, identity_url| 25 assert result.missing? 26 assert_equal "Sorry, the OpenID server couldn't be found", result.message 27 end 28 end 29 30 def test_authentication_should_fail_when_the_identity_server_times_out 31 @controller.stubs(:open_id_consumer).returns(stub(:begin => Proc.new { raise Timeout::Error })) 32 33 @controller.send(:authenticate_with_open_id, "http://someone.example.com") do |result, identity_url| 34 assert result.missing? 35 assert_equal "Sorry, the OpenID server couldn't be found", result.message 36 end 37 end 38 39 def test_authentication_should_begin_when_the_identity_server_is_present 40 @controller.stubs(:open_id_consumer).returns(stub(:begin => stub(:status => OpenID::SUCCESS))) 41 @controller.expects(:begin_open_id_authentication) 42 @controller.send(:authenticate_with_open_id, "http://someone.example.com") 7 43 end 8 44 end