lib/rmobio/cas.rb in rmobio-1.1.3 vs lib/rmobio/cas.rb in rmobio-1.1.4

- old
+ new

@@ -31,39 +31,20 @@ # require 'rubycas-client' require 'casclient/frameworks/rails/filter' require 'casclient' +require 'rmobio/utils' module Rmobio module Cas - class Client < CASClient::Client - attr_accessor :xml_response - - # Override service ticket validation so we use our XmlResponse - def validate_service_ticket(st) - RAILS_DEFAULT_LOGGER.debug 'CAS: Starting to validate service ticket...' unless not defined? RAILS_DEFAULT_LOGGER - uri = URI.parse(validate_url) - h = uri.query ? query_to_hash(uri.query) : {} - h['service'] = st.service - h['ticket'] = st.ticket - h['renew'] = 1 if st.renew - h['pgtUrl'] = proxy_callback_url if proxy_callback_url - uri.query = hash_to_query(h) - - st.response = request_cas_response(uri, MobioValidationResponse) - @xml_response = st.response - return st - end - end - - class MobioValidationResponse < CASClient::ValidationResponse + class MobioValidationResponse < CASClient::ValidationResponse attr_reader :uuid # Parse out our custom attributes def initialize(raw_text) - super(raw_text) + parse(raw_text) parse_uuid(raw_text) end def parse_uuid(raw_text) raise BadResponseException, @@ -79,10 +60,69 @@ # 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#{@xml.inspect}" end end + end + + class Client < CASClient::Client + attr_accessor :xml_response + + # Override service ticket validation so we use our XmlResponse + def validate_service_ticket(st) + RAILS_DEFAULT_LOGGER.debug 'CAS: Starting to validate service ticket...' unless not defined? RAILS_DEFAULT_LOGGER + uri = URI.parse(validate_url) + h = uri.query ? query_to_hash(uri.query) : {} + h['service'] = st.service + h['ticket'] = st.ticket + h['renew'] = 1 if st.renew + h['pgtUrl'] = proxy_callback_url if proxy_callback_url + + # Add our domain parameter + h['domain'] = Rmobio::Utils.get_domain(st.service) + uri.query = hash_to_query(h) + + # Override the validation response + st.response = request_cas_response(uri, Rmobio::Cas::MobioValidationResponse) + @xml_response = st.response + return st + end + + # We have to override this method because MobioValidationResponse is + # uninitialized in the base class + def request_cas_response(uri, type) + log.debug "Requesting CAS response form URI #{uri.inspect}" + + uri = URI.parse(uri) unless uri.kind_of? URI + https = Net::HTTP.new(uri.host, uri.port) + https.use_ssl = (uri.scheme == 'https') + raw_res = https.start do |conn| + conn.get("#{uri.path}?#{uri.query}") + end + + # TODO: check to make sure that response code is 200 and handle errors + # otherwise + + RAILS_DEFAULT_LOGGER.debug "CAS Responded with " + + "#{raw_res.inspect}:\n#{raw_res.body}" unless not defined? RAILS_DEFAULT_LOGGER + + type.new(raw_res.body) + end + + # Override to add the domain param + def add_service_to_login_url(service_url) + uri = super(service_url) + domain = Rmobio::Utils.get_domain(service_url) + + if not domain.nil? + RAILS_DEFAULT_LOGGER.debug 'CAS: Adding domain parameter ' + + domain + '...' unless not defined? RAILS_DEFAULT_LOGGER + param_token = uri.index("?").nil? ? '?' : '&' + uri << param_token + 'domain=' + domain + end + uri.to_s + end end class MobioCasFilter < CASClient::Frameworks::Rails::Filter # Override configure so we use our cas client @@ -90,17 +130,17 @@ @@config = config @@config[:logger] = RAILS_DEFAULT_LOGGER unless @@config[:logger] @@client = Rmobio::Cas::Client.new(config) @@log = client.log end - + + # Here's where we override the filter def self.filter(controller) - RAILS_DEFAULT_LOGGER.debug 'CAS: Starting filter...' unless not defined? RAILS_DEFAULT_LOGGER # Call filter on the base class - CASClient::Frameworks::Rails::Filter.filter(controller) + CASClient::Frameworks::Rails::Filter.filter(controller) @handset_id = controller.params[:handsetid] # Use the overloaded cas client to retrieve uuid. This should only # happen after service ticket validation. @@ -121,7 +161,10 @@ 'attributes for the user!' unless not defined? RAILS_DEFAULT_LOGGER return false end end end + class BadResponseException < Exception + end end -end +end +include Rmobio::Cas \ No newline at end of file