Skip to content

Instantly share code, notes, and snippets.

@pnomolos
Last active December 11, 2015 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pnomolos/4661334 to your computer and use it in GitHub Desktop.
Save pnomolos/4661334 to your computer and use it in GitHub Desktop.
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
@nglx
Copy link

nglx commented Mar 20, 2013

unless client != nil

made me really confused...

@AhmedElSharkasy
Copy link

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