Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fixed: improve persisting when API key is used (#13676)

Damian Legawiecd1d2eeee246f
diff --git a/spree/api/app/controllers/concerns/spree/api/v3/api_key_authentication.rb b/spree/api/app/controllers/concerns/spree/api/v3/api_key_authentication.rb
index 0c0f580610..2ae1e54480 100644
--- a/spree/api/app/controllers/concerns/spree/api/v3/api_key_authentication.rb
+++ b/spree/api/app/controllers/concerns/spree/api/v3/api_key_authentication.rb
@@ -27,7 +27,7 @@ module Spree
return false
end
- Spree::ApiKeyTouchJob.perform_later(@current_api_key.id)
+ touch_api_key_if_needed(@current_api_key)
true
end
@@ -49,12 +49,21 @@ module Spree
return false
end
- Spree::ApiKeyTouchJob.perform_later(@current_api_key.id)
+ touch_api_key_if_needed(@current_api_key)
true
end
private
+ # Marks the API key as used at most once per hour
+ # to avoid unnecessary DB writes and job queue pressure on every request.
+ # This follows the same approach as GitHub's personal access tokens.
+ def touch_api_key_if_needed(api_key)
+ return if api_key.last_used_at.present? && api_key.last_used_at > 1.hour.ago
+
+ Spree::ApiKeys::MarkAsUsed.perform_later(api_key.id, Time.current)
+ end
+
# Extracts the API key from the request headers or params.
#
# @return [String, nil] the API key token
diff --git a/spree/api/app/jobs/spree/api_keys/mark_as_used.rb b/spree/api/app/jobs/spree/api_keys/mark_as_used.rb
new file mode 100644
index 0000000000..772f378d07
--- /dev/null
+++ b/spree/api/app/jobs/spree/api_keys/mark_as_used.rb
@@ -0,0 +1,15 @@
+module Spree
+ module ApiKeys
+ class MarkAsUsed < Spree::BaseJob
+ queue_as Spree.queues.api_keys
+
+ def perform(api_key_id, used_at)
+ api_key = Spree::ApiKey.find_by(id: api_key_id)
+ return if api_key.nil?
+ return if api_key.last_used_at.present? && api_key.last_used_at >= used_at
+
+ api_key.update_column(:last_used_at, used_at)
+ end
+ end
+ end
+end
diff --git a/spree/api/spec/controllers/concerns/spree/api/v3/api_key_authentication_spec.rb b/spree/api/spec/controllers/concerns/spree/api/v3/api_key_authentication_spec.rb
new file mode 100644
index 0000000000..c78a8b2404
--- /dev/null
+++ b/spree/api/spec/controllers/concerns/spree/api/v3/api_key_authentication_spec.rb
@@ -0,0 +1,47 @@
+require 'spec_helper'
+
+RSpec.describe 'API Key Authentication Touch Throttling', type: :controller do
+ describe Spree::Api::V3::Store::StoresController do
+ controller(Spree::Api::V3::Store::StoresController) {}
+
+ render_views
+
+ include_context 'API v3 Store'
+
+ describe 'touch throttling for publishable keys' do
+ before do
+ request.headers['X-Spree-Api-Key'] = api_key.token
+ end
+
+ context 'when last_used_at is nil' do
+ before { api_key.update_column(:last_used_at, nil) }
+
+ it 'enqueues the mark as used job' do
+ expect {
+ get :show, params: { id: store.prefixed_id }
+ }.to have_enqueued_job(Spree::ApiKeys::MarkAsUsed).with(api_key.id, instance_of(ActiveSupport::TimeWithZone))
+ end
+ end
+
+ context 'when last_used_at is more than 1 hour ago' do
+ before { api_key.update_column(:last_used_at, 2.hours.ago) }
+
+ it 'enqueues the mark as used job' do
+ expect {
+ get :show, params: { id: store.prefixed_id }
+ }.to have_enqueued_job(Spree::ApiKeys::MarkAsUsed).with(api_key.id, instance_of(ActiveSupport::TimeWithZone))
+ end
+ end
+
+ context 'when last_used_at is less than 1 hour ago' do
+ before { api_key.update_column(:last_used_at, 30.minutes.ago) }
+
+ it 'does not enqueue the mark as used job' do
+ expect {
+ get :show, params: { id: store.prefixed_id }
+ }.not_to have_enqueued_job(Spree::ApiKeys::MarkAsUsed)
+ end
+ end
+ end
+ end
+end
diff --git a/spree/api/spec/jobs/spree/api_keys/mark_as_used_spec.rb b/spree/api/spec/jobs/spree/api_keys/mark_as_used_spec.rb
new file mode 100644
index 0000000000..3380c32a08
--- /dev/null
+++ b/spree/api/spec/jobs/spree/api_keys/mark_as_used_spec.rb
@@ -0,0 +1,41 @@
+require 'spec_helper'
+
+RSpec.describe Spree::ApiKeys::MarkAsUsed, type: :job do
+ let(:store) { create(:store) }
+ let(:api_key) { create(:api_key, store: store) }
+
+ describe '#perform' do
+ let(:used_at) { Time.current }
+
+ it 'updates last_used_at timestamp' do
+ expect {
+ described_class.new.perform(api_key.id, used_at)
+ }.to change { api_key.reload.last_used_at }
+ end
+
+ it 'sets last_used_at to the provided timestamp' do
+ used_at = Time.zone.parse('2025-06-15 12:00:00')
+ described_class.new.perform(api_key.id, used_at)
+ expect(api_key.reload.last_used_at).to eq(used_at)
+ end
+
+ it 'does not overwrite a newer last_used_at' do
+ newer_time = 1.hour.from_now
+ api_key.update_column(:last_used_at, newer_time)
+
+ expect {
+ described_class.new.perform(api_key.id, 2.hours.ago)
+ }.not_to change { api_key.reload.last_used_at }
+ end
+
+ it 'does nothing for non-existent api key' do
+ expect {
+ described_class.new.perform(999999, used_at)
+ }.not_to raise_error
+ end
+
+ it 'uses the api_keys queue' do
+ expect(described_class.new.queue_name).to eq(Spree.queues.api_keys.to_s)
+ end
+ end
+end
diff --git a/spree/core/app/jobs/spree/api_key_touch_job.rb b/spree/core/app/jobs/spree/api_key_touch_job.rb
deleted file mode 100644
index b1f941f272..0000000000
--- a/spree/core/app/jobs/spree/api_key_touch_job.rb
+++ /dev/null
@@ -1,9 +0,0 @@
-module Spree
- class ApiKeyTouchJob < Spree::BaseJob
- queue_as Spree.queues.api_keys
-
- def perform(api_key_id)
- Spree::ApiKey.find_by(id: api_key_id)&.touch(:last_used_at)
- end
- end
-end
diff --git a/spree/core/spec/jobs/spree/api_key_touch_job_spec.rb b/spree/core/spec/jobs/spree/api_key_touch_job_spec.rb
deleted file mode 100644
index 4eb267988b..0000000000
--- a/spree/core/spec/jobs/spree/api_key_touch_job_spec.rb
+++ /dev/null
@@ -1,32 +0,0 @@
-require 'spec_helper'
-
-RSpec.describe Spree::ApiKeyTouchJob, type: :job do
- let(:store) { create(:store) }
- let(:api_key) { create(:api_key, store: store) }
-
- describe '#perform' do
- it 'updates last_used_at timestamp' do
- expect {
- described_class.new.perform(api_key.id)
- }.to change { api_key.reload.last_used_at }
- end
-
- it 'does nothing for non-existent api key' do
- expect {
- described_class.new.perform(999999)
- }.not_to raise_error
- end
-
- it 'uses the api_keys queue' do
- expect(described_class.new.queue_name).to eq(Spree.queues.api_keys.to_s)
- end
- end
-
- describe 'enqueueing' do
- it 'enqueues the job' do
- expect {
- described_class.perform_later(api_key.id)
- }.to have_enqueued_job(described_class).with(api_key.id)
- end
- end
-end