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.

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.rb
index 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 Spree
module Platform
class ResourceController < ::Spree::Api::V2::ResourceController
# doorkeeper scopes usage: https://github.com/doorkeeper-gem/doorkeeper/wiki/Using-Scopes
+ before_action :validate_token_client
before_action -> { doorkeeper_authorize! :read, :admin }
before_action -> { doorkeeper_authorize! :write, :admin }, if: :write_request?
@@ -80,6 +81,12 @@ module Spree
access_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.rb
def authorize_spree_user
diff --git a/api/docs/oauth/index.yml b/api/docs/oauth/index.yml
index 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: password
password: spree123
- Create Platform User Token:
+ Create Platform API User Token:
value:
+ client_id: 7ZY15L7crVZul8i3PZPrnpOkEURK7xnXEWRZdE6K39M
+ client_secret: cxMZ0tbe604qj_13hibNmc3GDsXUQfpzHt9PvweihFc
grant_type: password
password: spree123
scope: 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: admin
Refresh a Token:
value:
grant_type: refresh_token
diff --git a/api/lib/spree/api/testing_support/v2/platform_contexts.rb b/api/lib/spree/api/testing_support/v2/platform_contexts.rb
index 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' do
scopes: 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.rb
index 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 do
it_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'
+ end
end
shared_examples 'returns error when record does not exist' do
diff --git a/api/spec/requests/spree/oauth/token_spec.rb b/api/spec/requests/spree/oauth/token_spec.rb
index 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 do
expect(token.resource_owner_id).to be_nil
expect(token.resource_owner_type).to be_nil
expect(token.application).to eq(client)
+ expect(token.scopes).to eq(['admin'])
end
end
+
+ 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) }
+ end
end
context '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' do
let(: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 do
it_behaves_like 'returns a token'
- it 'creates new user token tied to application' do
+ it 'creates new user token' 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