BigW Consortium Gitlab

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

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

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

8
  def manage_two_factor_authentication
Jose Ivan Vargas committed
9
    click_on 'Manage two-factor authentication'
10
    expect(page).to have_content("Setup new U2F device")
11 12 13
    wait_for_ajax
  end

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

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

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

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

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

Jose Ivan Vargas committed
39
        expect(page).to have_button('Setup new U2F device', disabled: true)
40 41 42 43
      end
    end

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

49
        u2f_device = register_u2f_device
50

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

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

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

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

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

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

        first_u2f_device = register_u2f_device
78
        second_u2f_device = register_u2f_device(name: 'My other device')
79 80 81

        click_on "Delete", match: :first

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

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

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

      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
110
        manage_two_factor_authentication
111 112 113

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

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

      it "allows retrying registration" do
        visit profile_account_path
125
        manage_two_factor_authentication
126 127 128

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

        # Successful registration
        register_u2f_device

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

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

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

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

        @u2f_device.respond_to_u2f_authentication
162 163 164

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

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

        @u2f_device.respond_to_u2f_authentication

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

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

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

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

192 193 194 195 196
    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)
197
          current_user.update_attribute(:otp_required_for_login, true)
198
          visit profile_account_path
199
          manage_two_factor_authentication
200
          register_u2f_device(name: 'My other device')
201 202 203 204 205
          logout

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

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

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

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

242 243 244 245 246 247 248 249 250 251 252 253
    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.")
254
        second_device = register_u2f_device(name: 'My other device')
255 256 257 258 259 260
        logout

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

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

          logout
        end
      end
268 269
    end

270 271 272 273 274 275 276
    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
277
        manage_two_factor_authentication
278 279 280 281 282
        expect(page).to have_content("Your U2F device needs to be set up.")
        register_u2f_device
      end

      it "deletes u2f registrations" do
283
        visit profile_account_path
284 285
        expect { click_on "Disable" }.to change { U2fRegistration.count }.by(-1)
      end
286 287
    end
  end
288 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

  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
334
end