0
+require 'openid/extensions/sreg'
0
+require 'openid/store/filesystem'
0
module OpenIdAuthentication
0
OPEN_ID_AUTHENTICATION_DIR = RAILS_ROOT + "/tmp/openids"
0
OpenIdAuthentication.store
0
@@ -20,19 +24,20 @@ module OpenIdAuthentication
0
- :missing => "Sorry, the OpenID server couldn't be found",
0
- :canceled => "OpenID verification was canceled",
0
- :failed => "Sorry, the OpenID verification failed"
0
+ :missing => "Sorry, the OpenID server couldn't be found",
0
+ :canceled => "OpenID verification was canceled",
0
+ :failed => "Sorry, the OpenID verification failed",
0
+ :setup_needed => "OpenID verification needs setup"
0
if code == :unsuccessful && unsuccessful?
0
@@ -40,7 +45,7 @@ module OpenIdAuthentication
0
ERROR_MESSAGES.keys.each { |state| define_method("#{state}?") { @code == state } }
0
@@ -50,24 +55,21 @@ module OpenIdAuthentication
0
ERROR_MESSAGES.keys.include?(@code)
0
def self.normalize_url(url)
0
- uri = URI.parse("http://#{uri}") unless uri.scheme
0
- uri.scheme = uri.scheme.downcase # URI should do this
0
- rescue URI::InvalidURIError
0
- raise InvalidOpenId.new("#{url} is not an OpenID URL")
0
+ uri = URI.parse(url.to_s.strip)
0
+ uri = URI.parse("http://#{uri}") unless uri.scheme
0
+ uri.scheme = uri.scheme.downcase # URI should do this
0
+ rescue URI::InvalidURIError
0
+ raise InvalidOpenId.new("#{url} is not an OpenID URL")
0
OpenIdAuthentication.normalize_url(url)
0
@@ -87,59 +89,63 @@ module OpenIdAuthentication
0
def begin_open_id_authentication(identity_url, fields = {})
0
- open_id_response = timeout_protection_from_identity_server { open_id_consumer.begin(identity_url) }
0
- case open_id_response.status
0
- yield Result[:missing], identity_url, nil
0
- add_simple_registration_fields(open_id_response, fields)
0
- redirect_to(open_id_redirect_url(open_id_response))
0
+ open_id_request = open_id_consumer.begin(identity_url)
0
+ add_simple_registration_fields(open_id_request, fields)
0
+ redirect_to(open_id_redirect_url(open_id_request))
0
+ rescue OpenID::OpenIDError, Timeout::Error => e
0
+ logger.error("[OPENID] #{e}")
0
+ yield Result[:missing], identity_url, nil
0
def complete_open_id_authentication
0
- open_id_response = timeout_protection_from_identity_server { open_id_consumer.complete(params) }
0
- identity_url = normalize_url(open_id_response.identity_url) if open_id_response.identity_url
0
+ params_with_path = params.reject { |key, value| request.path_parameters[key] }
0
+ open_id_response = timeout_protection_from_identity_server { open_id_consumer.complete(params_with_path, requested_url) }
0
+ identity_url = normalize_url(open_id_response.endpoint.claimed_id) if open_id_response.endpoint.claimed_id
0
case open_id_response.status
0
+ when OpenID::Consumer::SUCCESS
0
+ yield Result[:successful], identity_url, OpenID::SReg::Response.from_success_response(open_id_response)
0
+ when OpenID::Consumer::CANCEL
0
yield Result[:canceled], identity_url, nil
0
- logger.info "OpenID authentication failed: #{open_id_response.msg}"
0
+ when OpenID::Consumer::FAILURE
0
yield Result[:failed], identity_url, nil
0
- yield Result[:successful], identity_url, open_id_response.extension_response('sreg')
0
+ when OpenID::Consumer::SETUP_NEEDED
0
+ yield Result[:setup_needed], open_id_response.setup_url, nil
0
OpenID::Consumer.new(session, open_id_store)
0
- when :db : OpenIdAuthentication::DbStore.new
0
- when :file: OpenID::FilesystemStore.new(OPEN_ID_AUTHENTICATION_DIR)
0
+ OpenIdAuthentication::DbStore.new
0
+ OpenID::FilesystemStore.new(OPEN_ID_AUTHENTICATION_DIR)
0
raise "Unknown store: #{store}"
0
+ def add_simple_registration_fields(open_id_request, fields)
0
+ sreg_request = OpenID::SReg::Request.new
0
+ sreg_request.request_fields(Array(fields[:required]).map(&:to_s), true) if fields[:required]
0
+ sreg_request.request_fields(Array(fields[:optional]).map(&:to_s), false) if fields[:optional]
0
+ sreg_request.policy_url = fields[:policy_url] if fields[:policy_url]
0
+ open_id_request.add_extension(sreg_request)
0
- def add_simple_registration_fields(open_id_response, fields)
0
- open_id_response.add_extension_arg('sreg', 'required', [ fields[:required] ].flatten * ',') if fields[:required]
0
- open_id_response.add_extension_arg('sreg', 'optional', [ fields[:optional] ].flatten * ',') if fields[:optional]
0
+ def open_id_redirect_url(open_id_request)
0
+ open_id_request.return_to_args['open_id_complete'] = '1'
0
+ open_id_request.redirect_url(root_url, requested_url)
0
- def open_id_redirect_url(open_id_response)
0
- open_id_response.redirect_url(
0
- request.protocol + request.host_with_port + "/",
0
- open_id_response.return_to("#{request.protocol + request.host_with_port + request.relative_url_root + request.path}?open_id_complete=1")
0
+ "#{request.protocol + request.host_with_port + request.relative_url_root + request.path}"
0
def timeout_protection_from_identity_server
0
@@ -149,10 +155,10 @@ module OpenIdAuthentication
0
"Identity server timed out"
0
\ No newline at end of file
Comments
No one has commented yet.