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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
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.rbindex 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 ::TwoFactorAuthenticationendend+ # rubocop:disable Metrics/AbcSizedef confirm_and_save(result)if result.success? && @device.confirm_registration_and_saveflash[:notice] = t('two_factor_authentication.devices.registration_complete')@@ -109,6 +110,7 @@ module ::TwoFactorAuthenticationfalseendend+ # rubocop:enable Metrics/AbcSizedef request_token_for_device(device, locals)transmit = token_service(device).requestdiff --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.rbindex 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_paramsif @device.confirm_registration_and_save+ logout_other_sessions unless has_defaultRails.logger.info "Admin ##{current_user.id} registered a new device #{@device_type} for #{@user.id}."redirect_to index_pathelsediff --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.rbindex 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::TwoFactorDevicesControllerendit 'activates the device when entered correctly and logs out the user' do- allow(::Sessions::DropAllSessionsService)+ allow(Sessions::DropAllSessionsService).to receive(:call)+ # rubocop:disable RSpec/AnyInstanceallow_any_instance_of(TwoFactorAuthentication::TokenService).to receive(:verify).with('1234').and_return(ServiceResult.success)+ # rubocop:enable RSpec/AnyInstancepost :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::TwoFactorDevicesControllerexpect(device.active).to be trueexpect(device.default).to be true- expect(::Sessions::DropAllSessionsService)+ expect(Sessions::DropAllSessionsService).to have_received(:call).with(user)endenddiff --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.rbindex 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 dolet!(:default_device) { create(:two_factor_authentication_device_totp, user:, default: true) }it 'activates the device when entered correctly' do+ # rubocop:disable RSpec/AnyInstanceallow_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/AnyInstancepost :confirm, params: { device_id: device.id, otp: '1234' }expect(response).to redirect_to action: :indexdiff --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.rbindex 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 doallow(User).to receive(:current).and_return(logged_in_user)allow(OpenProject::TwoFactorAuthentication::TokenStrategyManager).to receive(:add_default_strategy?)- .and_return false+ .and_return falseenddescribe 'accessing' do@@ -93,14 +93,12 @@ describe TwoFactorAuthentication::Users::TwoFactorDevicesController doenddescribe '#register' do- before do- post :register, params: { id: user.id, key: :sms, device: params }- end-context 'with missing phone' dolet(:params) { { identifier: 'foo' } }it 'renders action new' do+ post :register, params: { id: user.id, key: :sms, device: params }+expect(response).to be_successfulexpect(response).to render_template 'new'expect(assigns[:device]).to be_invalid@@ -111,6 +109,8 @@ describe TwoFactorAuthentication::Users::TwoFactorDevicesController dolet(: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.lastexpect(response).to redirect_to edit_user_path(user.id, tab: :two_factor_authentication)@@ -119,6 +119,44 @@ describe TwoFactorAuthentication::Users::TwoFactorDevicesController doexpect(device.default).to be_truthyexpect(device.active).to be_truthyend++ 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+ endendend