Last active
December 11, 2015 21:18
-
-
Save pnomolos/4661334 to your computer and use it in GitHub Desktop.
Get OAuth-two-legged request working in Rails 3.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
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
made me really confused...