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.

[SD-1150] Do not return access token when user has not confirmed yet (#10751)

Agnieszka Jacekce88636fa3c8
diff --git a/api/config/initializers/doorkeeper.rb b/api/config/initializers/doorkeeper.rb
index b8198255c4..b487b06a09 100644
--- a/api/config/initializers/doorkeeper.rb
+++ b/api/config/initializers/doorkeeper.rb
@@ -7,7 +7,14 @@ Doorkeeper.configure do
resource_owner_from_credentials do
user = Spree.user_class.find_for_database_authentication(email: params[:username])
- user if user&.valid_for_authentication? { user.valid_password?(params[:password]) }
+
+ return nil if user.nil?
+
+ if defined?(Spree::Auth::Config) && Spree::Auth::Config[:confirmable] == true
+ user if user.active_for_authentication? && user.valid_for_authentication? { user.valid_password?(params[:password]) }
+ elsif user&.valid_for_authentication? { user.valid_password?(params[:password]) }
+ user
+ end
end
admin_authenticator do |routes|
diff --git a/api/spec/requests/spree/oauth/token_spec.rb b/api/spec/requests/spree/oauth/token_spec.rb
index 92387038a4..537a034279 100644
--- a/api/spec/requests/spree/oauth/token_spec.rb
+++ b/api/spec/requests/spree/oauth/token_spec.rb
@@ -6,15 +6,44 @@ describe 'Spree OAuth', type: :request do
describe 'get token' do
context 'by password' do
before do
+ module Spree
+ module Auth
+ class Config
+ def self.[](key)
+ {
+ confirmable: true
+ }[key]
+ end
+
+ def self.[]=(key)
+ end
+ end
+ end
+ end
+
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(active_value)
post '/spree_oauth/token?grant_type=password&[email protected]&password=secret'
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')
+ context 'when the user is confirmed' do
+ let(:active_value) { true }
+
+ 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
+ end
+
+ context 'when the user is not confirmed' do
+ let(:active_value) { false }
+
+ it 'does not return new token' do
+ expect(response.status).to eq(400)
+ expect(json_response[:error]).to eq("invalid_grant")
+ end
end
end
end