Class: Rack::Casual::Client

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

Overview

This is a über simple CAS client responsible for validating a ticket.

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)


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

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       = nil
end

Instance Attribute Details

#extra_attributesObject

Returns the value of attribute extra_attributes.



10
11
12
# File 'lib/rack/casual/client.rb', line 10

def extra_attributes
  @extra_attributes
end

#usernameObject

Returns the value of attribute username.



10
11
12
# File 'lib/rack/casual/client.rb', line 10

def username
  @username
end

Class Method Details

.login_url(service_url) ⇒ Object

Returns login url as string



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

def self.(service_url)
  new(service_url)..to_s
end

Instance Method Details

#cas_url(action = nil, options = nil) ⇒ 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


115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rack/casual/client.rb', line 115

def cas_url(action=nil, options = nil)
  url = Rack::Casual.cas_url.sub(/\/+$/, '')
  
  url << case action
  when :login    then "/login"
  when :validate then "/serviceValidate"
  else
    action.to_s
  end
  
  url += "?service=#{@service_url}"
  url += "&ticket=#{@ticket}" if @ticket
  URI.parse(url)
end

#find_attributes(xml) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/rack/casual/client.rb', line 73

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

#find_username(xml) ⇒ Object



69
70
71
# File 'lib/rack/casual/client.rb', line 69

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



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

def 
  cas_url(:login)
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>



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rack/casual/client.rb', line 55

def validate_ticket
  url = validation_url
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = (url.scheme == "https") 
  
  result = Nokogiri.parse(http.get(url.request_uri).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



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

def validation_url
  cas_url(:validate)
end