Class: HrrRbSsh::Authentication::Method::KeyboardInteractive

Inherits:
HrrRbSsh::Authentication::Method show all
Includes:
Loggable
Defined in:
lib/hrr_rb_ssh/authentication/method/keyboard_interactive.rb,
lib/hrr_rb_ssh/authentication/method/keyboard_interactive/context.rb,
lib/hrr_rb_ssh/authentication/method/keyboard_interactive/info_request.rb,
lib/hrr_rb_ssh/authentication/method/keyboard_interactive/info_response.rb

Defined Under Namespace

Classes: Context, InfoRequest, InfoResponse

Constant Summary collapse

NAME =
'keyboard-interactive'
PREFERENCE =
30

Instance Attribute Summary

Attributes included from Loggable

#log_key, #logger

Instance Method Summary collapse

Methods included from Loggable

#log_debug, #log_error, #log_fatal, #log_info, #log_warn

Methods included from SubclassWithPreferenceListable

#[], #inherited, #list_preferred, #list_supported

Constructor Details

#initialize(transport, options, variables, authentication_methods, logger: nil) ⇒ KeyboardInteractive

Returns a new instance of KeyboardInteractive.



15
16
17
18
19
20
21
22
# File 'lib/hrr_rb_ssh/authentication/method/keyboard_interactive.rb', line 15

def initialize transport, options, variables, authentication_methods, logger: nil
  self.logger = logger
  @transport = transport
  @options = options
  @authenticator = options.fetch( 'authentication_keyboard_interactive_authenticator', Authenticator.new{ false } )
  @variables = variables
  @authentication_methods = authentication_methods
end

Instance Method Details

#authenticate(userauth_request_message) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/hrr_rb_ssh/authentication/method/keyboard_interactive.rb', line 24

def authenticate userauth_request_message
  log_info { "authenticate" }
  username = userauth_request_message[:'user name']
  submethods = userauth_request_message[:'submethods']
  context = Context.new(@transport, username, submethods, @variables, @authentication_methods, logger: logger)
  @authenticator.authenticate context
end

#request_authentication(username, service_name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/hrr_rb_ssh/authentication/method/keyboard_interactive.rb', line 32

def request_authentication username, service_name
  message = {
    :'message number' => Message::SSH_MSG_USERAUTH_REQUEST::VALUE,
    :"user name"      => username,
    :"service name"   => service_name,
    :"method name"    => NAME,
    :"language tag"   => "",
    :'submethods'     => "",
  }
  payload = Message::SSH_MSG_USERAUTH_REQUEST.new(logger: logger).encode message
  @transport.send payload

  payload = @transport.receive
  case payload[0,1].unpack("C")[0]
  when Message::SSH_MSG_USERAUTH_INFO_REQUEST::VALUE
    message = Message::SSH_MSG_USERAUTH_INFO_REQUEST.new(logger: logger).decode payload
    num_responses = @options['client_authentication_keyboard_interactive'].size
    message = {
      :'message number' => Message::SSH_MSG_USERAUTH_INFO_RESPONSE::VALUE,
      :'num-responses'  => num_responses,
    }
    message_responses = @options['client_authentication_keyboard_interactive'].map.with_index{ |response, i|
      {:"response[#{i+1}]" => response}
    }.inject(Hash.new){ |a, b| a.merge(b) }
    message.update(message_responses)
    payload = Message::SSH_MSG_USERAUTH_INFO_RESPONSE.new(logger: logger).encode message
    @transport.send payload
    @transport.receive
  else
    payload
  end
end