Signature and Nonce for OAuth (Ruby)

I am looking for access to the SmugMug API from my application in order to capture user albums and images (users authenticated through ruby ​​OmniAuth).

According to the SmugMug OAuth API , OAuth requires six parameters.

I can get the token with OmniAuth, and the timestamp should be easy (Time.now.to_i right?). There are two things that I don’t know how to create - oauth_nonce and oauth_signature.

According to oauth docs, I generate nonce via timestamp, but how exactly did I do this? Should it be of a certain length and limited to certain characters?

And, of course, a signature. How can I generate HMAC-SHA1 sig with ruby? I know that the oauth gem can do this, but I would rather use it myself with OmniAuth. Looking at the code, I had problems deciphering how the oauth stone generates whitefish.

Thanks for any help.

+8
ruby ruby-on-rails ruby-on-rails-3 oauth omniauth
source share
3 answers

for signature

def sign( key, base_string ) digest = OpenSSL::Digest::Digest.new( 'sha1' ) hmac = OpenSSL::HMAC.digest( digest, key, base_string ) Base64.encode64( hmac ).chomp.gsub( /\n/, '' ) end#def 

You do not need to generate nonce from a timestamp, but this may make sense, since the timestamp is obviously unique, so it makes a good initial input for any randomization function.

I use this (what I got from another question here and changed)

 def nonce rand(10 ** 30).to_s.rjust(30,'0') end#def 

but you can use everything that generates a unique string.

See this method by erikeldridge on github and the OAuth Beginner's Guide for more

Edit

Since then, I have found a better way to generate random strings in the Ruby standard library, SecureRandom .

+10
source share

Odd can also be just a big, truly random number - for example, using the Ruby SecureRandom class (do not use rand):

 require 'securerandom' 

...

 nonce = SecureRandom.hex() 

This generates a 16-byte random number in hexadecimal format.

+4
source share

Why don't you just use Oauth ruby ​​gems for this?

0
source share

Source: https://habr.com/ru/post/650423/


All Articles