Skip to content

Instantly share code, notes, and snippets.

@adamcrown
Created August 21, 2014 16:02
Show Gist options
  • Save adamcrown/a7e757759469033584c4 to your computer and use it in GitHub Desktop.
Save adamcrown/a7e757759469033584c4 to your computer and use it in GitHub Desktop.
Example Sinatra rack-cas app
# Run with command:
# CAS_SERVER="http://example.com/login" rackup
require 'sinatra/base'
require 'rack/cas'
require 'yaml'
class SinatraRackCASExample < Sinatra::Base
before do
unless session['cas'] && session['cas']['user']
halt 401, 'Unauthorized'
end
end
get '/' do
"Hi #{session['cas']['user']}. You're logged in."
end
end
use Rack::Session::Cookie, key: 'rack.session', path: '/', secret: 'ca1257a7e2ede894900c5a4fb4a9abb40bb4fcbfb08d31bd67522363fba1ff925b5c5101ca9fc69d45b381a3c15ac5e1a854e86de23c789e6b99765cdb3a2451'
use Rack::CAS, server_url: ENV['CAS_SERVER']
run SinatraRackCASExample
@adamcrown
Copy link
Author

If you ran it with CAS_SERVER="https://my.server.URL" rackup -p 4567, then yes. It's trying to redirect you to what it thinks is your CAS server for authentication before redirect you back with a service ticket that it then uses to validate the authenticity of your token and grad your username and other info.

@Nautigsam
Copy link

I'm sorry, I have to be dumb, but when I launch this code, Firefox telling me this error:

The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
This problem can sometimes be caused by disabling or refusing to accept cookies.

I think I tried all I could, but I don't get it.

@Nautigsam
Copy link

Oh, my apologizes, the fact is the server I tried to connect to was my university's. And I just discovered that it didn't accept requests from outside the internal network. That's why it didn't work from my computer. Thanks a lot for your help, though.

@jostockley
Copy link

I'm trying this with rack-cas 0.10.1, ruby 1.9.3p551 and I get the following error:

$ CAS_SERVER="https://mycas.example.com/cas" rackup
/home/stockj3/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/rack-cas-0.10.1/lib/rack/cas.rb:12:in `initialize': undefined method `config' for RackCAS:Module (NoMethodError)
    from /home/stockj3/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/rack-1.6.0/lib/rack/builder.rb:86:in `new'
    from /home/stockj3/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/rack-1.6.0/lib/rack/builder.rb:86:in `block in use'
    from /home/stockj3/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/rack-1.6.0/lib/rack/builder.rb:147:in `[]'
    from /home/stockj3/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/rack-1.6.0/lib/rack/builder.rb:147:in `block in to_app'
    from /home/stockj3/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/rack-1.6.0/lib/rack/builder.rb:147:in `each'
    from /home/stockj3/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/rack-1.6.0/lib/rack/builder.rb:147:in `inject'
    from /home/stockj3/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/rack-1.6.0/lib/rack/builder.rb:147:in `to_app'
    from /home/stockj3/Projects/firestone-checker2/config.ru:in `<main>'
    from /home/stockj3/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/rack-1.6.0/lib/rack/builder.rb:49:in `eval'
    from /home/stockj3/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/rack-1.6.0/lib/rack/builder.rb:49:in `new_from_string'
    from /home/stockj3/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/rack-1.6.0/lib/rack/builder.rb:40:in `parse_file'
    from /home/stockj3/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/rack-1.6.0/lib/rack/server.rb:299:in `build_app_and_options_from_config'
    from /home/stockj3/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/rack-1.6.0/lib/rack/server.rb:208:in `app'
    from /home/stockj3/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/rack-1.6.0/lib/rack/server.rb:336:in `wrapped_app'
    from /home/stockj3/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/rack-1.6.0/lib/rack/server.rb:272:in `start'
    from /home/stockj3/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/rack-1.6.0/lib/rack/server.rb:147:in `start'
    from /home/stockj3/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/rack-1.6.0/bin/rackup:4:in `<top (required)>'
    from /home/stockj3/.rbenv/versions/1.9.3-p551/bin/rackup:23:in `load'
    from /home/stockj3/.rbenv/versions/1.9.3-p551/bin/rackup:23:in `<main>'

@ankane
Copy link

ankane commented May 23, 2015

@jostockley Add require "rack-cas" before require "rack/cas" to fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment