Class: Gem::GemcutterUtilities::WebauthnPoller

Inherits:
Object
  • Object
show all
Includes:
Gem::GemcutterUtilities
Defined in:
lib/rubygems/gemcutter_utilities/webauthn_poller.rb

Constant Summary collapse

TIMEOUT_IN_SECONDS =
300

Constants included from Gem::GemcutterUtilities

API_SCOPES, ERROR_CODE, EXCLUSIVELY_API_SCOPES

Instance Attribute Summary collapse

Attributes included from Gem::GemcutterUtilities

#scope

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gem::GemcutterUtilities

#add_key_option, #add_otp_option, #api_key, #mfa_unauthorized?, #otp, #rubygems_api_request, #set_api_key, #sign_in, #update_scope, #verify_api_key, #webauthn_enabled?, #with_response

Methods included from Text

#clean_text, #format_text, #levenshtein_distance, #min3, #truncate_text

Constructor Details

#initialize(options, host) ⇒ WebauthnPoller

Returns a new instance of WebauthnPoller.



29
30
31
32
# File 'lib/rubygems/gemcutter_utilities/webauthn_poller.rb', line 29

def initialize(options, host)
  @options = options
  @host = host
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



27
28
29
# File 'lib/rubygems/gemcutter_utilities/webauthn_poller.rb', line 27

def host
  @host
end

#optionsObject (readonly)

Returns the value of attribute options.



27
28
29
# File 'lib/rubygems/gemcutter_utilities/webauthn_poller.rb', line 27

def options
  @options
end

Class Method Details

.poll_thread(options, host, webauthn_url, credentials) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/rubygems/gemcutter_utilities/webauthn_poller.rb', line 34

def self.poll_thread(options, host, webauthn_url, credentials)
  Thread.new do
    thread = Thread.current
    thread.abort_on_exception = true
    thread.report_on_exception = false
    thread[:otp] = new(options, host).poll_for_otp(webauthn_url, credentials)
  rescue Gem::WebauthnVerificationError, Gem::Timeout::Error => e
    thread[:error] = e
  end
end

Instance Method Details

#poll_for_otp(webauthn_url, credentials) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rubygems/gemcutter_utilities/webauthn_poller.rb', line 45

def poll_for_otp(webauthn_url, credentials)
  Gem::Timeout.timeout(TIMEOUT_IN_SECONDS) do
    loop do
      response = webauthn_verification_poll_response(webauthn_url, credentials)
      raise Gem::WebauthnVerificationError, response.message unless response.is_a?(Gem::Net::HTTPSuccess)

      require "json"
      parsed_response = JSON.parse(response.body)
      case parsed_response["status"]
      when "pending"
        sleep 5
      when "success"
        return parsed_response["code"]
      else
        raise Gem::WebauthnVerificationError, parsed_response.fetch("message", "Invalid response from server")
      end
    end
  end
end