Class: Aptible::CLI::Helpers::SecurityKey::Authenticator

Inherits:
Object
  • Object
show all
Defined in:
lib/aptible/cli/helpers/security_key.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth, pid, out_read, err_read) ⇒ Authenticator

Returns a new instance of Authenticator.



57
58
59
60
61
62
# File 'lib/aptible/cli/helpers/security_key.rb', line 57

def initialize(auth, pid, out_read, err_read)
  @auth = auth
  @pid = pid
  @out_read = out_read
  @err_read = err_read
end

Instance Attribute Details

#pidObject (readonly)

Returns the value of attribute pid.



55
56
57
# File 'lib/aptible/cli/helpers/security_key.rb', line 55

def pid
  @pid
end

Class Method Details

.spawn(auth) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/aptible/cli/helpers/security_key.rb', line 78

def self.spawn(auth)
  in_read, in_write = IO.pipe
  out_read, out_write = IO.pipe
  err_read, err_write = IO.pipe

  pid = Process.spawn(
    'u2f-host', '-aauthenticate', '-o', auth.origin,
    in: in_read, out: out_write, err: err_write,
    close_others: true
  )

  U2F_LOGGER.debug("#{self} #{auth.key_handle}: spawned #{pid}")

  [in_read, out_write, err_write].each(&:close)

  in_write.write(auth.request.to_json)
  in_write.close

  new(auth, pid, out_read, err_read)
end

Instance Method Details

#exited(status) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/aptible/cli/helpers/security_key.rb', line 64

def exited(status)
  out, err = [@out_read, @err_read].map(&:read).map(&:chomp)

  if status.exitstatus == 0
    U2F_LOGGER.info("#{self.class} #{@auth.key_handle}: ok: #{out}")
    [nil, JSON.parse(out)]
  else
    U2F_LOGGER.warn("#{self.class} #{@auth.key_handle}: err: #{err}")
    [ThrottledAuthenticator.spawn(@auth), nil]
  end
ensure
  [@out_read, @err_read].each(&:close)
end