Skip to content

Instantly share code, notes, and snippets.

@sbfaulkner
Created July 7, 2015 13:27
Show Gist options
  • Save sbfaulkner/4b8ba460f267443bfa32 to your computer and use it in GitHub Desktop.
Save sbfaulkner/4b8ba460f267443bfa32 to your computer and use it in GitHub Desktop.
cas authentication
require 'rubygems'
require 'faraday'
require 'faraday_middleware'
cas = Faraday.new(:url => 'http://cas.dev') do |faraday|
faraday.request :url_encoded
faraday.response :logger
faraday.response :xml, :content_type => /\bxml$/
faraday.adapter Faraday.default_adapter
end
response = cas.post '/loginTicket'
if !response.success?
STDERR.puts "ERROR: unable to get login ticket (#{response.status})"
exit
end
lt = response.body
response = cas.post '/login', :username => ARGV[0], :password => ARGV[1], :lt => lt, :service => 'foo'
if response.status != 303
STDERR.puts "ERROR: login failed (#{response.status})"
exit
end
location = response.headers['location']
query = URI.parse(location).query
params = CGI.parse(query)
ticket = params['ticket'].first
response = cas.get '/serviceValidate', :service => 'foo', :ticket => ticket
if !response.success?
STDERR.puts "ERROR: login failed - unable to validate ticket (#{response.status})"
exit
end
response = response.body['serviceResponse']
if response.include?('authenticationFailure')
STDERR.puts "ERROR: authentication failed - #{response['authenticationFailure']}"
else
response['authenticationSuccess'].each do |key,value|
STDERR.puts "#{key}=#{YAML.load(value).inspect}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment