Skip to content

Instantly share code, notes, and snippets.

@beccasaurus
Created June 27, 2009 03:10
Show Gist options
  • Save beccasaurus/136872 to your computer and use it in GitHub Desktop.
Save beccasaurus/136872 to your computer and use it in GitHub Desktop.

Rack::OpenID example

This config.ru Rackup file is an example of a simple Rack application using Rack::OpenID

require 'rack/openid'
use Rack::Session::Cookie
use Rack::OpenID
run lambda {|env|
request = Rack::Request.new env
session = env['rack.session']
# /logout should clear the session and redirect back to /
if env['PATH_INFO'] == '/logout'
session.clear
[ 302, { 'Location' => '/' }, [] ]
# if we got a response from OpenID, save it in the session and redirect back to /
elsif openid_response = env['rack.openid.response']
session[:openid] = openid_response
[ 302, { 'Location' => '/' }, [] ]
# if we POST the form (with an openid_url field), redirect the user to login via OpenID
elsif openid_url = request.params['openid_url']
[ 401, { 'WWW-Authenticate' => "OpenID identifier=\"#{ openid_url }\""}, [] ]
# display a page with a login form and the user's current logged in status
else
if session[:openid]
if session[:openid].status == :failure
login_status = "Login Failed: #{ session[:openid].message }"
else
login_status = "Logged in as: #{ session[:openid].identity_url }"
end
end
[ 200, { 'Content-Type' => 'text/html' }, %{
<a href="/logout">Logout</a>
<p>#{ login_status }</p>
<pre style="overflow: auto; max-height: 100px; border: 1px solid gray;">#{ session[:openid].to_yaml }</pre>
<form action="/" method="post">
OpenID URL: <input type="text" name="openid_url" />
<input type="submit" value="Login" />
</form>
} ]
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment