Created
January 4, 2012 03:19
-
-
Save orlando/1558317 to your computer and use it in GitHub Desktop.
cheap oauth implementation
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
module Session | |
def self.generate_timestamp | |
Time.now.strftime("%s") | |
end | |
def self.generate_nonce | |
rand(36**16).to_s(36) | |
end | |
def self.generate_signature(url,params,oauth_consumer_secret,oauth_secret_token,method = 'GET') | |
params.delete(:oauth_signature) | |
parameters = Hash[*params.sort.flatten] | |
base_string = "#{method+'&'+ERB::Util.url_encode(url)+'&'}#{parameters.to_encoded_querystring}" | |
params[:oauth_signature] = Base64.encode64(OpenSSL::HMAC.digest('sha1',"#{oauth_consumer_secret}&#{oauth_secret_token}",base_string)).chomp.gsub(/\n/,'') | |
end | |
def self.build_url(params) | |
parameters = params.dup | |
parameters.merge!( | |
oauth_consumer_key: '', | |
oauth_token: '', | |
oauth_signature_method: 'HMAC-SHA1', | |
oauth_timestamp: generate_timestamp, | |
oauth_nonce: generate_nonce, | |
oauth_version: '1.0' | |
) | |
generate_signature(BASE_URI,parameters,OAUTH_CONSUMER_SECRET,'oauth_secret','GET') | |
url = "#{BASE_URI}?#{parameters.to_querystring}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment