Created
January 5, 2009 07:08
-
-
Save ahx/43293 to your computer and use it in GitHub Desktop.
An example for an OpenID consumer using Sinatra
This file contains 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
# An example for an OpenID consumer using Sinatra | |
require 'rubygems' | |
require 'sinatra' | |
gem 'ruby-openid', '>=2.1.2' | |
require 'openid' | |
require 'openid/store/filesystem' | |
helpers do | |
def openid_consumer | |
@openid_consumer ||= OpenID::Consumer.new(session, | |
OpenID::Store::Filesystem.new("#{File.dirname(__FILE__)}/tmp/openid")) | |
end | |
def root_url | |
request.url.match(/(^.*\/{2}[^\/]*)/)[1] | |
end | |
end | |
get '/login' do | |
erb :login | |
end | |
post '/login/openid' do | |
openid = params[:openid_identifier] | |
begin | |
oidreq = openid_consumer.begin(openid) | |
rescue OpenID::DiscoveryFailure => why | |
"Sorry, we couldn't find your identifier #{openid}." | |
else | |
# You could request additional information here - see specs: | |
# http://openid.net/specs/openid-simple-registration-extension-1_0.html | |
# oidreq.add_extension_arg('sreg','required','nickname') | |
# oidreq.add_extension_arg('sreg','optional','fullname, email') | |
# Send request - first parameter: Trusted Site, | |
# second parameter: redirect target | |
redirect oidreq.redirect_url(root_url, root_url + "/login/openid/complete") | |
end | |
end | |
get '/login/openid/complete' do | |
oidresp = openid_consumer.complete(params, request.url) | |
openid = oidresp.display_identifier | |
case oidresp.status | |
when OpenID::Consumer::FAILURE | |
"Sorry, we could not authenticate you with this identifier #{openid}." | |
when OpenID::Consumer::SETUP_NEEDED | |
"Immediate request failed - Setup Needed" | |
when OpenID::Consumer::CANCEL | |
"Login cancelled." | |
when OpenID::Consumer::SUCCESS | |
# Access additional informations: | |
# puts params['openid.sreg.nickname'] | |
# puts params['openid.sreg.fullname'] | |
"Login successfull." # startup something | |
end | |
end | |
use_in_file_templates! | |
__END__ | |
@@ layout | |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" | |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> | |
<head> | |
<meta http-equiv="content-type" content="text/html;charset=UTF-8" /> | |
<title>got openid?</title> | |
</head> | |
<body> | |
<%= yield %> | |
</body> | |
</html> | |
@@ login | |
<form method="post" accept-charset="UTF-8" action='/login/openid'> | |
Identifier: | |
<input type="text" class="openid" name="openid_identifier" /> | |
<input type="submit" value="Verify" /> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment