2016-06-06 00:53:27 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-03-23 09:08:39 -04:00
|
|
|
feature 'Using U2F (Universal 2nd Factor) Devices for Authentication', :js do
|
2016-08-11 01:35:36 -04:00
|
|
|
def manage_two_factor_authentication
|
2017-04-03 19:10:06 -04:00
|
|
|
click_on 'Manage two-factor authentication'
|
2017-04-04 14:06:32 -04:00
|
|
|
expect(page).to have_content("Setup new U2F device")
|
2017-05-17 14:25:13 -04:00
|
|
|
wait_for_requests
|
2016-08-11 01:35:36 -04:00
|
|
|
end
|
|
|
|
|
2017-03-23 09:08:39 -04:00
|
|
|
def register_u2f_device(u2f_device = nil, name: 'My device')
|
2016-08-16 15:16:15 -04:00
|
|
|
u2f_device ||= FakeU2fDevice.new(page, name)
|
2016-06-06 00:53:27 -04:00
|
|
|
u2f_device.respond_to_u2f_registration
|
2017-04-04 14:06:32 -04:00
|
|
|
click_on 'Setup new U2F device'
|
2016-06-06 00:53:27 -04:00
|
|
|
expect(page).to have_content('Your device was successfully set up')
|
2016-08-16 15:16:15 -04:00
|
|
|
fill_in "Pick a name", with: name
|
2017-04-04 14:06:32 -04:00
|
|
|
click_on 'Register U2F device'
|
2016-06-06 00:53:27 -04:00
|
|
|
u2f_device
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "registration" do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
2016-06-14 00:18:52 -04:00
|
|
|
before do
|
2017-06-05 14:44:29 -04:00
|
|
|
gitlab_sign_in(user)
|
2016-06-14 00:18:52 -04:00
|
|
|
user.update_attribute(:otp_required_for_login, true)
|
|
|
|
end
|
2016-06-06 00:53:27 -04:00
|
|
|
|
2016-06-14 00:18:52 -04:00
|
|
|
describe 'when 2FA via OTP is disabled' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
user.update_attribute(:otp_required_for_login, false)
|
|
|
|
end
|
2016-06-06 00:53:27 -04:00
|
|
|
|
2016-06-14 00:18:52 -04:00
|
|
|
it 'does not allow registering a new device' do
|
2016-06-06 00:53:27 -04:00
|
|
|
visit profile_account_path
|
2017-04-03 19:10:06 -04:00
|
|
|
click_on 'Enable two-factor authentication'
|
2016-06-06 00:53:27 -04:00
|
|
|
|
2017-04-03 19:10:06 -04:00
|
|
|
expect(page).to have_button('Setup new U2F device', disabled: true)
|
2016-06-06 00:53:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when 2FA via OTP is enabled' do
|
2016-08-16 15:16:15 -04:00
|
|
|
it 'allows registering a new device with a name' do
|
2016-06-06 00:53:27 -04:00
|
|
|
visit profile_account_path
|
2016-08-11 01:35:36 -04:00
|
|
|
manage_two_factor_authentication
|
2016-12-24 11:53:13 -05:00
|
|
|
expect(page).to have_content("You've already enabled two-factor authentication using mobile")
|
2016-06-06 00:53:27 -04:00
|
|
|
|
2016-08-16 15:16:15 -04:00
|
|
|
u2f_device = register_u2f_device
|
2016-06-06 00:53:27 -04:00
|
|
|
|
2016-12-24 11:53:13 -05:00
|
|
|
expect(page).to have_content(u2f_device.name)
|
|
|
|
expect(page).to have_content('Your U2F device was registered')
|
2016-06-06 00:53:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows registering more than one device' do
|
|
|
|
visit profile_account_path
|
|
|
|
|
|
|
|
# First device
|
2016-08-11 01:35:36 -04:00
|
|
|
manage_two_factor_authentication
|
2016-08-16 15:16:15 -04:00
|
|
|
first_device = register_u2f_device
|
2016-12-24 11:53:13 -05:00
|
|
|
expect(page).to have_content('Your U2F device was registered')
|
2016-06-06 00:53:27 -04:00
|
|
|
|
|
|
|
# Second device
|
2017-03-23 09:08:39 -04:00
|
|
|
second_device = register_u2f_device(name: 'My other device')
|
2016-12-24 11:53:13 -05:00
|
|
|
expect(page).to have_content('Your U2F device was registered')
|
2016-08-16 15:16:15 -04:00
|
|
|
|
2016-12-24 11:53:13 -05:00
|
|
|
expect(page).to have_content(first_device.name)
|
|
|
|
expect(page).to have_content(second_device.name)
|
2016-08-16 15:16:15 -04:00
|
|
|
expect(U2fRegistration.count).to eq(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows deleting a device' do
|
|
|
|
visit profile_account_path
|
2016-08-11 01:35:36 -04:00
|
|
|
manage_two_factor_authentication
|
2016-12-24 11:53:13 -05:00
|
|
|
expect(page).to have_content("You've already enabled two-factor authentication using mobile")
|
2016-08-16 15:16:15 -04:00
|
|
|
|
|
|
|
first_u2f_device = register_u2f_device
|
2017-03-23 09:08:39 -04:00
|
|
|
second_u2f_device = register_u2f_device(name: 'My other device')
|
2016-08-16 15:16:15 -04:00
|
|
|
|
2017-08-14 01:30:37 -04:00
|
|
|
accept_confirm { click_on "Delete", match: :first }
|
2016-08-16 15:16:15 -04:00
|
|
|
|
2016-12-24 11:53:13 -05:00
|
|
|
expect(page).to have_content('Successfully deleted')
|
2016-08-16 15:16:15 -04:00
|
|
|
expect(page.body).not_to match(first_u2f_device.name)
|
2016-12-24 11:53:13 -05:00
|
|
|
expect(page).to have_content(second_u2f_device.name)
|
2016-06-06 00:53:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows the same device to be registered for multiple users' do
|
|
|
|
# First user
|
|
|
|
visit profile_account_path
|
2016-08-11 01:35:36 -04:00
|
|
|
manage_two_factor_authentication
|
2016-06-06 00:53:27 -04:00
|
|
|
u2f_device = register_u2f_device
|
2016-12-24 11:53:13 -05:00
|
|
|
expect(page).to have_content('Your U2F device was registered')
|
2017-06-05 15:10:13 -04:00
|
|
|
gitlab_sign_out
|
2016-06-06 00:53:27 -04:00
|
|
|
|
|
|
|
# Second user
|
2017-06-05 14:44:29 -04:00
|
|
|
user = gitlab_sign_in(:user)
|
2016-06-14 00:18:52 -04:00
|
|
|
user.update_attribute(:otp_required_for_login, true)
|
2016-06-06 00:53:27 -04:00
|
|
|
visit profile_account_path
|
2016-08-11 01:35:36 -04:00
|
|
|
manage_two_factor_authentication
|
2017-03-23 09:08:39 -04:00
|
|
|
register_u2f_device(u2f_device, name: 'My other device')
|
2016-12-24 11:53:13 -05:00
|
|
|
expect(page).to have_content('Your U2F device was registered')
|
2016-06-06 00:53:27 -04:00
|
|
|
|
|
|
|
expect(U2fRegistration.count).to eq(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when there are form errors" do
|
|
|
|
it "doesn't register the device if there are errors" do
|
|
|
|
visit profile_account_path
|
2016-08-11 01:35:36 -04:00
|
|
|
manage_two_factor_authentication
|
2016-06-06 00:53:27 -04:00
|
|
|
|
|
|
|
# Have the "u2f device" respond with bad data
|
|
|
|
page.execute_script("u2f.register = function(_,_,_,callback) { callback('bad response'); };")
|
2017-04-03 19:10:06 -04:00
|
|
|
click_on 'Setup new U2F device'
|
2016-06-06 00:53:27 -04:00
|
|
|
expect(page).to have_content('Your device was successfully set up')
|
2017-04-03 19:10:06 -04:00
|
|
|
click_on 'Register U2F device'
|
2016-06-06 00:53:27 -04:00
|
|
|
|
|
|
|
expect(U2fRegistration.count).to eq(0)
|
2016-12-24 11:53:13 -05:00
|
|
|
expect(page).to have_content("The form contains the following error")
|
|
|
|
expect(page).to have_content("did not send a valid JSON response")
|
2016-06-06 00:53:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "allows retrying registration" do
|
|
|
|
visit profile_account_path
|
2016-08-11 01:35:36 -04:00
|
|
|
manage_two_factor_authentication
|
2016-06-06 00:53:27 -04:00
|
|
|
|
|
|
|
# Failed registration
|
|
|
|
page.execute_script("u2f.register = function(_,_,_,callback) { callback('bad response'); };")
|
2017-04-03 19:10:06 -04:00
|
|
|
click_on 'Setup new U2F device'
|
2016-06-06 00:53:27 -04:00
|
|
|
expect(page).to have_content('Your device was successfully set up')
|
2017-04-03 19:10:06 -04:00
|
|
|
click_on 'Register U2F device'
|
2016-12-24 11:53:13 -05:00
|
|
|
expect(page).to have_content("The form contains the following error")
|
2016-06-06 00:53:27 -04:00
|
|
|
|
|
|
|
# Successful registration
|
|
|
|
register_u2f_device
|
|
|
|
|
2016-12-24 11:53:13 -05:00
|
|
|
expect(page).to have_content('Your U2F device was registered')
|
2016-06-06 00:53:27 -04:00
|
|
|
expect(U2fRegistration.count).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "authentication" do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
# Register and logout
|
2017-06-05 14:44:29 -04:00
|
|
|
gitlab_sign_in(user)
|
2016-06-14 00:18:52 -04:00
|
|
|
user.update_attribute(:otp_required_for_login, true)
|
2016-06-06 00:53:27 -04:00
|
|
|
visit profile_account_path
|
2016-08-11 01:35:36 -04:00
|
|
|
manage_two_factor_authentication
|
2016-06-06 00:53:27 -04:00
|
|
|
@u2f_device = register_u2f_device
|
2017-06-05 15:10:13 -04:00
|
|
|
gitlab_sign_out
|
2016-06-06 00:53:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "when 2FA via OTP is disabled" do
|
|
|
|
it "allows logging in with the U2F device" do
|
2016-08-19 21:51:56 -04:00
|
|
|
user.update_attribute(:otp_required_for_login, false)
|
2017-06-05 14:51:54 -04:00
|
|
|
gitlab_sign_in(user)
|
2016-06-06 00:53:27 -04:00
|
|
|
|
|
|
|
@u2f_device.respond_to_u2f_authentication
|
2016-12-24 11:53:13 -05:00
|
|
|
|
|
|
|
expect(page).to have_css('.sign-out-link', visible: false)
|
2016-06-06 00:53:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "when 2FA via OTP is enabled" do
|
|
|
|
it "allows logging in with the U2F device" do
|
2016-06-14 00:18:52 -04:00
|
|
|
user.update_attribute(:otp_required_for_login, true)
|
2017-06-05 14:51:54 -04:00
|
|
|
gitlab_sign_in(user)
|
2016-06-06 00:53:27 -04:00
|
|
|
|
|
|
|
@u2f_device.respond_to_u2f_authentication
|
|
|
|
|
2016-12-24 11:53:13 -05:00
|
|
|
expect(page).to have_css('.sign-out-link', visible: false)
|
2016-06-06 00:53:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "when a given U2F device has already been registered by another user" do
|
|
|
|
describe "but not the current user" do
|
|
|
|
it "does not allow logging in with that particular device" do
|
|
|
|
# Register current user with the different U2F device
|
2017-06-05 14:44:29 -04:00
|
|
|
current_user = gitlab_sign_in(:user)
|
2016-06-14 00:18:52 -04:00
|
|
|
current_user.update_attribute(:otp_required_for_login, true)
|
2016-06-06 00:53:27 -04:00
|
|
|
visit profile_account_path
|
2016-08-11 01:35:36 -04:00
|
|
|
manage_two_factor_authentication
|
2017-03-23 09:08:39 -04:00
|
|
|
register_u2f_device(name: 'My other device')
|
2017-06-05 15:10:13 -04:00
|
|
|
gitlab_sign_out
|
2016-06-06 00:53:27 -04:00
|
|
|
|
|
|
|
# Try authenticating user with the old U2F device
|
2017-06-05 14:44:29 -04:00
|
|
|
gitlab_sign_in(current_user)
|
2016-06-06 00:53:27 -04:00
|
|
|
@u2f_device.respond_to_u2f_authentication
|
2016-12-24 11:53:13 -05:00
|
|
|
expect(page).to have_content('Authentication via U2F device failed')
|
2016-06-06 00:53:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "and also the current user" do
|
|
|
|
it "allows logging in with that particular device" do
|
|
|
|
# Register current user with the same U2F device
|
2017-06-05 14:44:29 -04:00
|
|
|
current_user = gitlab_sign_in(:user)
|
2016-06-14 00:18:52 -04:00
|
|
|
current_user.update_attribute(:otp_required_for_login, true)
|
2016-06-06 00:53:27 -04:00
|
|
|
visit profile_account_path
|
2016-08-11 01:35:36 -04:00
|
|
|
manage_two_factor_authentication
|
2016-06-06 00:53:27 -04:00
|
|
|
register_u2f_device(@u2f_device)
|
2017-06-05 15:10:13 -04:00
|
|
|
gitlab_sign_out
|
2016-06-06 00:53:27 -04:00
|
|
|
|
|
|
|
# Try authenticating user with the same U2F device
|
2017-06-05 14:44:29 -04:00
|
|
|
gitlab_sign_in(current_user)
|
2016-06-06 00:53:27 -04:00
|
|
|
@u2f_device.respond_to_u2f_authentication
|
|
|
|
|
2016-12-24 11:53:13 -05:00
|
|
|
expect(page).to have_css('.sign-out-link', visible: false)
|
2016-06-06 00:53:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "when a given U2F device has not been registered" do
|
|
|
|
it "does not allow logging in with that particular device" do
|
2017-03-23 09:08:39 -04:00
|
|
|
unregistered_device = FakeU2fDevice.new(page, 'My device')
|
2017-06-05 14:44:29 -04:00
|
|
|
gitlab_sign_in(user)
|
2016-06-06 00:53:27 -04:00
|
|
|
unregistered_device.respond_to_u2f_authentication
|
|
|
|
|
2016-12-24 11:53:13 -05:00
|
|
|
expect(page).to have_content('Authentication via U2F device failed')
|
2016-06-06 00:53:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-11 03:53:02 -04:00
|
|
|
describe "when more than one device has been registered by the same user" do
|
|
|
|
it "allows logging in with either device" do
|
|
|
|
# Register first device
|
2017-06-05 14:44:29 -04:00
|
|
|
user = gitlab_sign_in(:user)
|
2016-07-11 03:53:02 -04:00
|
|
|
user.update_attribute(:otp_required_for_login, true)
|
|
|
|
visit profile_two_factor_auth_path
|
|
|
|
expect(page).to have_content("Your U2F device needs to be set up.")
|
|
|
|
first_device = register_u2f_device
|
|
|
|
|
|
|
|
# Register second device
|
|
|
|
visit profile_two_factor_auth_path
|
|
|
|
expect(page).to have_content("Your U2F device needs to be set up.")
|
2017-03-23 09:08:39 -04:00
|
|
|
second_device = register_u2f_device(name: 'My other device')
|
2017-06-05 15:10:13 -04:00
|
|
|
gitlab_sign_out
|
2016-07-11 03:53:02 -04:00
|
|
|
|
|
|
|
# Authenticate as both devices
|
|
|
|
[first_device, second_device].each do |device|
|
2017-06-05 14:44:29 -04:00
|
|
|
gitlab_sign_in(user)
|
2016-07-11 03:53:02 -04:00
|
|
|
device.respond_to_u2f_authentication
|
2016-06-06 00:53:27 -04:00
|
|
|
|
2016-12-24 11:53:13 -05:00
|
|
|
expect(page).to have_css('.sign-out-link', visible: false)
|
2016-07-11 03:53:02 -04:00
|
|
|
|
2017-06-05 15:10:13 -04:00
|
|
|
gitlab_sign_out
|
2016-07-11 03:53:02 -04:00
|
|
|
end
|
|
|
|
end
|
2016-06-06 00:53:27 -04:00
|
|
|
end
|
|
|
|
|
2016-07-11 03:53:02 -04:00
|
|
|
describe "when two-factor authentication is disabled" do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
before do
|
2017-06-05 14:44:29 -04:00
|
|
|
user = gitlab_sign_in(:user)
|
2016-07-11 03:53:02 -04:00
|
|
|
user.update_attribute(:otp_required_for_login, true)
|
|
|
|
visit profile_account_path
|
2016-08-11 01:35:36 -04:00
|
|
|
manage_two_factor_authentication
|
2016-07-11 03:53:02 -04:00
|
|
|
expect(page).to have_content("Your U2F device needs to be set up.")
|
|
|
|
register_u2f_device
|
|
|
|
end
|
|
|
|
|
|
|
|
it "deletes u2f registrations" do
|
2018-01-02 08:07:13 -05:00
|
|
|
visit profile_two_factor_auth_path
|
2017-09-27 06:10:58 -04:00
|
|
|
expect do
|
|
|
|
accept_confirm { click_on "Disable" }
|
2017-10-04 14:54:34 -04:00
|
|
|
end.to change { U2fRegistration.count }.by(-1)
|
2016-07-11 03:53:02 -04:00
|
|
|
end
|
2016-06-06 00:53:27 -04:00
|
|
|
end
|
|
|
|
end
|
2016-12-24 11:53:13 -05:00
|
|
|
|
|
|
|
describe 'fallback code authentication' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
def assert_fallback_ui(page)
|
|
|
|
expect(page).to have_button('Verify code')
|
|
|
|
expect(page).to have_css('#user_otp_attempt')
|
|
|
|
expect(page).not_to have_link('Sign in via 2FA code')
|
|
|
|
expect(page).not_to have_css('#js-authenticate-u2f')
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
# Register and logout
|
2017-06-05 14:44:29 -04:00
|
|
|
gitlab_sign_in(user)
|
2016-12-24 11:53:13 -05:00
|
|
|
user.update_attribute(:otp_required_for_login, true)
|
|
|
|
visit profile_account_path
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when no u2f device is registered' do
|
|
|
|
before do
|
2017-06-05 15:10:13 -04:00
|
|
|
gitlab_sign_out
|
2017-06-05 14:51:54 -04:00
|
|
|
gitlab_sign_in(user)
|
2016-12-24 11:53:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'shows the fallback otp code UI' do
|
|
|
|
assert_fallback_ui(page)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when a u2f device is registered' do
|
|
|
|
before do
|
|
|
|
manage_two_factor_authentication
|
|
|
|
@u2f_device = register_u2f_device
|
2017-06-05 15:10:13 -04:00
|
|
|
gitlab_sign_out
|
2017-06-05 14:51:54 -04:00
|
|
|
gitlab_sign_in(user)
|
2016-12-24 11:53:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'provides a button that shows the fallback otp code UI' do
|
|
|
|
expect(page).to have_link('Sign in via 2FA code')
|
|
|
|
|
|
|
|
click_link('Sign in via 2FA code')
|
|
|
|
|
|
|
|
assert_fallback_ui(page)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-06-06 00:53:27 -04:00
|
|
|
end
|