Browse Source

Avoid depending on ActiveSupport (#818)

Andy Waite 3 years ago
parent
commit
72e2b6c944
2 changed files with 7 additions and 2 deletions
  1. 4 1
      lib/shopify_api/session.rb
  2. 3 1
      test/session_test.rb

+ 4 - 1
lib/shopify_api/session.rb

@@ -7,6 +7,8 @@ module ShopifyAPI
   end
 
   class Session
+    SECONDS_IN_A_DAY = 24 * 60 * 60
+
     cattr_accessor :api_key, :secret, :myshopify_domain
     self.myshopify_domain = 'myshopify.com'
 
@@ -106,7 +108,8 @@ module ShopifyAPI
     def request_token(params)
       return token if token
 
-      unless self.class.validate_signature(params) && params[:timestamp].to_i > 24.hours.ago.utc.to_i
+      twenty_four_hours_ago = Time.now.utc.to_i - SECONDS_IN_A_DAY
+      unless self.class.validate_signature(params) && params[:timestamp].to_i > twenty_four_hours_ago
         raise ShopifyAPI::ValidationException, "Invalid Signature: Possible malicious login"
       end
 

+ 3 - 1
test/session_test.rb

@@ -3,6 +3,8 @@ require 'test_helper'
 require 'timecop'
 
 class SessionTest < Test::Unit::TestCase
+  SECONDS_IN_A_DAY = 24 * 60 * 60
+
   def setup
     super
     ShopifyAPI::Session.secret = 'secret'
@@ -373,7 +375,7 @@ class SessionTest < Test::Unit::TestCase
   end
 
   test "raise error if timestamp is too old" do
-    params = { code: "any-code", timestamp: Time.now - 2.days }
+    params = { code: "any-code", timestamp: Time.now - 2 * SECONDS_IN_A_DAY }
     signature = generate_signature(params)
     params[:foo] = 'world'
     assert_raises(ShopifyAPI::ValidationException) do