@@ -29,6 +29,8 @@ def self.from_access_token(token, secret, options = {})
2929 # @option opts [FixNum, String] :algorithm (hmac-sha-256) the algorithm to use for the HMAC digest (one of 'hmac-sha-256', 'hmac-sha-1')
3030 def initialize ( client , token , secret , opts = { } )
3131 @secret = secret
32+ @seq_nr = SecureRandom . random_number ( 2 ** 64 - 1 )
33+ @kid = opts . delete ( :kid ) || strict_encode64 ( Digest ::SHA1 . digest ( token ) )
3234 self . algorithm = opts . delete ( :algorithm ) || 'hmac-sha-256'
3335
3436 super ( client , token , opts )
@@ -59,33 +61,30 @@ def headers
5961 # @param [Symbol] verb the HTTP request method
6062 # @param [String] url the HTTP URL path of the request
6163 def header ( verb , url )
62- timestamp = Time . now . utc . to_i
63- nonce = Digest :: MD5 . hexdigest ( [ timestamp , SecureRandom . hex ] . join ( ':' ) )
64+ timestamp = ( Time . now . to_f * 1000 ) . floor
65+ @seq_nr = ( @seq_nr + 1 ) % ( 2 ** 64 - 1 )
6466
65- uri = URI . parse ( url )
67+ uri = URI ( url )
6668
6769 fail ( ArgumentError , "could not parse \" #{ url } \" into URI" ) unless uri . is_a? ( URI ::HTTP )
6870
69- mac = signature ( timestamp , nonce , verb , uri )
71+ mac = signature ( timestamp , @seq_nr , verb , uri )
7072
71- "MAC id =\" #{ token } \" , ts=\" #{ timestamp } \" , nonce =\" #{ nonce } \" , mac=\" #{ mac } \" "
73+ "MAC kid =\" #{ @kid } \" , ts=\" #{ timestamp } \" , seq-nr =\" #{ @seq_nr } \" , mac=\" #{ mac } \" "
7274 end
7375
7476 # Generate the Base64-encoded HMAC digest signature
7577 #
76- # @param [Fixnum] timestamp the timestamp of the request in seconds since epoch
77- # @param [String] nonce the MAC header nonce
78+ # @param [Fixnum] timestamp the timestamp of the request in milliseconds since epoch
79+ # @param [Fixnum] seq_nr the MAC header sequence number
7880 # @param [Symbol] verb the HTTP request method
7981 # @param [String] url the HTTP URL path of the request
80- def signature ( timestamp , nonce , verb , uri )
82+ def signature ( timestamp , seq_nr , verb , uri )
8183 signature = [
84+ "#{ verb . to_s . upcase } #{ uri . request_uri } HTTP/1.1" ,
8285 timestamp ,
83- nonce ,
84- verb . to_s . upcase ,
85- uri . request_uri ,
86- uri . host ,
87- uri . port ,
88- '' , nil
86+ seq_nr ,
87+ ''
8988 ] . join ( "\n " )
9089
9190 strict_encode64 ( OpenSSL ::HMAC . digest ( @algorithm , secret , signature ) )
0 commit comments