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.

Adds encryption for app secrets and access tokens

Damian Legawiec7d193c2b42cd
diff --git a/api/config/initializers/doorkeeper.rb b/api/config/initializers/doorkeeper.rb
index c5ef568ca7..a12643b3b4 100644
--- a/api/config/initializers/doorkeeper.rb
+++ b/api/config/initializers/doorkeeper.rb
@@ -41,4 +41,8 @@ Doorkeeper.configure do
access_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'
end
diff --git a/api/lib/spree/api/testing_support/factories/oauth_application_factory.rb b/api/lib/spree/api/testing_support/factories/oauth_application_factory.rb
index 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 do
factory :oauth_application, class: Spree::OauthApplication do
name { "Admin Panel" }
scopes { "admin" }
- uid { "admin_panel" }
- secret { "secret" }
end
end
diff --git a/api/spec/models/spree/oauth_application_spec.rb b/api/spec/models/spree/oauth_application_spec.rb
index 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 Spree
describe OauthApplication, type: :model do
@@ -7,5 +8,21 @@ module Spree
it 'creates a valid oauth application' do
expect(subject).to be_valid
end
+
+ 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)
+ end
end
end
diff --git a/api/spec/requests/spree/oauth/token_spec.rb b/api/spec/requests/spree/oauth/token_spec.rb
index 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 do
let!(: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' do
shared_examples 'does not return a token' do
@@ -11,7 +15,71 @@ describe 'Spree OAuth', type: :request do
end
end
+ 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' do
before do
module Spree
@@ -37,18 +105,13 @@ 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
+ it 'creates new user 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)
- 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'
end
context 'when the user is not confirmed' do
diff --git a/api/spree_api.gemspec b/api/spree_api.gemspec
index 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'