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.

Enable polymoprhic resource owners for OAuth Access Tokens (#11350)

Damian Legawiec8c3b7ea218a1
diff --git a/api/config/initializers/doorkeeper.rb b/api/config/initializers/doorkeeper.rb
index 013613077b..b16b7020f7 100644
--- a/api/config/initializers/doorkeeper.rb
+++ b/api/config/initializers/doorkeeper.rb
@@ -4,9 +4,12 @@ Doorkeeper.configure do
api_only
base_controller 'ActionController::API'
+ # FIXME: we should only skip this for Storefront API until v5
+ # we should not skip this for Platform API
skip_client_authentication_for_password_grant { true } if defined?(skip_client_authentication_for_password_grant)
resource_owner_authenticator { current_spree_user }
+ use_polymorphic_resource_owner
resource_owner_from_credentials do
user = Spree.user_class.find_for_database_authentication(email: params[:username])
diff --git a/api/db/migrate/20210919183228_enable_polymorphic_resource_owner.rb b/api/db/migrate/20210919183228_enable_polymorphic_resource_owner.rb
new file mode 100644
index 0000000000..081b32da70
--- /dev/null
+++ b/api/db/migrate/20210919183228_enable_polymorphic_resource_owner.rb
@@ -0,0 +1,21 @@
+class EnablePolymorphicResourceOwner < ActiveRecord::Migration[5.2]
+ def change
+ add_column :spree_oauth_access_tokens, :resource_owner_type, :string
+ add_column :spree_oauth_access_grants, :resource_owner_type, :string
+ change_column_null :spree_oauth_access_grants, :resource_owner_type, false
+
+ add_index :spree_oauth_access_tokens,
+ [:resource_owner_id, :resource_owner_type],
+ name: 'polymorphic_owner_oauth_access_tokens'
+
+ add_index :spree_oauth_access_grants,
+ [:resource_owner_id, :resource_owner_type],
+ name: 'polymorphic_owner_oauth_access_grants'
+
+ Spree::OauthAccessToken.reset_column_information
+ Spree::OauthAccessToken.update_all(resource_owner_type: Spree.user_class)
+
+ Spree::OauthAccessGrant.reset_column_information
+ Spree::OauthAccessGrant.update_all(resource_owner_type: Spree.user_class)
+ end
+end
diff --git a/api/spec/requests/spree/oauth/token_spec.rb b/api/spec/requests/spree/oauth/token_spec.rb
index 62f2da8c03..b92e793c10 100644
--- a/api/spec/requests/spree/oauth/token_spec.rb
+++ b/api/spec/requests/spree/oauth/token_spec.rb
@@ -37,6 +37,12 @@ describe 'Spree OAuth', type: :request do
context 'when the user is confirmed' do
let(:active_value) { true }
+ let(:token) { Spree::OauthAccessToken.active_for(user).last }
+
+ it 'creates new token' do
+ expect(token.resource_owner_id).to eq(user.id)
+ expect(token.resource_owner_type).to eq('Spree::LegacyUser')
+ end
it 'returns new token' do
expect(response.status).to eq(200)