Class: Oauth
- Inherits:
-
Object
- Object
- Oauth
- Defined in:
- lib/orkut/oauth.rb
Instance Attribute Summary collapse
-
#callback_url ⇒ Object
Returns the value of attribute callback_url.
-
#consumer_key ⇒ Object
Returns the value of attribute consumer_key.
-
#consumer_secret ⇒ Object
Returns the value of attribute consumer_secret.
-
#oauth_version ⇒ Object
Returns the value of attribute oauth_version.
-
#params ⇒ Object
Returns the value of attribute params.
-
#req_method ⇒ Object
Returns the value of attribute req_method.
-
#sig_method ⇒ Object
Returns the value of attribute sig_method.
-
#token ⇒ Object
Returns the value of attribute token.
-
#token_secret ⇒ Object
Returns the value of attribute token_secret.
Instance Method Summary collapse
- #construct_req_url(url) ⇒ Object
-
#generate_nonce ⇒ Object
openssl::random_bytes returns non-word chars, which need to be removed.
- #generate_sig(args) ⇒ Object
-
#initialize ⇒ Oauth
constructor
A new instance of Oauth.
-
#normalize_req_params(params) ⇒ Object
sort (very important as it affects the signature), concat, and percent encode.
- #percent_encode(string) ⇒ Object
- #sign(url) ⇒ Object
- #to_header_string ⇒ Object
- #to_query_string ⇒ Object
Constructor Details
#initialize ⇒ Oauth
Returns a new instance of Oauth.
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/orkut/oauth.rb', line 14 def initialize @consumer_key = '' @consumer_secret = '' @token = '' @token_secret = '' @req_method = 'GET' @sig_method = 'HMAC-SHA1' @oauth_version = '1.0' @callback_url = '' @params = [] end |
Instance Attribute Details
#callback_url ⇒ Object
Returns the value of attribute callback_url.
12 13 14 |
# File 'lib/orkut/oauth.rb', line 12 def callback_url @callback_url end |
#consumer_key ⇒ Object
Returns the value of attribute consumer_key.
12 13 14 |
# File 'lib/orkut/oauth.rb', line 12 def consumer_key @consumer_key end |
#consumer_secret ⇒ Object
Returns the value of attribute consumer_secret.
12 13 14 |
# File 'lib/orkut/oauth.rb', line 12 def consumer_secret @consumer_secret end |
#oauth_version ⇒ Object
Returns the value of attribute oauth_version.
12 13 14 |
# File 'lib/orkut/oauth.rb', line 12 def oauth_version @oauth_version end |
#params ⇒ Object
Returns the value of attribute params.
12 13 14 |
# File 'lib/orkut/oauth.rb', line 12 def params @params end |
#req_method ⇒ Object
Returns the value of attribute req_method.
12 13 14 |
# File 'lib/orkut/oauth.rb', line 12 def req_method @req_method end |
#sig_method ⇒ Object
Returns the value of attribute sig_method.
12 13 14 |
# File 'lib/orkut/oauth.rb', line 12 def sig_method @sig_method end |
#token ⇒ Object
Returns the value of attribute token.
12 13 14 |
# File 'lib/orkut/oauth.rb', line 12 def token @token end |
#token_secret ⇒ Object
Returns the value of attribute token_secret.
12 13 14 |
# File 'lib/orkut/oauth.rb', line 12 def token_secret @token_secret end |
Instance Method Details
#construct_req_url(url) ⇒ Object
47 48 49 50 |
# File 'lib/orkut/oauth.rb', line 47 def construct_req_url( url) parsed_url = URI.parse( url ) parsed_url.scheme + '://' + parsed_url.host + parsed_url.path end |
#generate_nonce ⇒ Object
openssl::random_bytes returns non-word chars, which need to be removed. using alt method to get consistent length ref snippets.dzone.com/posts/show/491
28 29 30 |
# File 'lib/orkut/oauth.rb', line 28 def generate_nonce Array.new( 5 ) { rand(256) }.pack('C*').unpack('H*').first end |
#generate_sig(args) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/orkut/oauth.rb', line 53 def generate_sig( args ) key = percent_encode( args[:consumer_secret] ) + '&' + percent_encode( args[:token_secret] ) text = args[:base_str] # credit: http://blog.nathanielbibler.com/post/63031273/openssl-hmac-vs-ruby-hmac-benchmarks digest = OpenSSL::Digest::Digest.new( 'sha1' ) raw_sig = OpenSSL::HMAC.digest( digest, key, text ) # ref http://groups.google.com/group/oauth-ruby/browse_thread/thread/9110ed8c8f3cae81 Base64.encode64( raw_sig ).chomp.gsub( /\n/, '' ) end |
#normalize_req_params(params) ⇒ Object
sort (very important as it affects the signature), concat, and percent encode
42 43 44 |
# File 'lib/orkut/oauth.rb', line 42 def normalize_req_params( params ) percent_encode( params.sort().join( '&' ) ) end |
#percent_encode(string) ⇒ Object
32 33 34 35 36 |
# File 'lib/orkut/oauth.rb', line 32 def percent_encode( string ) # ref http://snippets.dzone.com/posts/show/1260 URI.escape( string, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]") ) end |
#sign(url) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/orkut/oauth.rb', line 80 def sign( url ) parsed_url = URI.parse( url ) # basic oauth params formatted as strings in array so we can easily sort @params.push( 'oauth_consumer_key=' + @consumer_key ) @params.push( 'oauth_nonce=' + generate_nonce() ) @params.push( 'oauth_signature_method=' + @sig_method ) @params.push( 'oauth_timestamp=' + Time.now.to_i.to_s ) @params.push( 'oauth_version=' + @oauth_version ) # if params passed in, add them & replace defaults if parsed_url.query @params = @params | parsed_url.query.split( '&' ) end # elems for base str normal_req_params = normalize_req_params( params ) # @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 base_str = [ @req_method, percent_encode( req_url ), normal_req_params ].join( '&' ) # sign signature = generate_sig({ :base_str => base_str, :consumer_secret => @consumer_secret, :token_secret => @token_secret }) @params.push( 'oauth_signature=' + percent_encode( signature ) ) return self end |
#to_header_string ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/orkut/oauth.rb', line 69 def to_header_string pairs = [] # format string as key1="val1", key2="val2" ... @params.find_all{ | item | item =~ /oauth_.*/ }.each{ | item | pair = '%s="%s"' % item.split( '=' ) pairs.push( pair ) } pairs.join( ', ' ) end |
#to_query_string ⇒ Object
65 66 67 |
# File 'lib/orkut/oauth.rb', line 65 def to_query_string @params.find_all{ | item | item =~ /oauth_.*/ }.join( '&' ) end |