Class: Rack::Casual::Client
- Inherits:
-
Object
- Object
- Rack::Casual::Client
- Defined in:
- lib/rack/casual/client.rb
Instance Attribute Summary collapse
-
#extra_attributes ⇒ Object
Returns the value of attribute extra_attributes.
-
#username ⇒ Object
Returns the value of attribute username.
Class Method Summary collapse
-
.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.
-
.login_url(service_url) ⇒ Object
Helper that returns the CAS login url as string.
-
.logout_url(options = {}) ⇒ Object
Return url to CAS logout page.
Instance Method Summary collapse
- #find_attributes(xml) ⇒ Object
- #find_username(xml) ⇒ Object
-
#initialize(service_url, ticket = nil) ⇒ Client
constructor
Creates a new object.
-
#login_url ⇒ Object
Return the URL to the CAS login page.
-
#validate_ticket ⇒ Object
Validate the ticket we got from CAS.
-
#validation_url ⇒ Object
URL to the CAS ticket validation service.
Constructor Details
#initialize(service_url, ticket = nil) ⇒ Client
Creates a new object
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_attributes ⇒ Object
Returns the value of attribute extra_attributes.
14 15 16 |
# File 'lib/rack/casual/client.rb', line 14 def extra_attributes @extra_attributes end |
#username ⇒ Object
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, = {}) url = Rack::Casual.cas_url.sub(/\/+$/, '') url << case action when :login then Rack::Casual.login_url when :logout then Rack::Casual.logout_url when :validate then Rack::Casual.validate_url else action.to_s end = .reject { |key,value| value.nil? } if .any? url += "?" + .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.login_url(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(={}) cas_url(:logout, ).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_url ⇒ Object
Return the URL to the CAS login page
37 38 39 |
# File 'lib/rack/casual/client.rb', line 37 def login_url Client.cas_url(:login, :service => @service_url) end |
#validate_ticket ⇒ Object
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 |