Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5d842ce

Browse files
author
Michael Wood
committedJan 29, 2009
Make the Oauth Provider a Sintra Rack Middleware app.
1 parent 16f27ca commit 5d842ce

File tree

4 files changed

+227
-0
lines changed

4 files changed

+227
-0
lines changed
 

‎lib/rack_oauth_provider.rb

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
require 'sinatra/base'
2+
require 'oauth/request_proxy/rack_request'
3+
require File.dirname(__FILE__) + '/oauth_provider/lib/oauth_provider'
4+
5+
class RackOAuthProvider < Sinatra::Base
6+
7+
def initialize(app, paths)
8+
@paths = paths
9+
@app = app
10+
end
11+
12+
set :root, File.dirname(__FILE__)
13+
set :views, File.dirname(__FILE__) + '/rack_oauth_provider'
14+
15+
provider = OAuthProvider::create(:sqlite3, 'provider.sqlite3')
16+
#provider = OAuthProvider::create(:data_mapper, 'provider.sqlite3')
17+
18+
mime :json, "application/json"
19+
20+
# http://blog.joncrosby.me/post/72451217/a-world-of-middleware
21+
# this hackeration is required for sinatra to be a nice rack citizen
22+
error 404 do
23+
@app.call(env)
24+
end
25+
26+
before do
27+
# check protected path agaist request path
28+
# see if we should proceed with oauth access confirmation...
29+
path = @request.path_info
30+
31+
@paths.each do |protected_oauth_path,protected_oauth_method|
32+
if protected_oauth_path.match(path)
33+
34+
if protected_oauth_method.include?(@request.request_method.to_s.downcase.to_sym)
35+
warn path + " was matched to " + @request.request_method.to_s + " " + protected_oauth_path.to_s
36+
37+
oauth_confirm_access(provider)
38+
end
39+
end
40+
end
41+
end
42+
43+
# OAuth routes
44+
get "/oauth/request_token" do
45+
provider.issue_request(request).query_string
46+
end
47+
48+
get "/oauth/access_token" do
49+
if access_token = provider.upgrade_request(request)
50+
access_token.query_string
51+
else
52+
raise Sinatra::NotFound, "No such request token"
53+
end
54+
end
55+
56+
# Authorize endpoints
57+
get "/oauth/authorize" do
58+
if @request_token = provider.backend.find_user_request(params[:oauth_token])
59+
erb :authorize
60+
else
61+
raise Sinatra::NotFound, "No such request token"
62+
end
63+
end
64+
65+
post "/oauth/authorize" do
66+
if request_token = provider.backend.find_user_request(params[:oauth_token])
67+
if request_token.authorize
68+
redirect request_token.callback
69+
else
70+
raise "Could not authorize"
71+
end
72+
else
73+
raise Sinatra::NotFound, "No such request token"
74+
end
75+
end
76+
77+
get "/oauth/applications" do
78+
@consumers = provider.consumers
79+
erb :applications
80+
end
81+
82+
post '/oauth/applications' do
83+
begin
84+
@consumer = provider.add_consumer(params[:application_callback])
85+
86+
#redirect "/oauth/applications"
87+
@consumer_key = @consumer.token.shared_key
88+
@consumer_secret = @consumer.token.secret_key
89+
90+
rescue Exception
91+
@error = "Failed to create a token!"
92+
end
93+
94+
@consumers = provider.consumers
95+
96+
erb :applications
97+
end
98+
99+
private
100+
101+
def oauth_confirm_access(provider)
102+
begin
103+
access = provider.confirm_access(@request)
104+
rescue Exception
105+
halt "No access! Please verify your OAuth access token and secret."
106+
end
107+
end
108+
109+
110+
end
111+
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<% if @error %>
2+
<div style="background-color: #fee; padding: 20px; margin-bottom: 10px;">
3+
<h2>Oops!</h2>
4+
5+
<p><%= @error %></p>
6+
</div>
7+
<% end %>
8+
9+
<% if @consumer_key and @consumer_secret %>
10+
<div style="background-color: #efe; padding: 20px; margin-bottom: 10px;">
11+
<h2>Application created!</h2>
12+
13+
<h3>Save this information!</h3>
14+
15+
<b>Consumer Key:</b>
16+
<%= @consumer_key %>
17+
<br/>
18+
19+
<b>Consumer Secret:</b>
20+
<%= @consumer_secret %>
21+
</div>
22+
<% end %>
23+
24+
25+
<h2>My Applications</h2>
26+
27+
<% if !@consumers.empty? %>
28+
<% @consumers.each do |consumer| %>
29+
<div style="background-color: #eef; padding: 20px; margin-bottom: 10px;">
30+
<b><%= consumer.callback %></b>
31+
<p>
32+
<b>Consumer Key:</b> <%= consumer.token.shared_key %>
33+
<br/>
34+
<b>Consumer Secret:</b> <%= consumer.token.secret_key %>
35+
</p>
36+
</div>
37+
<% end %>
38+
<% else %>
39+
You don't have any applications... yet.
40+
<% end %>
41+
42+
<h3>Create New Application</h3>
43+
44+
<div class="app">
45+
<form action="/oauth/applications" method="POST">
46+
<div>
47+
Application Callback:
48+
<br/>
49+
<input type="text" name="application_callback" id="application_callback" rows="20" />
50+
</div>
51+
<input type="submit" value="Create Application"/>
52+
</form>
53+
</div>

‎lib/rack_oauth_provider/authorize.erb

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<h2>You are about to authorize (<%= @request_token.consumer.callback %>)</h2>
2+
<form action="/oauth/authorize" method="post">
3+
<p>
4+
<input id="oauth_token" name="oauth_token" type="hidden" value="<%= @request_token.shared_key %>" />
5+
</p>
6+
7+
<p>
8+
<input name="commit" type="submit" value="Authorize" />
9+
</p>
10+
</form>

‎lib/rack_oauth_provider/layout.erb

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2+
<html xmlns="http://www.w3.org/1999/xhtml">
3+
<head>
4+
<title>Sinatra OAuth API Test!</title>
5+
<style>
6+
html {
7+
background-color: #eee;
8+
font-family: Arial, sans-serif;
9+
}
10+
h1 {
11+
margin-top:0;
12+
}
13+
input[type="text"],textarea
14+
{
15+
font-size:16px;
16+
width: 100%;
17+
}
18+
input[type="submit"] {
19+
margin-top: 20px;
20+
background-color: #efe;
21+
border: 1px solid #ccc;
22+
padding: 10px;
23+
font-weight: bold;
24+
font-size: 16px;
25+
cursor: pointer
26+
}
27+
#content {
28+
width: 600px;
29+
margin: 100px auto;
30+
background-color: #fff;
31+
border: 1px solid #ccc;
32+
padding: 20px;
33+
}
34+
#navigation {
35+
background-color: #eee;
36+
padding: 20px;
37+
margin-bottom: 20px;
38+
39+
}
40+
</style>
41+
</head>
42+
<body>
43+
<div id="content">
44+
<h1>Sinatra OAuth API Rack Middleware</h1>
45+
46+
<div id="navigation">
47+
<a href="/">Home</a> | <a href="/messages">Messages</a> | <a href="/oauth/applications">OAuth Applications</a>
48+
</div>
49+
50+
<%= yield %>
51+
</div>
52+
</body>
53+
</html>

0 commit comments

Comments
 (0)
Please sign in to comment.