Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get OAuth-two-legged request working in Rails 3.
# Based on work from http://mifsud.me/simple-two-legged-oauth-provider-in-rails-2
# Note that you need a user with :api_key and :secret fields
require 'oauth/request_proxy/rack_request'
class ApplicationController < ActionController::Base
protect_from_forgery
protected
def run_oauth_check
req = OAuth::RequestProxy::RackRequest.new(request)
return render :json => { :error => "Invalid request" },
:status => 400 unless req.parameters['oauth_consumer_key']
client = User.first :api_key => req.parameters['oauth_consumer_key']
return render :json => { :error => "Invalid credentials" },
:status => 401 if client.nil?
begin
signature = ::OAuth::Signature.build(::Rack::Request.new(env)) do |rp|
[nil, client.secret]
end
return render :json => { :error => "Invalid credentials" },
:status => 401 unless signature.verify
rescue ::OAuth::Signature::UnknownSignatureMethod => e
return render :json => { :error => "Unknown signature method" }, :status => 400
end
end
end
@managr

This comment has been minimized.

Copy link

managr commented Mar 20, 2013

unless client != nil

made me really confused...

@AhmedElSharkasy

This comment has been minimized.

Copy link

AhmedElSharkasy commented Nov 22, 2013

i am facing this error undefined method `unpack' for nil:NilClass at line 25 (signature.verify) , any clue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment