BigW Consortium Gitlab

u2f_spec.rb 10.9 KB
Newer Older
1 2 3
require 'spec_helper'

feature 'Using U2F (Universal 2nd Factor) Devices for Authentication', feature: true, js: true do
4 5
  include WaitForAjax

6 7
  before { allow_any_instance_of(U2fHelper).to receive(:inject_u2f_api?).and_return(true) }

8 9 10 11 12 13
  def manage_two_factor_authentication
    click_on 'Manage Two-Factor Authentication'
    expect(page).to have_content("Setup New U2F Device")
    wait_for_ajax
  end

14
  def register_u2f_device(u2f_device = nil)
15 16
    name = FFaker::Name.first_name
    u2f_device ||= FakeU2fDevice.new(page, name)
17 18 19
    u2f_device.respond_to_u2f_registration
    click_on 'Setup New U2F Device'
    expect(page).to have_content('Your device was successfully set up')
20
    fill_in "Pick a name", with: name
21 22 23 24 25 26 27
    click_on 'Register U2F Device'
    u2f_device
  end

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

28 29 30 31
    before do
      login_as(user)
      user.update_attribute(:otp_required_for_login, true)
    end
32

33 34
    describe 'when 2FA via OTP is disabled' do
      before { user.update_attribute(:otp_required_for_login, false) }
35

36
      it 'does not allow registering a new device' do
37 38 39
        visit profile_account_path
        click_on 'Enable Two-Factor Authentication'

40
        expect(page).to have_button('Setup New U2F Device', disabled: true)
41 42 43 44
      end
    end

    describe 'when 2FA via OTP is enabled' do
45
      it 'allows registering a new device with a name' do
46
        visit profile_account_path
47
        manage_two_factor_authentication
48
        expect(page).to have_content("You've already enabled two-factor authentication using mobile")
49

50
        u2f_device = register_u2f_device
51

52 53
        expect(page).to have_content(u2f_device.name)
        expect(page).to have_content('Your U2F device was registered')
54 55 56 57 58 59
      end

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

        # First device
60
        manage_two_factor_authentication
61
        first_device = register_u2f_device
62
        expect(page).to have_content('Your U2F device was registered')
63 64

        # Second device
65
        second_device = register_u2f_device
66
        expect(page).to have_content('Your U2F device was registered')
67

68 69
        expect(page).to have_content(first_device.name)
        expect(page).to have_content(second_device.name)
70 71 72 73 74
        expect(U2fRegistration.count).to eq(2)
      end

      it 'allows deleting a device' do
        visit profile_account_path
75
        manage_two_factor_authentication
76
        expect(page).to have_content("You've already enabled two-factor authentication using mobile")
77 78 79 80 81 82

        first_u2f_device = register_u2f_device
        second_u2f_device = register_u2f_device

        click_on "Delete", match: :first

83
        expect(page).to have_content('Successfully deleted')
84
        expect(page.body).not_to match(first_u2f_device.name)
85
        expect(page).to have_content(second_u2f_device.name)
86 87 88 89 90 91
      end
    end

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

      # Second user
98 99
      user = login_as(:user)
      user.update_attribute(:otp_required_for_login, true)
100
      visit profile_account_path
101
      manage_two_factor_authentication
102
      register_u2f_device(u2f_device)
103
      expect(page).to have_content('Your U2F device was registered')
104 105 106 107 108 109 110

      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
111
        manage_two_factor_authentication
112 113 114 115 116 117 118 119

        # Have the "u2f device" respond with bad data
        page.execute_script("u2f.register = function(_,_,_,callback) { callback('bad response'); };")
        click_on 'Setup New U2F Device'
        expect(page).to have_content('Your device was successfully set up')
        click_on 'Register U2F Device'

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

      it "allows retrying registration" do
        visit profile_account_path
126
        manage_two_factor_authentication
127 128 129 130 131 132

        # Failed registration
        page.execute_script("u2f.register = function(_,_,_,callback) { callback('bad response'); };")
        click_on 'Setup New U2F Device'
        expect(page).to have_content('Your device was successfully set up')
        click_on 'Register U2F Device'
133
        expect(page).to have_content("The form contains the following error")
134 135 136 137

        # Successful registration
        register_u2f_device

138
        expect(page).to have_content('Your U2F device was registered')
