Class: Rack::Casual::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/casual/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service_url, ticket = nil) ⇒ Client

Creates a new object

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
# File 'lib/rack/casual/client.rb', line 17

def initialize(service_url, ticket=nil)
  raise(ArgumentError, "Base URL must be configured") if Rack::Casual.cas_url.nil?

  @service_url  = service_url    
  @ticket       = ticket
  @result, @username, @extra_attributes = nil
end

Instance Attribute Details

#extra_attributesObject

Returns the value of attribute extra_attributes.



14
15
16
# File 'lib/rack/casual/client.rb', line 14

def extra_attributes
  @extra_attributes
end

#usernameObject

Returns the value of attribute username.



14
15
16
# File 'lib/rack/casual/client.rb', line 14

def username
  @username
end

Class Method Details

.cas_url(action = nil, options = {}) ⇒ Object

Returns a CAS url if action is :login or :validate, then the appropriate login and service-validation actions are used. Otherwise the argument is used as the first action.

Options is a hash that is appended to the url.

Return value is a URI object.

Examples:

cas_url :login                          # => http://localhost/login
cas_url :validate, :ticket => "T123"    # => http://localhost/serviceValidate?ticket=T123


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rack/casual/client.rb', line 107

def self.cas_url(action=nil, options = {})
  url = Rack::Casual.cas_url.sub(/\/+$/, '')
    
  url << case action
  when :login    then Rack::Casual.
  when :logout   then Rack::Casual.logout_url
  when :validate then Rack::Casual.validate_url
  else
    action.to_s
  end
    
  options = options.reject { |key,value| value.nil? }
  if options.any?
    url += "?" + options.map{|key,value| "#{key}=#{value}" }.join("&")
  end

  URI.parse(url)
end

.login_url(service_url) ⇒ Object

Helper that returns the CAS login url as string



27
28
29
# File 'lib/rack/casual/client.rb', line 27

def self.(service_url)
  cas_url(:login, :service => service_url).to_s
end

.logout_url(options = {}) ⇒ Object

Return url to CAS logout page



32
33
34
# File 'lib/rack/casual/client.rb', line 32

def self.logout_url(options={})
  cas_url(:logout, options).to_s
end

Instance Method Details

#find_attributes(xml) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/rack/casual/client.rb', line 84

def find_attributes(xml)
  @extra_attributes = {}
  xml.search("//cas:authenticationSuccess/*").each do |el|
    # puts " * Attribute #{el.name} = #{el.content.to_s}"
    value = YAML::parse(el.content).value.first.value rescue nil
    @extra_attributes[el.name] = value
  end
end

#find_username(xml) ⇒ Object



80
81
82
# File 'lib/rack/casual/client.rb', line 80

def find_username(xml)
  @username = xml.search("//cas:authenticationSuccess //cas:user").first.text rescue nil
end

#login_urlObject

Return the URL to the CAS login page



37
38
39
# File 'lib/rack/casual/client.rb', line 37

def 
  Client.cas_url(:login, :service => @service_url)
end

#validate_ticketObject

Validate the ticket we got from CAS

On ticket validation success: <cas:serviceResponse xmlns:cas=‘www.yale.edu/tp/cas’>

<cas:authenticationSuccess>
    <cas:user>username</cas:user>
        <cas:proxyGrantingTicket>PGTIOU-84678-8a9d...
    </cas:proxyGrantingTicket>
</cas:authenticationSuccess>

</cas:serviceResponse>

On ticket validation failure: <cas:serviceResponse xmlns:cas=‘www.yale.edu/tp/cas’>

<cas:authenticationFailure code="INVALID_TICKET">
    Ticket ST-1856339-aA5Yuvrxzpv8Tau1cYQ7 not recognized
</cas:authenticationFailure>

</cas:serviceResponse>



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rack/casual/client.rb', line 65

def validate_ticket
  url = validation_url
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = (url.scheme == "https") 
    
  body = http.get(url.request_uri).body
  result = Nokogiri.parse(body)

  # set username and extra attributes
  find_username(result)
  find_attributes(result) if @username

  !@username.nil?
end

#validation_urlObject

URL to the CAS ticket validation service



42
43
44
# File 'lib/rack/casual/client.rb', line 42

def validation_url
  Client.cas_url(:validate, :ticket => @ticket, :service => @service_url)
end