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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Added OAuth 2.1 tables and models
Damian Legawiecd4e09a6c8471
diff --git a/spree/core/app/models/spree/oauth_access_grant.rb b/spree/core/app/models/spree/oauth_access_grant.rbnew file mode 100644index 0000000000..c11616daf3--- /dev/null+++ b/spree/core/app/models/spree/oauth_access_grant.rb@@ -0,0 +1,11 @@+module Spree+ class OauthAccessGrant < Spree.base_class+ include ::Doorkeeper::Orm::ActiveRecord::Mixins::AccessGrant++ self.table_name = 'spree_oauth_access_grants'++ belongs_to :application,+ class_name: 'Spree::OauthApplication',+ optional: false+ end+enddiff --git a/spree/core/app/models/spree/oauth_access_token.rb b/spree/core/app/models/spree/oauth_access_token.rbnew file mode 100644index 0000000000..968d907fef--- /dev/null+++ b/spree/core/app/models/spree/oauth_access_token.rb@@ -0,0 +1,16 @@+module Spree+ # The credential a client sends. Its resource owner is a real user, so a+ # call carries that person's own permissions resolved fresh on every+ # request — narrowing a role or deleting the user takes effect immediately,+ # without touching the token. This is what an API key cannot do: a key is a+ # standing grant with nobody behind it.+ class OauthAccessToken < Spree.base_class+ include ::Doorkeeper::Orm::ActiveRecord::Mixins::AccessToken++ self.table_name = 'spree_oauth_access_tokens'++ belongs_to :application,+ class_name: 'Spree::OauthApplication',+ optional: true+ end+enddiff --git a/spree/core/app/models/spree/oauth_application.rb b/spree/core/app/models/spree/oauth_application.rbnew file mode 100644index 0000000000..150eb45d8a--- /dev/null+++ b/spree/core/app/models/spree/oauth_application.rb@@ -0,0 +1,31 @@+module Spree+ # A registered OAuth client: an MCP client today, a marketplace app or a+ # merchant-approved integration later. Clients are pre-registered rather+ # than dynamically registered — RFC 7591 is deprecated, and the consumer+ # clients worth supporting need no registration endpoint.+ class OauthApplication < Spree.base_class+ include ::Doorkeeper::Orm::ActiveRecord::Mixins::Application++ self.table_name = 'spree_oauth_applications'++ has_prefix_id :oauthapp++ belongs_to :store, class_name: 'Spree::Store'++ has_many :access_grants,+ class_name: 'Spree::OauthAccessGrant',+ foreign_key: :application_id,+ dependent: :destroy+ has_many :access_tokens,+ class_name: 'Spree::OauthAccessToken',+ foreign_key: :application_id,+ dependent: :destroy++ # When someone last used this application, for the connections screen.+ #+ # @return [ActiveSupport::TimeWithZone, nil]+ def last_used_at+ access_tokens.order(:created_at).last&.created_at+ end+ end+enddiff --git a/spree/core/app/models/spree/store.rb b/spree/core/app/models/spree/store.rbindex a724bb632d..940ff0fba5 100644--- a/spree/core/app/models/spree/store.rb+++ b/spree/core/app/models/spree/store.rb@@ -339,6 +339,7 @@ module Spreehas_many :catalogs, class_name: 'Spree::Catalog', dependent: :destroy, inverse_of: :storehas_many :api_keys, class_name: 'Spree::ApiKey', dependent: :destroy+ has_many :oauth_applications, class_name: 'Spree::OauthApplication', dependent: :destroyhas_many :allowed_origins, class_name: 'Spree::AllowedOrigin', dependent: :destroy#diff --git a/spree/core/db/migrate/20261005120000_create_spree_oauth_tables.rb b/spree/core/db/migrate/20261005120000_create_spree_oauth_tables.rbnew file mode 100644index 0000000000..ced21fb212--- /dev/null+++ b/spree/core/db/migrate/20261005120000_create_spree_oauth_tables.rb@@ -0,0 +1,66 @@+# OAuth 2.1 authorization server tables. Separate from any OAuth Spree+# carried before 5.4: those served password and client-credentials grants for+# the legacy API, and nothing migrates forward.+class CreateSpreeOauthTables < ActiveRecord::Migration[8.1]+ def change+ create_table :spree_oauth_applications do |t|+ t.string :name, null: false+ t.string :uid, null: false+ # Null for a public client, which is every MCP client: they run in a+ # browser or on a vendor's server and cannot hold a secret, which is+ # why PKCE is mandatory rather than optional here.+ t.string :secret+ t.text :redirect_uri+ t.string :scopes, null: false, default: ''+ t.boolean :confidential, null: false, default: false+ t.references :store, null: false, index: true+ t.timestamps+ end++ add_index :spree_oauth_applications, :uid, unique: true++ create_table :spree_oauth_access_grants do |t|+ t.references :resource_owner, polymorphic: true, null: false, index: true+ t.references :application, null: false, index: true+ t.string :token, null: false+ t.integer :expires_in, null: false+ t.text :redirect_uri+ t.string :scopes, null: false, default: ''+ t.datetime :revoked_at+ t.string :code_challenge+ t.string :code_challenge_method+ # RFC 8707: what the grant was requested for, carried to the token so+ # the audience can be checked on every call. The column name is+ # Doorkeeper's — its `resource_indicators_supported?` detects the+ # feature by this column's presence.+ t.text :resource+ t.timestamps+ end++ add_index :spree_oauth_access_grants, :token, unique: true++ create_table :spree_oauth_access_tokens do |t|+ t.references :resource_owner, polymorphic: true, index: true+ t.references :application, index: true+ # Text rather than string: tokens are hashed, and a hash outgrows 255+ # characters under some configurations.+ t.text :token, null: false+ t.text :refresh_token+ t.integer :expires_in+ t.datetime :revoked_at+ t.string :scopes+ t.string :previous_refresh_token, null: false, default: ''+ t.text :resource+ t.timestamps+ end++ add_index :spree_oauth_access_tokens, :refresh_token, unique: true+ # Hashed tokens are long, so the lookup index covers a prefix on MySQL,+ # where a full-length text index is rejected.+ if connection.adapter_name.match?(/mysql/i)+ add_index :spree_oauth_access_tokens, :token, unique: true, length: 255+ else+ add_index :spree_oauth_access_tokens, :token, unique: true+ end+ end+enddiff --git a/spree/core/lib/spree/core.rb b/spree/core/lib/spree/core.rbindex 83ac93846d..12f3036dbb 100644--- a/spree/core/lib/spree/core.rb+++ b/spree/core/lib/spree/core.rb@@ -15,6 +15,10 @@ require 'alba'require 'acts-as-taggable-on'require 'awesome_nested_set'require 'cancan'+# The OAuth models include Doorkeeper's mixins, so the gem has to be loaded+# before they are autoloaded — core owns the tables, api owns the server+# configuration.+require 'doorkeeper'require 'countries/global'require 'friendly_id'require 'json_schemer'diff --git a/spree/core/spree_core.gemspec b/spree/core/spree_core.gemspecindex f8d119747f..0fcba06a41 100644--- a/spree/core/spree_core.gemspec+++ b/spree/core/spree_core.gemspec@@ -40,6 +40,12 @@ Gem::Specification.new do |s|s.add_dependency 'benchmark' # needed for ruby 4.0 and benchmark gems.add_dependency 'cancancan', '~> 3.2's.add_dependency 'countries'+ # OAuth 2.1 authorization server. Pinned to a release candidate because it+ # is the first version carrying RFC 8707 resource indicators, which the MCP+ # authorization spec requires and which cannot be added from outside — the+ # audience has to be bound when the token is issued. Relax to '~> 6.0' once+ # 6.0.0 is final.+ s.add_dependency 'doorkeeper', '~> 6.0.0.rc2's.add_dependency 'friendly_id', '~> 5.2', '>= 5.2.1's.add_dependency 'geocoder's.add_dependency 'highline', '>= 2', '< 4' # Necessary for the install generator