public
Description: Provides a more HTTPish API around the ruby-openid library
Homepage: http://rdoc.info/projects/josh/rack-openid/
Clone URL: git://github.com/josh/rack-openid.git
rack-openid / examples / sinatra.rb
100644 48 lines (37 sloc) 0.932 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
require 'sinatra'
 
# Session needs to be before Rack::OpenID
use Rack::Session::Cookie
 
gem 'rack-openid'
require 'rack/openid'
 
gem 'josh-memcache_openid_store'
require 'openid/store/memcache'
 
use Rack::OpenID, OpenID::Store::Memcache.new
 
get '/login' do
  erb :login
end
 
post '/login' do
  if resp = request.env["rack.openid.response"]
    if resp.status == :success
      "Welcome: #{resp.display_identifier}"
    else
      "Error: #{resp.status}"
    end
  else
    headers 'WWW-Authenticate' => Rack::OpenID.build_header(
      :identifier => params["openid_identifier"]
    )
    throw :halt, [401, 'got openid?']
  end
end
 
use_in_file_templates!
 
__END__
 
@@ login
<form action="/login" method="post">
<p>
<label for="openid_identifier">OpenID:</label>
<input id="openid_identifier" name="openid_identifier" type="text" />
</p>
 
<p>
<input name="commit" type="submit" value="Sign in" />
</p>
</form>