u2f_spec.rb 10.4 KB
Newer Older
1 2
require 'spec_helper'

3
describe 'Using U2F (Universal 2nd Factor) Devices for Authentication', :js do
4
  def manage_two_factor_authentication
Jose Ivan Vargas's avatar
Jose Ivan Vargas committed
5
    click_on 'Manage two-factor authentication'
6
    expect(page).to have_content("Set up new U2F device")
7
    wait_for_requests
8 9
  end

10
  def register_u2f_device(u2f_device = nil, name: 'My device')
11
    u2f_device ||= FakeU2fDevice.new(page, name)
12
    u2f_device.respond_to_u2f_registration
13
    click_on 'Set up new U2F device'
14
    expect(page).to have_content('Your device was successfully set up')
15
    fill_in "Pick a name", with: name
16
    click_on 'Register U2F device'
17 18 19 20 21 22
    u2f_device
  end

  describe "registration" do
    let(:user) { create(:user) }

23
    before do
24
      gitlab_sign_in(user)
25 26
      user.update_attribute(:otp_required_for_login, true)
    end
27

28
    describe 'when 2FA via OTP is disabled' do
29 30 31
      before do
        user.update_attribute(:otp_required_for_login, false)
      end
32

33
      it 'does not allow registering a new device' do
34
        visit profile_account_path
Jose Ivan Vargas's avatar
Jose Ivan Vargas committed
35
        click_on 'Enable two-factor authentication'
36

37
        expect(page).to have_button('Set up new U2F device', disabled: true)
38 39 40 41
      end
    end

    describe 'when 2FA via OTP is enabled' do
42
      it 'allows registering a new device with a name' do
43
        visit profile_account_path
44
        manage_two_factor_authentication
45
        expect(page).to have_content("You've already enabled two-factor authentication using one time password authenticators")
46

47
        u2f_device = register_u2f_device
48

49 50
        expect(page).to have_content(u2f_device.name)
        expect(page).to have_content('Your U2F device was registered')
51 52 53 54 55 56
      end

      it 'allows registering more than one device' do
        visit profile_account_path

        # First device
57
        manage_two_factor_authentication
58
        first_device = register_u2f_device
59
        expect(page).to have_content('Your U2F device was registered')
60 61

        # Second device
62
        second_device = register_u2f_device(name: 'My other device')
63
        expect(page).to have_content('Your U2F device was registered')
64

65 66
        expect(page).to have_content(first_device.name)
        expect(page).to have_content(second_device.name)
67 68 69 70 71
        expect(U2fRegistration.count).to eq(2)
      end

      it 'allows deleting a device' do
        visit profile_account_path
72
        manage_two_factor_authentication
73
        expect(page).to have_content("You've already enabled two-factor authentication using one time password authenticators")
74 75

        first_u2f_device = register_u2f_device
76
        second_u2f_device = register_u2f_device(name: 'My other device')
77

78
        accept_confirm { click_on "Delete", match: :first }
79

80
        expect(page).to have_content('Successfully deleted')
81
        expect(page.body).not_to match(first_u2f_device.name)
82
        expect(page).to have_content(second_u2f_device.name)
83 84 85 86 87 88
      end
    end

    it 'allows the same device to be registered for multiple users' do
      # First user
      visit profile_account_path
89
      manage_two_factor_authentication
90
      u2f_device = register_u2f_device
91
      expect(page).to have_content('Your U2F device was registered')
92
      gitlab_sign_out
93 94

      # Second user
95
      user = gitlab_sign_in(:user)
96
      user.update_attribute(:otp_required_for_login, true)
97
      visit profile_account_path
98
      manage_two_factor_authentication
99
      register_u2f_device(u2f_device, name: 'My other device')
100
      expect(page).to have_content('Your U2F device was registered')
101 102 103 104 105 106 107

      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
108
        manage_two_factor_authentication
109 110 111

        # Have the "u2f device" respond with bad data
        page.execute_script("u2f.register = function(_,_,_,callback) { callback('bad response'); };")
112
        click_on 'Set up new U2F device'
113
        expect(page).to have_content('Your device was successfully set up')
Jose Ivan Vargas's avatar
Jose Ivan Vargas committed
114
        click_on 'Register U2F device'
115 116

        expect(U2fRegistration.count).to eq(0)
117 118
        expect(page).to have_content("The form contains the following error")
        expect(page).to have_content("did not send a valid JSON response")
119 120 121 122
      end

      it "allows retrying registration" do
        visit profile_account_path
123
        manage_two_factor_authentication
124 125 126

        # Failed registration
        page.execute_script("u2f.register = function(_,_,_,callback) { callback('bad response'); };")
127
        click_on 'Set up new U2F device'
128
        expect(page).to have_content('Your device was successfully set up')
