Class: CASClient::ValidationResponse

Inherits:
Object
  • Object
show all
Includes:
XmlResponse
Defined in:
lib/casclient/responses.rb

Overview

Represents a response from the CAS server to a ‘validate’ request (i.e. after validating a service/proxy ticket).

Instance Attribute Summary collapse

Attributes included from XmlResponse

#failure_code, #failure_message, #parse_datetime, #xml

Instance Method Summary collapse

Methods included from XmlResponse

#check_and_parse_xml, #to_s

Constructor Details

#initialize(raw_text, options = {}) ⇒ ValidationResponse

Returns a new instance of ValidationResponse.



34
35
36
# File 'lib/casclient/responses.rb', line 34

def initialize(raw_text, options={})
  parse(raw_text, options)
end

Instance Attribute Details

#extra_attributesObject (readonly)

Returns the value of attribute extra_attributes.



32
33
34
# File 'lib/casclient/responses.rb', line 32

def extra_attributes
  @extra_attributes
end

#pgt_iouObject (readonly)

Returns the value of attribute pgt_iou.



32
33
34
# File 'lib/casclient/responses.rb', line 32

def pgt_iou
  @pgt_iou
end

#protocolObject (readonly)

Returns the value of attribute protocol.



32
33
34
# File 'lib/casclient/responses.rb', line 32

def protocol
  @protocol
end

#proxiesObject (readonly)

Returns the value of attribute proxies.



32
33
34
# File 'lib/casclient/responses.rb', line 32

def proxies
  @proxies
end

#userObject (readonly)

Returns the value of attribute user.



32
33
34
# File 'lib/casclient/responses.rb', line 32

def user
  @user
end

Instance Method Details

#is_failure?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/casclient/responses.rb', line 108

def is_failure?
  (instance_variable_defined?(:@valid) && !@valid) || (protocol > 1.0 && xml.name == "authenticationFailure" )
end

#is_success?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/casclient/responses.rb', line 104

def is_success?
  (instance_variable_defined?(:@valid) &&  @valid) || (protocol > 1.0 && xml.name == "authenticationSuccess")
end

#parse(raw_text, options) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/casclient/responses.rb', line 38

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
  end
  
  @xml = check_and_parse_xml(raw_text)
  
  # 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?
    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 = []
      proxy_els.each do |el|
        @proxies << el.text
      end
    end
    
    @extra_attributes = {}
    @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|
      inner_text = el.cdatas.length > 0 ? el.cdatas.join('') : el.text
      @extra_attributes.merge! el.name => inner_text
    end
    
    # unserialize extra attributes
    @extra_attributes.each do |k, v|
      if v.blank?
        @extra_attributes[k] = nil
      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