Permalink
Browse files

Moved OAuth functionality to the Sinatra OAuth Rack Middleware lib.

  • Loading branch information...
1 parent 6995b7b commit 4bb95ac5f201e98c7b26c66f5181133972979871 @eddanger committed Jan 29, 2009
Showing with 11 additions and 82 deletions.
  1. +11 −82 provider.rb
View
@@ -4,10 +4,16 @@
require 'dm-validations'
require 'dm-timestamps'
require 'dm-serializer'
-require 'oauth/request_proxy/rack_request'
-require File.dirname(__FILE__) + '/lib/oauth_provider/lib/oauth_provider'
-#require 'restfulx'
-#require File.dirname(__FILE__) + '/lib/restfulx/lib/restfulx'
+require File.dirname(__FILE__) + '/lib/rack_oauth_provider'
+
+# a list of oauth protected paths
+paths = {
+ Regexp.new('\/messages.json') => [:get, :post],
+ Regexp.new('\/messages\/[0-9]+.json') => [:get, :put, :delete],
+}
+
+use RackOAuthProvider, paths do
+end
DataMapper.setup(:default, "sqlite3:///#{Dir.pwd}/provider.sqlite3")
@@ -24,10 +30,7 @@ class Message
DataMapper.auto_upgrade!
-provider = OAuthProvider::create(:sqlite3, 'test.sqlite3')
-#provider = OAuthProvider::create(:data_mapper, 'test.sqlite3')
-
-mime :json, "application/json"
+set :views, File.dirname(__FILE__) + '/views'
error do
exception = request.env['sinatra.error']
@@ -38,62 +41,6 @@ class Message
erb :error
end
-# OAuth routes
-get "/oauth/request_token" do
- provider.issue_request(request).query_string
-end
-
-get "/oauth/access_token" do
- if access_token = provider.upgrade_request(request)
- access_token.query_string
- else
- raise Sinatra::NotFound, "No such request token"
- end
-end
-
-# Authorize endpoints
-get "/oauth/authorize" do
- if @request_token = provider.backend.find_user_request(params[:oauth_token])
- erb :authorize
- else
- raise Sinatra::NotFound, "No such request token"
- end
-end
-
-post "/oauth/authorize" do
- if request_token = provider.backend.find_user_request(params[:oauth_token])
- if request_token.authorize
- redirect request_token.callback
- else
- raise "Could not authorize"
- end
- else
- raise Sinatra::NotFound, "No such request token"
- end
-end
-
-get "/oauth/applications" do
- @consumers = provider.consumers
- erb :applications
-end
-
-post '/oauth/applications' do
- begin
- @consumer = provider.add_consumer(params[:application_callback])
-
- #redirect "/oauth/applications"
- @consumer_key = @consumer.token.shared_key
- @consumer_secret = @consumer.token.secret_key
-
- rescue Exception
- @error = "Failed to create a token!"
- end
-
- @consumers = provider.consumers
-
- erb :applications
-end
-
# index!
get '/' do
erb :index
@@ -126,38 +73,28 @@ class Message
end
get '/:model.json' do
- oauth_confirm_access(provider, request)
-
"#{params[:model]}".singularize.camelize.to_class.all.to_json
end
post '/:model.json' do
- oauth_confirm_access(provider, request)
-
name = params[:model].singularize
record = name.camelize.to_class.new(JSON.parse(CGI::unescape(request.body.string))[name])
record.save
record.to_json
end
get '/:model/:id.json' do
- oauth_confirm_access(provider, request)
-
"#{params[:model]}".singularize.camelize.to_class.get(params[:id]).to_json
end
put '/:model/:id.json' do
- oauth_confirm_access(provider, request)
-
name = params[:model].singularize
record = name.camelize.to_class.get(params[:id])
record.update_attributes(JSON.parse(request.body.string)[name])
record.to_json
end
delete '/:model/:id.json' do
- oauth_confirm_access(provider, request)
-
record = "#{params[:model]}".singularize.camelize.to_class.get(params[:id])
result = record.to_json
record.destroy
@@ -166,14 +103,6 @@ class Message
private
-def oauth_confirm_access(provider, request)
- begin
- access = provider.confirm_access(request)
- rescue Exception
- halt "No access! Please verify your OAuth access token and secret."
- end
-end
-
class String
def to_class
Kernel.const_get(self)

0 comments on commit 4bb95ac

Please sign in to comment.