viki_utils 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/viki/uri_signer.rb +6 -2
 - data/lib/viki_utils/version.rb +1 -1
 - data/spec/viki/uri_signer_spec.rb +7 -1
 - metadata +1 -1
 
    
        data/lib/viki/uri_signer.rb
    CHANGED
    
    | 
         @@ -11,7 +11,7 @@ module Viki 
     | 
|
| 
       11 
11 
     | 
    
         
             
                  @secret = secret
         
     | 
| 
       12 
12 
     | 
    
         
             
                end
         
     | 
| 
       13 
13 
     | 
    
         | 
| 
       14 
     | 
    
         
            -
                def sign_request(uri,body = nil)
         
     | 
| 
      
 14 
     | 
    
         
            +
                def sign_request(uri, body = nil)
         
     | 
| 
       15 
15 
     | 
    
         
             
                  Addressable::URI.parse(uri).tap do |signed_uri|
         
     | 
| 
       16 
16 
     | 
    
         
             
                    query = signed_uri.query_values || {}
         
     | 
| 
       17 
17 
     | 
    
         
             
                    query[:t] = Time.now.to_i
         
     | 
| 
         @@ -20,10 +20,14 @@ module Viki 
     | 
|
| 
       20 
20 
     | 
    
         
             
                    to_sign = [signed_uri.path, signed_uri.query].join('?')
         
     | 
| 
       21 
21 
     | 
    
         
             
                    to_sign += Oj.dump(body) if body
         
     | 
| 
       22 
22 
     | 
    
         | 
| 
       23 
     | 
    
         
            -
                    sig =  
     | 
| 
      
 23 
     | 
    
         
            +
                    sig = UriSigner.sign(@secret, to_sign)
         
     | 
| 
       24 
24 
     | 
    
         
             
                    query.merge!(sig: sig)
         
     | 
| 
       25 
25 
     | 
    
         
             
                    signed_uri.query_values = query
         
     | 
| 
       26 
26 
     | 
    
         
             
                  end
         
     | 
| 
       27 
27 
     | 
    
         
             
                end
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
                def self.sign(secret, data)
         
     | 
| 
      
 30 
     | 
    
         
            +
                  OpenSSL::HMAC.hexdigest(DIGEST, secret, data)
         
     | 
| 
      
 31 
     | 
    
         
            +
                end
         
     | 
| 
       28 
32 
     | 
    
         
             
              end
         
     | 
| 
       29 
33 
     | 
    
         
             
            end
         
     | 
    
        data/lib/viki_utils/version.rb
    CHANGED
    
    
| 
         @@ -3,7 +3,7 @@ require_relative "../../lib/viki/uri_signer" 
     | 
|
| 
       3 
3 
     | 
    
         
             
            require 'timecop'
         
     | 
| 
       4 
4 
     | 
    
         | 
| 
       5 
5 
     | 
    
         
             
            describe Viki::UriSigner do
         
     | 
| 
       6 
     | 
    
         
            -
              describe " 
     | 
| 
      
 6 
     | 
    
         
            +
              describe "#sign_request" do
         
     | 
| 
       7 
7 
     | 
    
         
             
                let(:uri) { URI("http://example.com/endpoint") }
         
     | 
| 
       8 
8 
     | 
    
         
             
                let(:secret) { "your_secret" }
         
     | 
| 
       9 
9 
     | 
    
         | 
| 
         @@ -40,4 +40,10 @@ describe Viki::UriSigner do 
     | 
|
| 
       40 
40 
     | 
    
         
             
                  end
         
     | 
| 
       41 
41 
     | 
    
         
             
                end
         
     | 
| 
       42 
42 
     | 
    
         
             
              end
         
     | 
| 
      
 43 
     | 
    
         
            +
             
     | 
| 
      
 44 
     | 
    
         
            +
              describe ".sign" do
         
     | 
| 
      
 45 
     | 
    
         
            +
                it "returns the signature" do
         
     | 
| 
      
 46 
     | 
    
         
            +
                  Viki::UriSigner.sign('pineapple', "it's over 9000").should == 'c838e400e8a4713af5bfe21601af9aee3a6201d7'
         
     | 
| 
      
 47 
     | 
    
         
            +
                end
         
     | 
| 
      
 48 
     | 
    
         
            +
              end
         
     | 
| 
       43 
49 
     | 
    
         
             
            end
         
     |