lib/casclient/responses.rb in rubycas-client-2.2.1 vs lib/casclient/responses.rb in rubycas-client-2.3.0.rc1

- old
+ new

@@ -29,19 +29,18 @@ class ValidationResponse include XmlResponse attr_reader :protocol, :user, :pgt_iou, :proxies, :extra_attributes - def initialize(raw_text) - parse(raw_text) + def initialize(raw_text, options={}) + parse(raw_text, options) end - def parse(raw_text) + def parse(raw_text, options) raise BadResponseException, "CAS response is empty/blank." if raw_text.blank? @parse_datetime = Time.now - if raw_text =~ /^(yes|no)\n(.*?)\n$/m @protocol = 1.0 @valid = $~[1] == 'yes' @user = $~[2] return @@ -51,11 +50,12 @@ # if we got this far then we've got a valid XML response, so we're doing CAS 2.0 @protocol = 2.0 if is_success? - @user = @xml.elements["cas:user"].text.strip if @xml.elements["cas:user"] + cas_user = @xml.elements["cas:user"] + @user = cas_user.text.strip if cas_user @pgt_iou = @xml.elements["cas:proxyGrantingTicket"].text.strip if @xml.elements["cas:proxyGrantingTicket"] proxy_els = @xml.elements.to_a('//cas:authenticationSuccess/cas:proxies/cas:proxy') if proxy_els.size > 0 @proxies = [] @@ -63,32 +63,47 @@ @proxies << el.text end end @extra_attributes = {} - @xml.elements.to_a('//cas:authenticationSuccess/*').each do |el| - @extra_attributes.merge!(Hash.from_xml(el.to_s)) unless el.prefix == 'cas' + @xml.elements.to_a('//cas:authenticationSuccess/cas:attributes/* | //cas:authenticationSuccess/*[local-name() != \'proxies\' and local-name() != \'proxyGrantingTicket\' and local-name() != \'user\' and local-name() != \'attributes\']').each do |el| + # generating the hash requires prefixes to be defined, so add all of the namespaces + el.namespaces.each {|k,v| el.add_namespace(k,v)} + @extra_attributes.merge!(Hash.from_xml(el.to_s)) end # unserialize extra attributes @extra_attributes.each do |k, v| if v.blank? @extra_attributes[k] = nil - else - @extra_attributes[k] = YAML.load(v) + elsif !options[:encode_extra_attributes_as] + begin + @extra_attributes[k] = YAML.load(v) + rescue ArgumentError + raise ArgumentError, "Did not find :encode_extra_attributes_as config parameter, hence default encoding scheme is YAML but CAS response recieved in encoded differently " + end + else + if options[:encode_extra_attributes_as] == :json + begin + @extra_attributes[k] = JSON.parse(v) + rescue JSON::ParserError + @extra_attributes[k] = YAML.load(v) + end + else + @extra_attributes[k] = YAML.load(v) + end end end elsif is_failure? @failure_code = @xml.elements['//cas:authenticationFailure'].attributes['code'] @failure_message = @xml.elements['//cas:authenticationFailure'].text.strip else # this should never happen, since the response should already have been recognized as invalid raise BadResponseException, "BAD CAS RESPONSE:\n#{raw_text.inspect}\n\nXML DOC:\n#{doc.inspect}" end - end - + def is_success? (instance_variable_defined?(:@valid) && @valid) || (protocol > 1.0 && xml.name == "authenticationSuccess") end def is_failure? @@ -101,11 +116,11 @@ class ProxyResponse include XmlResponse attr_reader :proxy_ticket - def initialize(raw_text) + def initialize(raw_text, options={}) parse(raw_text) end def parse(raw_text) raise BadResponseException, @@ -139,11 +154,11 @@ # (i.e. after submitting a username/password). class LoginResponse attr_reader :tgt, :ticket, :service_redirect_url attr_reader :failure_message - def initialize(http_response = nil) + def initialize(http_response = nil, options={}) parse_http_response(http_response) if http_response end def parse_http_response(http_response) header = http_response.to_hash @@ -158,12 +173,10 @@ location = header['location'].first if header['location'] && header['location'].first if location =~ /ticket=([^&]+)/ @ticket = $~[1] end - if (http_response.kind_of?(Net::HTTPSuccess) || http_response.kind_of?(Net::HTTPFound)) && @ticket.present? - log.info("Login was successful for ticket: #{@ticket.inspect}.") - else + if not ((http_response.kind_of?(Net::HTTPSuccess) || http_response.kind_of?(Net::HTTPFound)) && @ticket.present?) @failure = true # Try to extract the error message -- this only works with RubyCAS-Server. # For other servers we just return the entire response body (i.e. the whole error page). body = http_response.body if body =~ /<div class="messagebox mistake">(.*?)<\/div>/m