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
Do not allow access to Platform API for tokens without client associated
Damian Legawiec01409cee5726
diff --git a/api/app/controllers/spree/api/v2/platform/resource_controller.rb b/api/app/controllers/spree/api/v2/platform/resource_controller.rbindex 6877c1e30e..c93d34489c 100644--- a/api/app/controllers/spree/api/v2/platform/resource_controller.rb+++ b/api/app/controllers/spree/api/v2/platform/resource_controller.rb@@ -4,6 +4,7 @@ module Spreemodule Platformclass ResourceController < ::Spree::Api::V2::ResourceController# doorkeeper scopes usage: https://github.com/doorkeeper-gem/doorkeeper/wiki/Using-Scopes+ before_action :validate_token_clientbefore_action -> { doorkeeper_authorize! :read, :admin }before_action -> { doorkeeper_authorize! :write, :admin }, if: :write_request?@@ -80,6 +81,12 @@ module Spreeaccess_denied_401(exception)end+ def validate_token_client+ return if doorkeeper_token.nil?++ raise Doorkeeper::Errors::DoorkeeperError if doorkeeper_token.application.nil?+ end+# if using a user oAuth token we need to check CanCanCan abilities# defined in https://github.com/spree/spree/blob/master/core/app/models/spree/ability.rbdef authorize_spree_userdiff --git a/api/docs/oauth/index.yml b/api/docs/oauth/index.ymlindex a342ed0845..b558874078 100644--- a/api/docs/oauth/index.yml+++ b/api/docs/oauth/index.yml@@ -63,22 +63,25 @@ paths:- $ref: '#/components/schemas/ClientTokenBody'- $ref: '#/components/schemas/RefreshTokenBody'examples:- Create Storefront User Token:+ Create Storefront API User Token:value:grant_type: passwordusername: [email protected]password: spree123- Create Platform User Token:+ Create Platform API User Token:value:+ client_id: 7ZY15L7crVZul8i3PZPrnpOkEURK7xnXEWRZdE6K39M+ client_secret: cxMZ0tbe604qj_13hibNmc3GDsXUQfpzHt9PvweihFcgrant_type: passwordusername: [email protected]password: spree123scope: admin- Create a Token Using Client Credentials:+ Create Platform API Application Token:value:grant_type: client_credentials- client_id: qJDIwX00fehqHxS6xmb-k- client_secret: z6rgMlsl-mbygJ11vjA+ client_id: 7ZY15L7crVZul8i3PZPrnpOkEURK7xnXEWRZdE6K39M+ client_secret: cxMZ0tbe604qj_13hibNmc3GDsXUQfpzHt9PvweihFc+ scope: adminRefresh a Token:value:grant_type: refresh_tokendiff --git a/api/lib/spree/api/testing_support/v2/platform_contexts.rb b/api/lib/spree/api/testing_support/v2/platform_contexts.rbindex 6cc22ba6d1..d5893fad39 100644--- a/api/lib/spree/api/testing_support/v2/platform_contexts.rb+++ b/api/lib/spree/api/testing_support/v2/platform_contexts.rb@@ -31,10 +31,18 @@ shared_context 'Platform API v2' doscopes: admin_app.scopes)end+ let(:user_oauth_token_without_app) do+ Spree::OauthAccessToken.create!(+ resource_owner: user,+ scopes: 'admin'+ )+ end+let(:valid_authorization) { "Bearer #{oauth_token.token}" }let(:valid_read_authorization) { "Bearer #{read_oauth_token.token}" }let(:valid_user_authorization) { "Bearer #{user_oauth_token.token}" }+ let(:valid_user_authorization_without_app) { "Bearer #{user_oauth_token_without_app.token}" }let(:bogus_authorization) { "Bearer #{::Base64.strict_encode64('bogus:bogus')}" }let(:Authorization) { valid_authorization }diff --git a/api/spec/requests/spree/api/v2/platform/resources_spec.rb b/api/spec/requests/spree/api/v2/platform/resources_spec.rbindex b0682bf62f..933ed100f3 100644--- a/api/spec/requests/spree/api/v2/platform/resources_spec.rb+++ b/api/spec/requests/spree/api/v2/platform/resources_spec.rb@@ -34,6 +34,14 @@ describe 'Platform API v2 Resources spec', type: :request doit_behaves_like 'returns 401 HTTP status'end++ context 'with token not associated with an client application' do+ let(:bearer_token) { { 'Authorization' => valid_user_authorization_without_app } }++ before { execute }++ it_behaves_like 'returns 401 HTTP status'+ endendshared_examples 'returns error when record does not exist' dodiff --git a/api/spec/requests/spree/oauth/token_spec.rb b/api/spec/requests/spree/oauth/token_spec.rbindex a0d678fca8..8286d92902 100644--- a/api/spec/requests/spree/oauth/token_spec.rb+++ b/api/spec/requests/spree/oauth/token_spec.rb@@ -35,7 +35,8 @@ describe 'Spree OAuth', type: :request do{client_id: client.uid,client_secret: client_secret,- grant_type: 'client_credentials'+ grant_type: 'client_credentials',+ scope: 'admin'}end@@ -47,20 +48,80 @@ describe 'Spree OAuth', type: :request doexpect(token.resource_owner_id).to be_nilexpect(token.resource_owner_type).to be_nilexpect(token.application).to eq(client)+ expect(token.scopes).to eq(['admin'])endend++ context 'without client secret' do+ let(:params) do+ {+ client_id: client.uid,+ grant_type: 'client_credentials'+ }+ end++ before { post '/spree_oauth/token', params: params }++ it { expect(response.status).to eq(401) }+ endendcontext 'by password' do- context 'with client id & secret passed' do+ context 'with client' do+ before do+ allow(Spree.user_class).to receive(:find_for_database_authentication).with(hash_including(:email)) { user }+ allow(user).to receive(:valid_for_authentication?).and_return(true)+ allow(user).to receive(:active_for_authentication?).and_return(true)+ end++ context 'with valid credentials' do+ let(:params) do+ {+ client_id: client.uid,+ client_secret: client_secret,+ grant_type: 'password',+ username: user.email,+ password: 'secret',+ scope: 'admin'+ }+ end++ before { post '/spree_oauth/token', params: params }++ it_behaves_like 'returns a token'++ it 'creates new user token tied to application' do+ expect(token.resource_owner_id).to eq(user.id)+ expect(token.resource_owner_type).to eq('Spree::LegacyUser')+ expect(token.application).to eq(client)+ expect(token.scopes).to eq(['admin'])+ end+ end++ context 'with invalid client credentials' do+ let(:params) do+ {+ client_id: client.uid,+ grant_type: 'password',+ username: user.email,+ password: 'secret',+ scope: 'admin'+ }+ end++ before { post '/spree_oauth/token', params: params }++ it { expect(response.status).to eq(401) }+ end+ end++ context 'without client, with scopes' dolet(:params) do{- client_id: client.uid,- client_secret: client_secret,grant_type: 'password',username: user.email,password: 'secret',- scopes: 'admin'+ scope: 'admin'}end@@ -73,10 +134,10 @@ describe 'Spree OAuth', type: :request doit_behaves_like 'returns a token'- it 'creates new user token tied to application' do+ it 'creates new user token' doexpect(token.resource_owner_id).to eq(user.id)expect(token.resource_owner_type).to eq('Spree::LegacyUser')- expect(token.application).to eq(client)+ expect(token.scopes).to eq(['admin'])endend