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
Adds encryption for app secrets and access tokens
Damian Legawiec7d193c2b42cd
diff --git a/api/config/initializers/doorkeeper.rb b/api/config/initializers/doorkeeper.rbindex c5ef568ca7..a12643b3b4 100644--- a/api/config/initializers/doorkeeper.rb+++ b/api/config/initializers/doorkeeper.rb@@ -41,4 +41,8 @@ Doorkeeper.configure doaccess_token_class 'Spree::OauthAccessToken'access_grant_class 'Spree::OauthAccessGrant'application_class 'Spree::OauthApplication'++ # using Bcrupt for token secrets is currently not supported by Doorkeeper+ hash_token_secrets fallback: :plain+ hash_application_secrets fallback: :plain, using: '::Doorkeeper::SecretStoring::BCrypt'enddiff --git a/api/lib/spree/api/testing_support/factories/oauth_application_factory.rb b/api/lib/spree/api/testing_support/factories/oauth_application_factory.rbindex 782412115a..74d3177e55 100644--- a/api/lib/spree/api/testing_support/factories/oauth_application_factory.rb+++ b/api/lib/spree/api/testing_support/factories/oauth_application_factory.rb@@ -2,7 +2,5 @@ FactoryBot.define dofactory :oauth_application, class: Spree::OauthApplication doname { "Admin Panel" }scopes { "admin" }- uid { "admin_panel" }- secret { "secret" }endenddiff --git a/api/spec/models/spree/oauth_application_spec.rb b/api/spec/models/spree/oauth_application_spec.rbindex 1fd51edd3e..7c1c99a873 100644--- a/api/spec/models/spree/oauth_application_spec.rb+++ b/api/spec/models/spree/oauth_application_spec.rb@@ -1,4 +1,5 @@require 'spec_helper'+require 'bcrypt'module Spreedescribe OauthApplication, type: :model do@@ -7,5 +8,21 @@ module Spreeit 'creates a valid oauth application' doexpect(subject).to be_validend++ it 'assigns client id and secret' do+ expect { subject.save! }.to change(subject, :uid).and change(subject, :secret)+ end++ it 'can access plain text secret only once after save' do+ subject.save!+ expect(subject.plaintext_secret).to be_present+ expect(described_class.last.plaintext_secret).to be_nil+ end++ it 'encrypts secret' do+ subject.save!+ expect(subject.secret).not_to eq(subject.plaintext_secret)+ expect(BCrypt::Password.new(subject.secret)).to eq(subject.plaintext_secret)+ endendenddiff --git a/api/spec/requests/spree/oauth/token_spec.rb b/api/spec/requests/spree/oauth/token_spec.rbindex b92e793c10..a0d678fca8 100644--- a/api/spec/requests/spree/oauth/token_spec.rb+++ b/api/spec/requests/spree/oauth/token_spec.rb@@ -1,7 +1,11 @@require 'spec_helper'+require 'digest'describe 'Spree OAuth', type: :request dolet!(:user) { create(:user, email: '[email protected]', password: 'secret', password_confirmation: 'secret') }+ let(:token) { Spree::OauthAccessToken.active_for(user).last }+ let(:client_secret) { 'secret' }+ let(:client) { create(:oauth_application, secret: client_secret) }describe 'get token' doshared_examples 'does not return a token' do@@ -11,7 +15,71 @@ describe 'Spree OAuth', type: :request doendend+ shared_examples 'returns a token' do+ it 'with all required attributes' do+ expect(response.status).to eq(200)+ expect(json_response).to have_attributes(%w[access_token token_type expires_in created_at])+ expect(json_response['token_type']).to eq('Bearer')+ end++ it 'returns non encrypted token and stores encrypted version in the database' do+ expect(Digest::SHA256.hexdigest(json_response['access_token'])).to eq(token.token)+ end+ end++ context 'with client credentials' do+ context 'with valid credentials' do+ let(:token) { Spree::OauthAccessToken.where(application: client).last }++ let(:params) do+ {+ client_id: client.uid,+ client_secret: client_secret,+ grant_type: 'client_credentials'+ }+ end++ before { post '/spree_oauth/token', params: params }++ it_behaves_like 'returns a token'++ it 'creates new application token' do+ expect(token.resource_owner_id).to be_nil+ expect(token.resource_owner_type).to be_nil+ expect(token.application).to eq(client)+ end+ end+ end+context 'by password' do+ context 'with client id & secret passed' do+ let(:params) do+ {+ client_id: client.uid,+ client_secret: client_secret,+ grant_type: 'password',+ username: user.email,+ password: 'secret',+ scopes: 'admin'+ }+ end++ 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)+ post '/spree_oauth/token', params: params+ end++ 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)+ end+ end+context 'with confirmation' dobefore domodule Spree@@ -37,18 +105,13 @@ describe 'Spree OAuth', type: :request docontext 'when the user is confirmed' dolet(:active_value) { true }- let(:token) { Spree::OauthAccessToken.active_for(user).last }- it 'creates new token' do+ it 'creates new user token' doexpect(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)- expect(json_response).to have_attributes([:access_token, :token_type, :expires_in, :refresh_token, :created_at])- expect(json_response['token_type']).to eq('Bearer')- end+ it_behaves_like 'returns a token'endcontext 'when the user is not confirmed' dodiff --git a/api/spree_api.gemspec b/api/spree_api.gemspecindex 8be4fdb742..a0f12d4646 100644--- a/api/spree_api.gemspec+++ b/api/spree_api.gemspec@@ -28,6 +28,7 @@ Gem::Specification.new do |s|s.add_development_dependency 'multi_json's.add_development_dependency 'rswag-specs'+ s.add_dependency 'bcrypt', '~> 3.1's.add_dependency 'doorkeeper', '~> 5.3's.add_dependency 'jsonapi-serializer', '~> 2.1's.add_dependency 'rabl', '~> 0.14', '>= 0.14.2'