class PayPal::SDK::Core::Util::OauthSignature

Attributes

password[RW]
timestamp[RW]
token[RW]
token_secret[RW]
url[RW]
username[RW]

Public Class Methods

new(username, password, token, token_secret, url, timestamp = nil) click to toggle source
# File lib/paypal-sdk/core/util/oauth_signature.rb, line 11
def initialize(username, password, token, token_secret, url, timestamp = nil)
  @username = username
  @password = password
  @token = token
  @token_secret = token_secret
  @url = url
  @timestamp = timestamp || Time.now.to_i.to_s
end

Public Instance Methods

authorization_string() click to toggle source
# File lib/paypal-sdk/core/util/oauth_signature.rb, line 20
def authorization_string
  signature = oauth_signature
  "token=#{token},signature=#{signature},timestamp=#{timestamp}"
end
base_string() click to toggle source
# File lib/paypal-sdk/core/util/oauth_signature.rb, line 35
def base_string
  params = {
    "oauth_consumer_key" => username,
    "oauth_version" => "1.0",
    "oauth_signature_method" => "HMAC-SHA1",
    "oauth_token" => token,
    "oauth_timestamp" => timestamp,
  }
  sorted_query_string = params.sort.map{|v| v.join("=") }.join("&")

  base = [
    "POST",
    paypal_encode(url),
    paypal_encode(sorted_query_string)
  ].join("&")
  base = base.gsub(/%[0-9A-F][0-9A-F]/, &:downcase )
end
oauth_signature() click to toggle source
# File lib/paypal-sdk/core/util/oauth_signature.rb, line 25
def oauth_signature
  key = [
    paypal_encode(password),
    paypal_encode(token_secret),
  ].join("&").gsub(/%[0-9A-F][0-9A-F]/, &:downcase )

  digest = OpenSSL::HMAC.digest('sha1', key, base_string)
  Base64.encode64(digest).chomp
end
paypal_encode(str) click to toggle source

The PayPalURLEncoder java class percent encodes everything other than 'a-zA-Z0-9 _'. Then it converts ' ' to '+'. Ruby's CGI.encode takes care of the ' ' and '*' to satisfy PayPal (but beware, URI.encode percent encodes spaces, and does nothing with '*'). Finally, CGI.encode does not encode '.-', which we need to do here.

# File lib/paypal-sdk/core/util/oauth_signature.rb, line 58
def paypal_encode str
  CGI.escape(str.to_s).gsub('.', '%2E').gsub('-', '%2D')
end