Class: Tambur::Util::OAuth

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-tambur/vendor/oauth_util.rb

Overview

A utility for signing an url using OAuth in a way that’s convenient for debugging Note: the standard Ruby OAuth lib is here github.com/mojodna/oauth License: gist.github.com/375593 Usage: see example.rb below

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOAuth

Returns a new instance of OAuth.



20
21
22
23
24
25
26
27
28
29
# File 'lib/ruby-tambur/vendor/oauth_util.rb', line 20

def initialize
  @consumer_key = ''
  @consumer_secret = ''
  @token = ''
  @token_secret = ''
  @req_method = 'GET'
  @sig_method = 'HMAC-SHA1'
  @oauth_version = '1.0'
  @callback_url = ''
end

Instance Attribute Details

#base_strObject

Returns the value of attribute base_str.



17
18
19
# File 'lib/ruby-tambur/vendor/oauth_util.rb', line 17

def base_str
  @base_str
end

#callback_urlObject

Returns the value of attribute callback_url.



17
18
19
# File 'lib/ruby-tambur/vendor/oauth_util.rb', line 17

def callback_url
  @callback_url
end

#consumer_keyObject

Returns the value of attribute consumer_key.



17
18
19
# File 'lib/ruby-tambur/vendor/oauth_util.rb', line 17

def consumer_key
  @consumer_key
end

#consumer_secretObject

Returns the value of attribute consumer_secret.



17
18
19
# File 'lib/ruby-tambur/vendor/oauth_util.rb', line 17

def consumer_secret
  @consumer_secret
end

#oauth_versionObject

Returns the value of attribute oauth_version.



17
18
19
# File 'lib/ruby-tambur/vendor/oauth_util.rb', line 17

def oauth_version
  @oauth_version
end

#paramsObject

Returns the value of attribute params.



17
18
19
# File 'lib/ruby-tambur/vendor/oauth_util.rb', line 17

def params
  @params
end

#req_methodObject

Returns the value of attribute req_method.



17
18
19
# File 'lib/ruby-tambur/vendor/oauth_util.rb', line 17

def req_method
  @req_method
end

#req_urlObject

Returns the value of attribute req_url.



17
18
19
# File 'lib/ruby-tambur/vendor/oauth_util.rb', line 17

def req_url
  @req_url
end

#sig_methodObject

Returns the value of attribute sig_method.



17
18
19
# File 'lib/ruby-tambur/vendor/oauth_util.rb', line 17

def sig_method
  @sig_method
end

#tokenObject

Returns the value of attribute token.



17
18
19
# File 'lib/ruby-tambur/vendor/oauth_util.rb', line 17

def token
  @token
end

#token_secretObject

Returns the value of attribute token_secret.



17
18
19
# File 'lib/ruby-tambur/vendor/oauth_util.rb', line 17

def token_secret
  @token_secret
end

Instance Method Details

#nonceObject

openssl::random_bytes returns non-word chars, which need to be removed. using alt method to get length ref snippets.dzone.com/posts/show/491



33
34
35
# File 'lib/ruby-tambur/vendor/oauth_util.rb', line 33

def nonce
  Array.new( 5 ) { rand(256) }.pack('C*').unpack('H*').first
end

#percent_encode(string) ⇒ Object



37
38
39
# File 'lib/ruby-tambur/vendor/oauth_util.rb', line 37

def percent_encode( string )
  return URI.escape( string, /[^a-zA-Z0-9\-\.\_\~]/)
end

#query_stringObject

sort (very important as it affects the signature), concat, and percent encode



57
58
59
60
61
62
63
# File 'lib/ruby-tambur/vendor/oauth_util.rb', line 57

def query_string
  pairs = []
  @params.sort.each { | key, val |
    pairs.push( "#{ percent_encode( key ) }=#{ percent_encode( val.to_s ) }" )
  }
  pairs.join '&'
end

#sign(parsed_url) ⇒ Object

organize params & create signature



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ruby-tambur/vendor/oauth_util.rb', line 66

def sign( parsed_url )

  @params = {
    'oauth_consumer_key' => @consumer_key,
    'oauth_nonce' => nonce,
    'oauth_signature_method' => @sig_method,
    'oauth_timestamp' => Time.now.to_i.to_s,
    'oauth_version' => @oauth_version
  }

  # if url has query, merge key/values into params obj overwriting defaults
  if parsed_url.query
    @params.merge! CGI.parse( parsed_url.query ).inject({}){|h, (k, v)|h[k.to_s] = v[0];h}
  end

  # @ref http://oauth.net/core/1.0/#rfc.section.9.1.2
  @req_url = parsed_url.scheme + '://' + parsed_url.host + parsed_url.path

  # create base str. make it an object attr for ez debugging
  # ref http://oauth.net/core/1.0/#anchor14
  @base_str = [
    @req_method,
    percent_encode( req_url ),

    # normalization is just x-www-form-urlencoded
    percent_encode( query_string )

  ].join( '&' )

  # add signature
  @params[ 'oauth_signature' ] = signature

  return self
end

#signatureObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/ruby-tambur/vendor/oauth_util.rb', line 42

def signature
  key = percent_encode( @consumer_secret ) + '&' + percent_encode( @token_secret )

  # ref: http://blog.nathanielbibler.com/post/63031273/openssl-hmac-vs-ruby-hmac-benchmarks
  digest = OpenSSL::Digest::Digest.new( 'sha1' )
  hmac = OpenSSL::HMAC.digest( digest, key, @base_str )

  # ref http://groups.google.com/group/oauth-ruby/browse_thread/thread/9110ed8c8f3cae81
  Base64.encode64( hmac ).chomp.gsub( /\n/, '' )
end