Skip to content

Commit

Permalink
Adding collection for AX data as well as SReg data
Browse files Browse the repository at this point in the history
  • Loading branch information
peat authored and josh committed Jan 12, 2009
1 parent 88a14c7 commit eb3698d
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions lib/open_id_authentication.rb
@@ -1,5 +1,6 @@
require 'uri'
require 'openid/extensions/sreg'
require 'openid/extensions/ax'
require 'openid/store/filesystem'

require File.dirname(__FILE__) + '/open_id_authentication/db_store'
Expand Down Expand Up @@ -135,9 +136,13 @@ def begin_open_id_authentication(identity_url, options = {})
identity_url = normalize_identifier(identity_url)
return_to = options.delete(:return_to)
method = options.delete(:method)

options[:required] ||= [] # reduces validation later
options[:optional] ||= []

open_id_request = open_id_consumer.begin(identity_url)
add_simple_registration_fields(open_id_request, options)
add_ax_fields(open_id_request, options)
redirect_to(open_id_redirect_url(open_id_request, return_to, method))
rescue OpenIdAuthentication::InvalidOpenId => e
yield Result[:invalid], identity_url, nil
Expand All @@ -154,7 +159,16 @@ def complete_open_id_authentication

case open_id_response.status
when OpenID::Consumer::SUCCESS
yield Result[:successful], identity_url, OpenID::SReg::Response.from_success_response(open_id_response)
profile_data = {}

# merge the SReg data and the AX data into a single hash of profile data
[ OpenID::SReg::Response, OpenID::AX::FetchResponse ].each do |data_response|
if data_response.from_success_response( open_id_response )
profile_data.merge! data_response.from_success_response( open_id_response ).data
end
end

yield Result[:successful], identity_url, profile_data
when OpenID::Consumer::CANCEL
yield Result[:canceled], identity_url, nil
when OpenID::Consumer::FAILURE
Expand All @@ -170,12 +184,34 @@ def open_id_consumer

def add_simple_registration_fields(open_id_request, fields)
sreg_request = OpenID::SReg::Request.new
sreg_request.request_fields(Array(fields[:required]).map(&:to_s), true) if fields[:required]
sreg_request.request_fields(Array(fields[:optional]).map(&:to_s), false) if fields[:optional]

# filter out AX identifiers (URIs)
required_fields = fields[:required].collect { |f| f.to_s unless f =~ /^https?:\/\// }.compact
optional_fields = fields[:optional].collect { |f| f.to_s unless f =~ /^https?:\/\// }.compact

sreg_request.request_fields(required_fields, true) unless required_fields.blank?
sreg_request.request_fields(optional_fields, false) unless optional_fields.blank?
sreg_request.policy_url = fields[:policy_url] if fields[:policy_url]
open_id_request.add_extension(sreg_request)
end

def add_ax_fields( open_id_request, fields )
ax_request = OpenID::AX::FetchRequest.new

# look through the :required and :optional fields for URIs (AX identifiers)
fields[:required].each do |f|
next unless f =~ /^https?:\/\//
ax_request.add( OpenID::AX::AttrInfo.new( f, nil, true ) )
end

fields[:optional].each do |f|
next unless f =~ /^https?:\/\//
ax_request.add( OpenID::AX::AttrInfo.new( f, nil, false ) )
end

open_id_request.add_extension( ax_request )
end

def open_id_redirect_url(open_id_request, return_to = nil, method = nil)
open_id_request.return_to_args['_method'] = (method || request.method).to_s
open_id_request.return_to_args['open_id_complete'] = '1'
Expand Down

0 comments on commit eb3698d

Please sign in to comment.