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: every connection failed with "The requested scope is invalid"

Damian Legawiec26cf18fa211d
diff --git a/spree/api/app/controllers/spree/api/v3/admin/oauth/authorizations_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/oauth/authorizations_controller.rb
index c00fb2d374..8801d38b83 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/oauth/authorizations_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/oauth/authorizations_controller.rb
@@ -159,11 +159,27 @@ module Spree
:client_id, :redirect_uri, :response_type, :state, :scope,
:code_challenge, :code_challenge_method, :response_mode
).to_h.symbolize_keys.
+ merge(scope: known_scopes).
# Carried through raw: RFC 8707 allows one value or several,
# and Doorkeeper's own validator normalizes both shapes.
merge(resource: params[:resource])
end
+ # The requested scopes this server knows, with the rest dropped.
+ #
+ # RFC 6749 §3.3 lets an authorization server ignore part of a
+ # requested scope, and the consent screen narrows to what the
+ # approver holds regardless. Refusing the whole request instead
+ # fails a client that cached an older catalogue — and the MCP
+ # spec tells a client to ask for everything it was once told
+ # about, so a scope we stop offering would lock out every client
+ # that discovered us before.
+ def known_scopes
+ accepted = Spree::Api::Oauth.staff_scope_keys.map(&:to_s)
+
+ params[:scope].to_s.split.select { |scope| accepted.include?(scope) }.join(' ')
+ end
+
def requested_resource
@pre_auth.resource_indicators&.first
end
diff --git a/spree/api/app/controllers/spree/api/v3/oauth/metadata_controller.rb b/spree/api/app/controllers/spree/api/v3/oauth/metadata_controller.rb
index 78978b8bea..5d164d2603 100644
--- a/spree/api/app/controllers/spree/api/v3/oauth/metadata_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/oauth/metadata_controller.rb
@@ -29,11 +29,14 @@ module Spree
render json: {
resource: resource,
authorization_servers: [issuer],
- # Staff keys only: a grant acts for an admin user, so a
- # seller-only key could never be exercised through it.
- scopes_supported: Spree.permissions.grantable_keys(
- Spree::PermissionConfiguration::STAFF_AUDIENCE
- ),
+ # Exactly what the authorization server will accept.
+ #
+ # The spec tells a client with no scope challenge to request
+ # everything advertised here, and Doorkeeper refuses a request
+ # naming a scope it does not know — so advertising one more
+ # than it accepts turns every connection into "The requested
+ # scope is invalid".
+ scopes_supported: Spree::Api::Oauth.staff_scope_keys,
bearer_methods_supported: ['header'],
resource_documentation: 'https://spreecommerce.org/docs/developer/agentic/admin-mcp'
}
diff --git a/spree/api/spec/requests/spree/api/v3/admin/oauth/authorizations_spec.rb b/spree/api/spec/requests/spree/api/v3/admin/oauth/authorizations_spec.rb
index 51550e7ef6..1e8020f883 100644
--- a/spree/api/spec/requests/spree/api/v3/admin/oauth/authorizations_spec.rb
+++ b/spree/api/spec/requests/spree/api/v3/admin/oauth/authorizations_spec.rb
@@ -94,6 +94,21 @@ RSpec.describe 'Admin OAuth consent', type: :request do
expect(response).to have_http_status(:forbidden)
end
+ # A scope governing credentials rather than commerce is not offered, and
+ # the MCP spec tells a client to request everything it was once told
+ # about — so refusing the whole request would lock out any client that
+ # discovered the catalogue before a scope was withdrawn.
+ describe 'a scope this server does not offer' do
+ it 'is dropped rather than failing the request' do
+ admin = create(:admin_user)
+
+ approve(admin, scope: 'read_products write_api_keys read_staff')
+
+ expect(response).to have_http_status(:ok)
+ expect(Spree::OauthAccessGrant.order(:id).last.scopes.to_a).to contain_exactly('read_products')
+ end
+ end
+
# The merchant may hand over less than was asked for; that choice has to
# survive, or the checkboxes are decoration.
it 'honours a narrower selection than the client requested' do