Sha256: 5f2cb3e4b0ee8b133fc5b9a05300a3819121819e6a99ff1fd5c23e4ec1241a8f

Contents?: true

Size: 1.67 KB

Versions: 8

Compression:

Stored size: 1.67 KB

Contents

require 'nokogiri'

module CasrackTheAuthenticator
  
  class ServiceTicketValidator
    
    VALIDATION_REQUEST_HEADERS = { 'Accept' => '*/*' }
    
    # Build a validator from a +configuration+, a
    # +return_to+ URL, and a +ticket+.
    #
    # @param [CasrackTheAuthenticator::Configuration] configuration the CAS configuration
    # @param [String] return_to_url the URL of this CAS client service
    # @param [String] ticket the service ticket to validate
    def initialize(configuration, return_to_url, ticket)
      @uri = URI.parse(configuration.service_validate_url(return_to_url, ticket))
    end
    
    # Request validation of the ticket from the CAS server's
    # serviceValidate (CAS 2.0) function.
    #
    # Swallows all XML parsing errors (and returns +nil+ in those cases).
    #
    # @return [String, nil] a username if the response is valid; +nil+ otherwise.
    #
    # @raise any connection errors encountered.
    def user
      parse_user(get_validation_response_body)
    end
    
    private
    
    def get_validation_response_body
      result = ''
      Net::HTTP.new(@uri.host, @uri.port).start do |c|
        response = c.get "#{@uri.path}?#{@uri.query}", VALIDATION_REQUEST_HEADERS
        result = response.body
      end
      result
    end
    
    def parse_user(body)
      begin
        doc = Nokogiri::XML(body)
        node   = doc.xpath('/cas:serviceResponse/cas:authenticationSuccess/cas:user').first
        node ||= doc.xpath('/serviceResponse/authenticationSuccess/user').first  # try w/o the namespace just in case
        node.nil? ? nil : node.content
      rescue Nokogiri::XML::XPath::SyntaxError
        nil
      end
    end
    
  end
  
end

Version data entries

8 entries across 8 versions & 4 rubygems

Version Path
gcnovus-casrack_the_authenticator-1.6.0 lib/casrack_the_authenticator/service_ticket_validator.rb
factorylabs-casrack_the_authenticator-1.6.0 lib/casrack_the_authenticator/service_ticket_validator.rb
akamai_bookmarklet-0.1.2 vendor/gems/ruby/1.8/gems/casrack_the_authenticator-1.6.0/lib/casrack_the_authenticator/service_ticket_validator.rb
akamai_bookmarklet-0.1.1 vendor/gems/ruby/1.8/gems/casrack_the_authenticator-1.6.0/lib/casrack_the_authenticator/service_ticket_validator.rb
akamai_bookmarklet-0.1.0 vendor/gems/ruby/1.8/gems/casrack_the_authenticator-1.6.0/lib/casrack_the_authenticator/service_ticket_validator.rb
casrack_the_authenticator-1.6.0 lib/casrack_the_authenticator/service_ticket_validator.rb
casrack_the_authenticator-1.5.0 lib/casrack_the_authenticator/service_ticket_validator.rb
casrack_the_authenticator-1.4.0 lib/casrack_the_authenticator/service_ticket_validator.rb