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.

Fix: OAuth tokens on user identities are now encrypted at rest (#14708)

Damian Legawiecc08e702680a4
diff --git a/docs/developer/how-to/custom-api-authentication.mdx b/docs/developer/how-to/custom-api-authentication.mdx
index 8ff77df667..71c967d879 100644
--- a/docs/developer/how-to/custom-api-authentication.mdx
+++ b/docs/developer/how-to/custom-api-authentication.mdx
@@ -142,6 +142,10 @@ end
`find_or_create_user_from_oauth` returns the **user**, not the identity. It creates the `Spree::UserIdentity` row on first login (mapping `provider + uid → user`) and reuses it on subsequent logins — so repeat sign-ins land on the same Spree customer.
+<Note>
+Any `access_token` and `refresh_token` you pass in `tokens` are stored on the identity and encrypted at rest with Active Record encryption. Configure the encryption keys in production (`bin/rails db:encryption:init`, then store the output in your encrypted credentials). Without them, Spree stores the tokens in plain text.
+</Note>
+
## Step 2: Register the Strategy
Add the strategy to a registry in `config/initializers/spree.rb`. **This is the only line that decides which API the provider serves** — `store_authentication_strategies` for customer login, `admin_authentication_strategies` for staff login. Register with both to allow the same provider on either surface. The key you choose here is what clients send as `provider` in the login payload.
@@ -188,7 +192,7 @@ Restrict a surface to SSO by removing the built-in email/password strategy after
</Tip>
<Warning>
-`Spree::UserIdentity` validates that `provider` is a registered strategy key. Registration must happen during boot — before the first login attempt.
+`Spree::UserIdentity` validates that `provider` is a key registered in the store, admin or seller strategy registry. Registration must happen during boot — before the first login attempt.
</Warning>
## Step 3: Call the Exchange Endpoint
diff --git a/spree/core/app/models/spree/user_identity.rb b/spree/core/app/models/spree/user_identity.rb
index a68183280c..8e229466a9 100644
--- a/spree/core/app/models/spree/user_identity.rb
+++ b/spree/core/app/models/spree/user_identity.rb
@@ -9,15 +9,19 @@ module Spree
validates :provider, inclusion: {
in: lambda { |_record|
- (Spree.store_authentication_strategies.keys + Spree.admin_authentication_strategies.keys).uniq.map(&:to_s)
+ (
+ Spree.store_authentication_strategies.keys +
+ Spree.admin_authentication_strategies.keys +
+ Spree.seller_authentication_strategies.keys
+ ).uniq.map(&:to_s)
}
}
- # Store provider-specific data
- # info: JSON field with provider-specific data (name, avatar, etc)
- # access_token: encrypted OAuth access token
- # refresh_token: encrypted OAuth refresh token
- # expires_at: token expiration timestamp
+ # Keys may come from config or from encrypted credentials. Rows written before
+ # encryption was enabled stay readable and are encrypted on their next write.
+ if ActiveRecord::Encryption.config.has_primary_key?
+ encrypts :access_token, :refresh_token, support_unencrypted_data: true
+ end
# Find or create user from OAuth data
def self.find_or_create_from_oauth(provider:, uid:, info:, tokens: {}, user_class: nil)
diff --git a/spree/core/db/migrate/20260923000001_change_spree_user_identities_tokens_to_text.rb b/spree/core/db/migrate/20260923000001_change_spree_user_identities_tokens_to_text.rb
new file mode 100644
index 0000000000..9cddf4bfad
--- /dev/null
+++ b/spree/core/db/migrate/20260923000001_change_spree_user_identities_tokens_to_text.rb
@@ -0,0 +1,10 @@
+class ChangeSpreeUserIdentitiesTokensToText < ActiveRecord::Migration[8.1]
+ def up
+ change_column :spree_user_identities, :access_token, :text
+ change_column :spree_user_identities, :refresh_token, :text
+ end
+
+ def down
+ raise ActiveRecord::IrreversibleMigration, 'Encrypted tokens may not fit back into string columns'
+ end
+end
diff --git a/spree/core/lib/generators/spree/dummy/templates/rails/test.rb b/spree/core/lib/generators/spree/dummy/templates/rails/test.rb
index cd79da0d88..bef1167d8a 100644
--- a/spree/core/lib/generators/spree/dummy/templates/rails/test.rb
+++ b/spree/core/lib/generators/spree/dummy/templates/rails/test.rb
@@ -43,5 +43,12 @@ Dummy::Application.configure do
config.cache_store = :null_store
+ # Spree encrypts secrets at rest (webhook signing keys, OAuth tokens) only when
+ # Active Record encryption is configured, so the suite configures it like a
+ # production app would.
+ config.active_record.encryption.primary_key = 'spree-test-primary-key'
+ config.active_record.encryption.deterministic_key = 'spree-test-deterministic-key'
+ config.active_record.encryption.key_derivation_salt = 'spree-test-key-derivation-salt'
+
routes.default_url_options = { host: 'localhost', port: 3000 }
end
diff --git a/spree/core/spec/models/spree/user_identity_spec.rb b/spree/core/spec/models/spree/user_identity_spec.rb
index 4afe40f8e9..4ab27cebe2 100644
--- a/spree/core/spec/models/spree/user_identity_spec.rb
+++ b/spree/core/spec/models/spree/user_identity_spec.rb
@@ -10,6 +10,13 @@ describe Spree::UserIdentity, type: :model do
expect(identity.errors[:provider]).to include('is not included in the list')
end
+ it 'accepts a provider registered only for the Seller API' do
+ Spree.seller_authentication_strategies.add(:seller_sso, Class.new)
+ expect(build(:user_identity, provider: 'seller_sso')).to be_valid
+ ensure
+ Spree.seller_authentication_strategies.remove(:seller_sso)
+ end
+
describe 'uniqueness validation' do
let(:user) { create(:user) }
let!(:existing_identity) do
@@ -39,6 +46,40 @@ describe Spree::UserIdentity, type: :model do
end
end
+ describe 'token encryption' do
+ let(:identity) do
+ create(:user_identity, access_token: 'plain-access-token', refresh_token: 'plain-refresh-token')
+ end
+
+ it 'stores the OAuth tokens encrypted' do
+ raw = described_class.connection.select_one(
+ described_class.where(id: identity.id).select(:access_token, :refresh_token).to_sql
+ )
+
+ expect(raw['access_token']).to be_present
+ expect(raw['access_token']).not_to include('plain-access-token')
+ expect(raw['refresh_token']).to be_present
+ expect(raw['refresh_token']).not_to include('plain-refresh-token')
+ end
+
+ it 'reads the OAuth tokens back decrypted' do
+ reloaded = described_class.find(identity.id)
+
+ expect(reloaded.access_token).to eq('plain-access-token')
+ expect(reloaded.refresh_token).to eq('plain-refresh-token')
+ end
+
+ it 'still reads tokens stored before encryption was enabled' do
+ described_class.where(id: identity.id)
+ .update_all("access_token = 'legacy-access-token', refresh_token = 'legacy-refresh-token'")
+
+ reloaded = described_class.find(identity.id)
+
+ expect(reloaded.access_token).to eq('legacy-access-token')
+ expect(reloaded.refresh_token).to eq('legacy-refresh-token')
+ end
+ end
+
describe '.find_or_create_from_oauth' do
let(:provider) { 'email' }
let(:uid) { '123456789' }