Created
July 7, 2015 13:27
-
-
Save sbfaulkner/4b8ba460f267443bfa32 to your computer and use it in GitHub Desktop.
cas authentication
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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