139 140 141 142 143 144 145 146 147 148 149
        expect(U2fRegistration.count).to eq(1)
      end
    end
  end

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

    before do
      # Register and logout
      login_as(user)
150
      user.update_attribute(:otp_required_for_login, true)
151
      visit profile_account_path
152
      manage_two_factor_authentication
153 154 155 156 157 158
      @u2f_device = register_u2f_device
      logout
    end

    describe "when 2FA via OTP is disabled" do
      it "allows logging in with the U2F device" do
159
        user.update_attribute(:otp_required_for_login, false)
160 161 162
        login_with(user)

        @u2f_device.respond_to_u2f_authentication
163 164 165

        expect(page).to have_content('We heard back from your U2F device')
        expect(page).to have_css('.sign-out-link', visible: false)
166 167 168 169 170
      end
    end

    describe "when 2FA via OTP is enabled" do
      it "allows logging in with the U2F device" do
171
        user.update_attribute(:otp_required_for_login, true)
172 173 174 175
        login_with(user)

        @u2f_device.respond_to_u2f_authentication

176 177
        expect(page).to have_content('We heard back from your U2F device')
        expect(page).to have_css('.sign-out-link', visible: false)
178 179 180
      end
    end

181 182 183 184
    it 'persists remember_me value via hidden field' do
      login_with(user, remember: true)

      @u2f_device.respond_to_u2f_authentication
185
      expect(page).to have_content('We heard back from your U2F device')
186 187 188 189 190 191 192

      within 'div#js-authenticate-u2f' do
        field = first('input#user_remember_me', visible: false)
        expect(field.value).to eq '1'
      end
    end

193 194 195 196 197
    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
          current_user = login_as(:user)
198
          current_user.update_attribute(:otp_required_for_login, true)
199
          visit profile_account_path
200
          manage_two_factor_authentication
201 202 203 204 205 206
          register_u2f_device
          logout

          # Try authenticating user with the old U2F device
          login_as(current_user)
          @u2f_device.respond_to_u2f_authentication
207 208
          expect(page).to have_content('We heard back from your U2F device')
          expect(page).to have_content('Authentication via U2F device failed')
209 210 211 212 213 214 215
        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
          current_user = login_as(:user)
216
          current_user.update_attribute(:otp_required_for_login, true)
217
          visit profile_account_path
218
          manage_two_factor_authentication
219 220 221 222 223 224
          register_u2f_device(@u2f_device)
          logout

          # Try authenticating user with the same U2F device
          login_as(current_user)
          @u2f_device.respond_to_u2f_authentication
225
          expect(page).to have_content('We heard back from your U2F device')
226

227
          expect(page).to have_css('.sign-out-link', visible: false)
228 229 230 231 232 233
        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
234
        unregistered_device = FakeU2fDevice.new(page, FFaker::Name.first_name)
235 236
        login_as(user)
        unregistered_device.respond_to_u2f_authentication
237
        expect(page).to have_content('We heard back from your U2F device')
238

239
        expect(page).to have_content('Authentication via U2F device failed')
240 241 242
      end
    end

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
    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
        user = login_as(:user)
        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.")
        second_device = register_u2f_device
        logout

        # Authenticate as both devices
        [first_device, second_device].each do |device|
          login_as(user)
          device.respond_to_u2f_authentication
262
          expect(page).to have_content('We heard back from your U2F device')
263

264
          expect(page).to have_css('.sign-out-link', visible: false)
265 266 267 268

          logout
        end
      end
269 270
    end

271 272 273 274 275 276 277
    describe "when two-factor authentication is disabled" do
      let(:user) { create(:user) }

      before do
        user = login_as(:user)
        user.update_attribute(:otp_required_for_login, true)
        visit profile_account_path
278
        manage_two_factor_authentication
279 280 281 282 283
        expect(page).to have_content("Your U2F device needs to be set up.")
        register_u2f_device
      end

      it "deletes u2f registrations" do
284
        visit profile_account_path
285 286
        expect { click_on "Disable" }.to change { U2fRegistration.count }.by(-1)
      end
287 288
    end
  end
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334

  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
      login_as(user)
      user.update_attribute(:otp_required_for_login, true)
      visit profile_account_path
    end

    describe 'when no u2f device is registered' do
      before do
        logout
        login_with(user)
      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
        logout
        login_with(user)
      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
335
end