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
  before { allow_any_instance_of(U2fHelper).to receive(:inject_u2f_api?).and_return(true) }

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

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

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

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

30 31
    describe 'when 2FA via OTP is disabled' do
      before { user.update_attribute(:otp_required_for_login, false) }
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

Jose Ivan Vargas's avatar
Jose Ivan Vargas committed
37
        expect(page).to have_button('Setup 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 mobile")
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 mobile")
74 75

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

        click_on "Delete", match: :first

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 93 94
      logout

      # Second user
95 96
      user = login_as(:user)
      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'); };")
Jose Ivan Vargas's avatar
Jose Ivan Vargas committed
112
        click_on 'Setup 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'); };")
Jose Ivan Vargas's avatar
Jose Ivan Vargas committed
127
        click_on 'Setup 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 146
        expect(U2fRegistration.count).to eq(1)
      end
    end
  end

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

    before do
      # Register and logout
      login_as(user)
147
      user.update_attribute(:otp_required_for_login, true)
148
      visit profile_account_path
149
      manage_two_factor_authentication
150 151 152 153 154 155
      @u2f_device = register_u2f_device
      logout
    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 158 159
        login_with(user)

        @u2f_device.respond_to_u2f_authentication
160 161 162

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

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

        @u2f_device.respond_to_u2f_authentication

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

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

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

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

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

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

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

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

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

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

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

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

          logout
        end
      end
266 267
    end

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

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

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