Skip to content

Instantly share code, notes, and snippets.

@orlando
Created January 4, 2012 03:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orlando/1558317 to your computer and use it in GitHub Desktop.
Save orlando/1558317 to your computer and use it in GitHub Desktop.
cheap oauth implementation
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