debian-mirror-gitlab/spec/features/admin/admin_mode/login_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

267 lines
9.1 KiB
Ruby
Raw Normal View History

2020-04-08 14:13:33 +05:30
# frozen_string_literal: true
require 'spec_helper'
2021-02-22 17:27:13 +05:30
RSpec.describe 'Admin Mode Login' do
2020-04-08 14:13:33 +05:30
include TermsHelper
include UserLoginHelper
2020-05-24 23:13:21 +05:30
include LdapHelpers
2020-04-08 14:13:33 +05:30
describe 'with two-factor authentication', :js do
def enter_code(code)
fill_in 'user_otp_attempt', with: code
click_button 'Verify code'
end
2022-08-27 11:52:29 +05:30
flag_values = [true, false]
flag_values.each do |val|
before do
stub_feature_flags(restyle_login_page: val)
end
context 'with valid username/password' do
let(:user) { create(:admin, :two_factor) }
2020-04-08 14:13:33 +05:30
2022-08-27 11:52:29 +05:30
context 'using one-time code' do
it 'blocks login if we reuse the same code immediately' do
2020-04-08 14:13:33 +05:30
gitlab_sign_in(user, remember: true)
expect(page).to have_content('Two-Factor Authentication')
2022-08-27 11:52:29 +05:30
repeated_otp = user.current_otp
enter_code(repeated_otp)
2020-04-08 14:13:33 +05:30
gitlab_enable_admin_mode_sign_in(user)
expect(page).to have_content('Two-Factor Authentication')
2022-08-27 11:52:29 +05:30
enter_code(repeated_otp)
2020-04-08 14:13:33 +05:30
2022-08-27 11:52:29 +05:30
expect(page).to have_current_path admin_session_path, ignore_query: true
expect(page).to have_content('Invalid two-factor code')
2020-04-08 14:13:33 +05:30
end
2022-08-27 11:52:29 +05:30
context 'not re-using codes' do
before do
gitlab_sign_in(user, remember: true)
2020-04-08 14:13:33 +05:30
2022-08-27 11:52:29 +05:30
expect(page).to have_content('Two-factor authentication code')
2020-04-08 14:13:33 +05:30
enter_code(user.current_otp)
2022-08-27 11:52:29 +05:30
gitlab_enable_admin_mode_sign_in(user)
2020-04-08 14:13:33 +05:30
2022-08-27 11:52:29 +05:30
expect(page).to have_content('Two-Factor Authentication')
2020-04-08 14:13:33 +05:30
end
2022-08-27 11:52:29 +05:30
it 'allows login with valid code' do
# Cannot reuse the TOTP
travel_to(30.seconds.from_now) do
enter_code(user.current_otp)
2020-04-08 14:13:33 +05:30
2022-08-27 11:52:29 +05:30
expect(page).to have_current_path admin_root_path, ignore_query: true
expect(page).to have_content('Admin mode enabled')
end
end
it 'blocks login with invalid code' do
# Cannot reuse the TOTP
travel_to(30.seconds.from_now) do
enter_code('foo')
2020-04-08 14:13:33 +05:30
2022-08-27 11:52:29 +05:30
expect(page).to have_content('Invalid two-factor code')
end
2020-04-08 14:13:33 +05:30
end
2022-08-27 11:52:29 +05:30
it 'allows login with invalid code, then valid code' do
# Cannot reuse the TOTP
travel_to(30.seconds.from_now) do
enter_code('foo')
expect(page).to have_content('Invalid two-factor code')
enter_code(user.current_otp)
2020-04-08 14:13:33 +05:30
2022-05-07 20:08:51 +05:30
expect(page).to have_current_path admin_root_path, ignore_query: true
2020-04-08 14:13:33 +05:30
expect(page).to have_content('Admin mode enabled')
end
end
2022-08-27 11:52:29 +05:30
context 'using backup code' do
let(:codes) { user.generate_otp_backup_codes! }
before do
expect(codes.size).to eq 10
2020-04-08 14:13:33 +05:30
2022-08-27 11:52:29 +05:30
# Ensure the generated codes get saved
2020-04-08 14:13:33 +05:30
user.save!
2022-08-27 11:52:29 +05:30
end
context 'with valid code' do
it 'allows login' do
enter_code(codes.sample)
2020-04-08 14:13:33 +05:30
2022-08-27 11:52:29 +05:30
expect(page).to have_current_path admin_root_path, ignore_query: true
expect(page).to have_content('Admin mode enabled')
end
2020-04-08 14:13:33 +05:30
2022-08-27 11:52:29 +05:30
it 'invalidates the used code' do
expect { enter_code(codes.sample) }
.to change { user.reload.otp_backup_codes.size }.by(-1)
end
2020-04-08 14:13:33 +05:30
end
2022-08-27 11:52:29 +05:30
context 'with invalid code' do
it 'blocks login' do
code = codes.sample
expect(user.invalidate_otp_backup_code!(code)).to eq true
user.save!
expect(user.reload.otp_backup_codes.size).to eq 9
enter_code(code)
2020-04-08 14:13:33 +05:30
2022-08-27 11:52:29 +05:30
expect(page).to have_content('Invalid two-factor code.')
end
end
end
end
2020-04-08 14:13:33 +05:30
end
2022-08-27 11:52:29 +05:30
context 'when logging in via omniauth' do
let(:user) { create(:omniauth_user, :admin, :two_factor, extern_uid: 'my-uid', provider: 'saml', password_automatically_set: false) }
2020-04-08 14:13:33 +05:30
let(:mock_saml_response) do
File.read('spec/fixtures/authentication/saml_response.xml')
end
2022-08-27 11:52:29 +05:30
before do
stub_omniauth_saml_config(enabled: true, auto_link_saml_user: true, allow_single_sign_on: ['saml'], providers: [mock_saml_config_with_upstream_two_factor_authn_contexts])
2020-04-08 14:13:33 +05:30
end
2022-08-27 11:52:29 +05:30
context 'when authn_context is worth two factors' do
let(:mock_saml_response) do
File.read('spec/fixtures/authentication/saml_response.xml')
.gsub('urn:oasis:names:tc:SAML:2.0:ac:classes:Password',
'urn:oasis:names:tc:SAML:2.0:ac:classes:SecondFactorOTPSMS')
end
2020-04-08 14:13:33 +05:30
2022-08-27 11:52:29 +05:30
it 'signs user in without prompting for second factor' do
sign_in_using_saml!
2020-04-08 14:13:33 +05:30
2022-08-27 11:52:29 +05:30
expect(page).not_to have_content('Two-Factor Authentication')
2020-04-08 14:13:33 +05:30
2022-08-27 11:52:29 +05:30
enable_admin_mode_using_saml!
2020-04-08 14:13:33 +05:30
2022-08-27 11:52:29 +05:30
expect(page).not_to have_content('Two-Factor Authentication')
2022-05-07 20:08:51 +05:30
expect(page).to have_current_path admin_root_path, ignore_query: true
2020-04-08 14:13:33 +05:30
expect(page).to have_content('Admin mode enabled')
end
end
2022-08-27 11:52:29 +05:30
context 'when two factor authentication is required' do
it 'shows 2FA prompt after omniauth login' do
sign_in_using_saml!
2020-04-08 14:13:33 +05:30
2022-08-27 11:52:29 +05:30
expect(page).to have_content('Two-Factor Authentication')
enter_code(user.current_otp)
2020-05-24 23:13:21 +05:30
2022-08-27 11:52:29 +05:30
enable_admin_mode_using_saml!
expect(page).to have_content('Two-Factor Authentication')
2020-10-24 23:57:45 +05:30
2022-08-27 11:52:29 +05:30
# Cannot reuse the TOTP
travel_to(30.seconds.from_now) do
enter_code(user.current_otp)
2020-05-24 23:13:21 +05:30
2022-08-27 11:52:29 +05:30
expect(page).to have_current_path admin_root_path, ignore_query: true
expect(page).to have_content('Admin mode enabled')
end
end
end
def sign_in_using_saml!
gitlab_sign_in_via('saml', user, 'my-uid', mock_saml_response)
end
def enable_admin_mode_using_saml!
gitlab_enable_admin_mode_sign_in_via('saml', user, 'my-uid', mock_saml_response)
end
2020-05-24 23:13:21 +05:30
end
2022-08-27 11:52:29 +05:30
context 'when logging in via ldap' do
let(:uid) { 'my-uid' }
let(:provider_label) { 'Main LDAP' }
let(:provider_name) { 'main' }
let(:provider) { "ldap#{provider_name}" }
let(:ldap_server_config) do
{
'label' => provider_label,
'provider_name' => provider,
'attributes' => {},
'encryption' => 'plain',
'uid' => 'uid',
'base' => 'dc=example,dc=com'
}
end
2020-05-24 23:13:21 +05:30
2022-08-27 11:52:29 +05:30
let(:user) { create(:omniauth_user, :admin, :two_factor, extern_uid: uid, provider: provider) }
2020-05-24 23:13:21 +05:30
2022-08-27 11:52:29 +05:30
before do
setup_ldap(provider, user, uid, ldap_server_config)
end
2020-05-24 23:13:21 +05:30
2022-08-27 11:52:29 +05:30
context 'when two factor authentication is required' do
it 'shows 2FA prompt after ldap login' do
sign_in_using_ldap!(user, provider_label)
expect(page).to have_content('Two-Factor Authentication')
2020-05-24 23:13:21 +05:30
enter_code(user.current_otp)
2022-08-27 11:52:29 +05:30
enable_admin_mode_using_ldap!(user)
2020-05-24 23:13:21 +05:30
2022-08-27 11:52:29 +05:30
expect(page).to have_content('Two-Factor Authentication')
# Cannot reuse the TOTP
travel_to(30.seconds.from_now) do
enter_code(user.current_otp)
expect(page).to have_current_path admin_root_path, ignore_query: true
expect(page).to have_content('Admin mode enabled')
end
2020-05-24 23:13:21 +05:30
end
end
2022-08-27 11:52:29 +05:30
def setup_ldap(provider, user, uid, ldap_server_config)
stub_ldap_setting(enabled: true)
2020-05-24 23:13:21 +05:30
2022-08-27 11:52:29 +05:30
allow(::Gitlab::Auth::Ldap::Config).to receive_messages(enabled: true, servers: [ldap_server_config])
allow(Gitlab::Auth::OAuth::Provider).to receive_messages(providers: [provider.to_sym])
2020-05-24 23:13:21 +05:30
2022-08-27 11:52:29 +05:30
Ldap::OmniauthCallbacksController.define_providers!
Rails.application.reload_routes!
2020-05-24 23:13:21 +05:30
2022-08-27 11:52:29 +05:30
mock_auth_hash(provider, uid, user.email)
allow(Gitlab::Auth::Ldap::Access).to receive(:allowed?).with(user).and_return(true)
2020-05-24 23:13:21 +05:30
2022-08-27 11:52:29 +05:30
allow_any_instance_of(ActionDispatch::Routing::RoutesProxy)
.to receive(:"user_#{provider}_omniauth_callback_path")
.and_return("/users/auth/#{provider}/callback")
end
2020-05-24 23:13:21 +05:30
2022-08-27 11:52:29 +05:30
def sign_in_using_ldap!(user, provider_label)
visit new_user_session_path
click_link provider_label
fill_in 'username', with: user.username
fill_in 'password', with: user.password
click_button 'Sign in'
end
2020-05-24 23:13:21 +05:30
2022-08-27 11:52:29 +05:30
def enable_admin_mode_using_ldap!(user)
visit new_admin_session_path
click_link provider_label
fill_in 'username', with: user.username
fill_in 'password', with: user.password
click_button 'Enter Admin Mode'
end
2020-05-24 23:13:21 +05:30
end
end
2020-04-08 14:13:33 +05:30
end
end
end