Class: PayPal::SDK::Core::Util::OauthSignature

Inherits:
Object
  • Object
show all
Defined in:
lib/paypal-sdk/core/util/oauth_signature.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, token, token_secret, url, timestamp = nil) ⇒ OauthSignature

Returns a new instance of OauthSignature.



11
12
13
14
15
16
17
18
# 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

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



9
10
11
# File 'lib/paypal-sdk/core/util/oauth_signature.rb', line 9

def password
  @password
end

#timestampObject

Returns the value of attribute timestamp.



9
10
11
# File 'lib/paypal-sdk/core/util/oauth_signature.rb', line 9

def timestamp
  @timestamp
end

#tokenObject

Returns the value of attribute token.



9
10
11
# File 'lib/paypal-sdk/core/util/oauth_signature.rb', line 9

def token
  @token
end

#token_secretObject

Returns the value of attribute token_secret.



9
10
11
# File 'lib/paypal-sdk/core/util/oauth_signature.rb', line 9

def token_secret
  @token_secret
end

#urlObject

Returns the value of attribute url.



9
10
11
# File 'lib/paypal-sdk/core/util/oauth_signature.rb', line 9

def url
  @url
end

#usernameObject

Returns the value of attribute username.



9
10
11
# File 'lib/paypal-sdk/core/util/oauth_signature.rb', line 9

def username
  @username
end

Instance Method Details

#authorization_stringObject



20
21
22
23
# 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_stringObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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_signatureObject



25
26
27
28
29
30
31
32
33
# 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) ⇒ Object

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.



58
59
60
# 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