Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Log out sessions when registering first device on behalf of user

Oliver Günther93f3de4a448a
diff --git a/modules/two_factor_authentication/app/controllers/two_factor_authentication/base_controller.rb b/modules/two_factor_authentication/app/controllers/two_factor_authentication/base_controller.rb
index 3efe381397c..418221b64c3 100644
--- a/modules/two_factor_authentication/app/controllers/two_factor_authentication/base_controller.rb
+++ b/modules/two_factor_authentication/app/controllers/two_factor_authentication/base_controller.rb
@@ -95,6 +95,7 @@ module ::TwoFactorAuthentication
end
end
+ # rubocop:disable Metrics/AbcSize
def confirm_and_save(result)
if result.success? && @device.confirm_registration_and_save
flash[:notice] = t('two_factor_authentication.devices.registration_complete')
@@ -109,6 +110,7 @@ module ::TwoFactorAuthentication
false
end
end
+ # rubocop:enable Metrics/AbcSize
def request_token_for_device(device, locals)
transmit = token_service(device).request
diff --git a/modules/two_factor_authentication/app/controllers/two_factor_authentication/users/two_factor_devices_controller.rb b/modules/two_factor_authentication/app/controllers/two_factor_authentication/users/two_factor_devices_controller.rb
index 6da675c35c1..244867bf878 100644
--- a/modules/two_factor_authentication/app/controllers/two_factor_authentication/users/two_factor_devices_controller.rb
+++ b/modules/two_factor_authentication/app/controllers/two_factor_authentication/users/two_factor_devices_controller.rb
@@ -23,8 +23,10 @@ module ::TwoFactorAuthentication
@device_type = params[:key].to_sym
@device = new_device_type! @device_type
+ has_default = ::TwoFactorAuthentication::Device.has_default?(target_user)
@device.attributes = new_device_params
if @device.confirm_registration_and_save
+ logout_other_sessions unless has_default
Rails.logger.info "Admin ##{current_user.id} registered a new device #{@device_type} for #{@user.id}."
redirect_to index_path
else
diff --git a/modules/two_factor_authentication/spec/controllers/two_factor_authentication/forced_registration/two_factor_devices_controller_spec.rb b/modules/two_factor_authentication/spec/controllers/two_factor_authentication/forced_registration/two_factor_devices_controller_spec.rb
index a68c8ca73d9..84624f6e9c0 100644
--- a/modules/two_factor_authentication/spec/controllers/two_factor_authentication/forced_registration/two_factor_devices_controller_spec.rb
+++ b/modules/two_factor_authentication/spec/controllers/two_factor_authentication/forced_registration/two_factor_devices_controller_spec.rb
@@ -186,13 +186,15 @@ describe TwoFactorAuthentication::ForcedRegistration::TwoFactorDevicesController
end
it 'activates the device when entered correctly and logs out the user' do
- allow(::Sessions::DropAllSessionsService)
+ allow(Sessions::DropAllSessionsService)
.to receive(:call)
+ # rubocop:disable RSpec/AnyInstance
allow_any_instance_of(TwoFactorAuthentication::TokenService)
.to receive(:verify)
.with('1234')
.and_return(ServiceResult.success)
+ # rubocop:enable RSpec/AnyInstance
post :confirm, params: { device_id: device.id, otp: '1234' }
expect(response).to redirect_to stage_success_path(stage: :two_factor_authentication, secret: 'asdf')
@@ -201,7 +203,7 @@ describe TwoFactorAuthentication::ForcedRegistration::TwoFactorDevicesController
expect(device.active).to be true
expect(device.default).to be true
- expect(::Sessions::DropAllSessionsService)
+ expect(Sessions::DropAllSessionsService)
.to have_received(:call).with(user)
end
end
diff --git a/modules/two_factor_authentication/spec/controllers/two_factor_authentication/my/two_factor_devices_controller_spec.rb b/modules/two_factor_authentication/spec/controllers/two_factor_authentication/my/two_factor_devices_controller_spec.rb
index 4c254b70b16..bab9fbc6748 100644
--- a/modules/two_factor_authentication/spec/controllers/two_factor_authentication/my/two_factor_devices_controller_spec.rb
+++ b/modules/two_factor_authentication/spec/controllers/two_factor_authentication/my/two_factor_devices_controller_spec.rb
@@ -202,10 +202,12 @@ describe TwoFactorAuthentication::My::TwoFactorDevicesController do
let!(:default_device) { create(:two_factor_authentication_device_totp, user:, default: true) }
it 'activates the device when entered correctly' do
+ # rubocop:disable RSpec/AnyInstance
allow_any_instance_of(TwoFactorAuthentication::TokenService)
- .to receive(:verify)
- .with('1234')
- .and_return(ServiceResult.success)
+ .to receive(:verify)
+ .with('1234')
+ .and_return(ServiceResult.success)
+ # rubocop:enable RSpec/AnyInstance
post :confirm, params: { device_id: device.id, otp: '1234' }
expect(response).to redirect_to action: :index
diff --git a/modules/two_factor_authentication/spec/controllers/two_factor_authentication/users/two_factor_devices_controller_spec.rb b/modules/two_factor_authentication/spec/controllers/two_factor_authentication/users/two_factor_devices_controller_spec.rb
index 34a16090c7e..bca221bc385 100644
--- a/modules/two_factor_authentication/spec/controllers/two_factor_authentication/users/two_factor_devices_controller_spec.rb
+++ b/modules/two_factor_authentication/spec/controllers/two_factor_authentication/users/two_factor_devices_controller_spec.rb
@@ -23,7 +23,7 @@ describe TwoFactorAuthentication::Users::TwoFactorDevicesController do
allow(User).to receive(:current).and_return(logged_in_user)
allow(OpenProject::TwoFactorAuthentication::TokenStrategyManager)
.to receive(:add_default_strategy?)
- .and_return false
+ .and_return false
end
describe 'accessing' do
@@ -93,14 +93,12 @@ describe TwoFactorAuthentication::Users::TwoFactorDevicesController do
end
describe '#register' do
- before do
- post :register, params: { id: user.id, key: :sms, device: params }
- end
-
context 'with missing phone' do
let(:params) { { identifier: 'foo' } }
it 'renders action new' do
+ post :register, params: { id: user.id, key: :sms, device: params }
+
expect(response).to be_successful
expect(response).to render_template 'new'
expect(assigns[:device]).to be_invalid
@@ -111,6 +109,8 @@ describe TwoFactorAuthentication::Users::TwoFactorDevicesController do
let(:params) { { phone_number: '+49123456789', identifier: 'foo' } }
it 'redirects to result' do
+ post :register, params: { id: user.id, key: :sms, device: params }
+
device = user.otp_devices.reload.last
expect(response).to redirect_to edit_user_path(user.id, tab: :two_factor_authentication)
@@ -119,6 +119,44 @@ describe TwoFactorAuthentication::Users::TwoFactorDevicesController do
expect(device.default).to be_truthy
expect(device.active).to be_truthy
end
+
+ context 'when user has active sessions' do
+ let!(:plain_session1) { create(:user_session, user:) }
+ let!(:user_session1) { Sessions::UserSession.find_by(session_id: plain_session1.session_id) }
+
+ let!(:plain_session2) { create(:user_session, user:) }
+ let!(:user_session2) { Sessions::UserSession.find_by(session_id: plain_session2.session_id) }
+
+ let!(:other_plain_session) { create(:user_session, user: other_user) }
+ let!(:other_session) { Sessions::UserSession.find_by(session_id: other_plain_session.session_id) }
+
+ it 'drops all sessions of that user' do
+ post :register, params: { id: user.id, key: :sms, device: params }
+
+ expect { user_session1.reload }.to raise_error(ActiveRecord::RecordNotFound)
+ expect { user_session2.reload }.to raise_error(ActiveRecord::RecordNotFound)
+
+ expect { other_session.reload }.not_to raise_error
+ end
+
+ context 'when user has an active device' do
+ let!(:device) { create(:two_factor_authentication_device_totp, user:, default: true) }
+
+ it 'does nothing' do
+ post :register, params: { id: user.id, key: :sms, device: params }
+
+ expect(user.otp_devices.count).to eq 2
+ expect(device.reload).to be_default
+ expect(user.otp_devices.last).to be_active
+ expect(user.otp_devices.last).not_to be_default
+
+ expect { user_session1.reload }.not_to raise_error
+ expect { user_session2.reload }.not_to raise_error
+
+ expect { other_session.reload }.not_to raise_error
+ end
+ end
+ end
end
end