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

8
  def manage_two_factor_authentication
Jose Ivan Vargas's avatar
Jose Ivan Vargas committed
9
    click_on 'Manage two-factor authentication'
10
    expect(page).to have_content("Setup new U2F device")
11
    wait_for_requests
12 13
  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
    describe 'when 2FA via OTP is disabled' do
33 34 35
      before do
        user.update_attribute(:otp_required_for_login, false)
      end
36

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

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

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

51
        u2f_device = register_u2f_device
52

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

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

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

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

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

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

        first_u2f_device = register_u2f_device
80
        second_u2f_device = register_u2f_device(name: 'My other device')
81 82 83

        click_on "Delete", match: :first

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

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

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

      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
112
        manage_two_factor_authentication
113 114 115

        # 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
116
        click_on 'Setup new U2F device'
117
        expect(page).to have_content('Your device was successfully set up')
Jose Ivan Vargas's avatar
Jose Ivan Vargas committed
118
        click_on 'Register U2F device'
119 120

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

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

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

        # Successful registration
        register_u2f_device

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

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

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

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

        @u2f_device.respond_to_u2f_authentication
164 165 166

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

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

        @u2f_device.respond_to_u2f_authentication

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

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

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

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

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

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

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

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

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

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

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

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

          logout
        end
      end
270 271
    end

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

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

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