Jose Ivan Vargas's avatar
Jose Ivan Vargas committed
129
        click_on 'Register U2F device'
130
        expect(page).to have_content("The form contains the following error")
131 132 133 134

        # Successful registration
        register_u2f_device

135
        expect(page).to have_content('Your U2F device was registered')
136 137 138 139 140 141 142 143 144 145
        expect(U2fRegistration.count).to eq(1)
      end
    end
  end

  describe "authentication" do
    let(:user) { create(:user) }

    before do
      # Register and logout
146
      gitlab_sign_in(user)
147
      user.update_attribute(:otp_required_for_login, true)
148
      visit profile_account_path
149
      manage_two_factor_authentication
150
      @u2f_device = register_u2f_device
151
      gitlab_sign_out
152 153 154 155
    end

    describe "when 2FA via OTP is disabled" do
      it "allows logging in with the U2F device" do
156
        user.update_attribute(:otp_required_for_login, false)
157
        gitlab_sign_in(user)
158 159

        @u2f_device.respond_to_u2f_authentication
160 161

        expect(page).to have_css('.sign-out-link', visible: false)
162 163 164 165 166
      end
    end

    describe "when 2FA via OTP is enabled" do
      it "allows logging in with the U2F device" do
167
        user.update_attribute(:otp_required_for_login, true)
168
        gitlab_sign_in(user)
169 170 171

        @u2f_device.respond_to_u2f_authentication

172
        expect(page).to have_css('.sign-out-link', visible: false)
173 174 175 176 177 178 179
      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
180
          current_user = gitlab_sign_in(:user)
181
          current_user.update_attribute(:otp_required_for_login, true)
182
          visit profile_account_path
183
          manage_two_factor_authentication
184
          register_u2f_device(name: 'My other device')
185
          gitlab_sign_out
186 187

          # Try authenticating user with the old U2F device
188
          gitlab_sign_in(current_user)
189
          @u2f_device.respond_to_u2f_authentication
190
          expect(page).to have_content('Authentication via U2F device failed')
191 192 193 194 195 196
        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
197
          current_user = gitlab_sign_in(:user)
198
          current_user.update_attribute(:otp_required_for_login, true)
199
          visit profile_account_path
200
          manage_two_factor_authentication
201
          register_u2f_device(@u2f_device)
202
          gitlab_sign_out
203 204

          # Try authenticating user with the same U2F device
205
          gitlab_sign_in(current_user)
206 207
          @u2f_device.respond_to_u2f_authentication

208
          expect(page).to have_css('.sign-out-link', visible: false)
209 210 211 212 213 214
        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
215
        unregistered_device = FakeU2fDevice.new(page, 'My device')
216
        gitlab_sign_in(user)
217 218
        unregistered_device.respond_to_u2f_authentication

219
        expect(page).to have_content('Authentication via U2F device failed')
220 221 222
      end
    end

223 224 225
    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
226
        user = gitlab_sign_in(:user)
227 228 229 230 231 232 233 234
        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.")
235
        second_device = register_u2f_device(name: 'My other device')
236
        gitlab_sign_out
237 238 239

        # Authenticate as both devices
        [first_device, second_device].each do |device|
240
          gitlab_sign_in(user)
241
          device.respond_to_u2f_authentication
242

243
          expect(page).to have_css('.sign-out-link', visible: false)
244

245
          gitlab_sign_out
246 247
        end
      end
248 249
    end

250 251 252 253
    describe "when two-factor authentication is disabled" do
      let(:user) { create(:user) }

      before do
254
        user = gitlab_sign_in(:user)
255 256
        user.update_attribute(:otp_required_for_login, true)
        visit profile_account_path
257
        manage_two_factor_authentication
258 259 260 261 262
        expect(page).to have_content("Your U2F device needs to be set up.")
        register_u2f_device
      end

      it "deletes u2f registrations" do
George Tsiolis's avatar
George Tsiolis committed
263
        visit profile_two_factor_auth_path
264 265
        expect do
          accept_confirm { click_on "Disable" }
Jose Ivan Vargas's avatar
Jose Ivan Vargas committed
266
        end.to change { U2fRegistration.count }.by(-1)
267
      end
268 269
    end
  end
270 271 272 273 274 275 276 277 278 279 280 281 282

  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
283
      gitlab_sign_in(user)
284 285 286 287 288 289
      user.update_attribute(:otp_required_for_login, true)
      visit profile_account_path
    end

    describe 'when no u2f device is registered' do
      before do
290
        gitlab_sign_out
291
        gitlab_sign_in(user)
292 293 294 295 296 297 298 299 300 301 302
      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
303
        gitlab_sign_out
304
        gitlab_sign_in(user)
305 306 307 308 309 310 311 312 313 314 315
      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
316
end