BigW Consortium Gitlab

fake_u2f_device.rb 988 Bytes
Newer Older
1
class FakeU2fDevice
2 3 4
  attr_reader :name

  def initialize(page, name)
5
    @page = page
6
    @name = name
7
  end
8

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
  def respond_to_u2f_registration
    app_id = @page.evaluate_script('gon.u2f.app_id')
    challenges = @page.evaluate_script('gon.u2f.challenges')

    json_response = u2f_device(app_id).register_response(challenges[0])

    @page.execute_script("
    u2f.register = function(appId, registerRequests, signRequests, callback) {
      callback(#{json_response});
    };
    ")
  end

  def respond_to_u2f_authentication
    app_id = @page.evaluate_script('gon.u2f.app_id')
24 25
    challenge = @page.evaluate_script('gon.u2f.challenge')
    json_response = u2f_device(app_id).sign_response(challenge)
26 27 28 29 30

    @page.execute_script("
    u2f.sign = function(appId, challenges, signRequests, callback) {
      callback(#{json_response});
    };
31
    window.gl.u2fAuthenticate.start();
32 33 34 35 36 37 38 39 40
    ")
  end

  private

  def u2f_device(app_id)
    @u2f_device ||= U2F::FakeU2F.new(app_id)
  end
end