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

Inherits:
HrrRbSsh::Authentication::Method show all
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 Method Summary collapse

Methods included from SubclassWithPreferenceListable

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

Constructor Details

#initialize(transport, options, variables, authentication_methods) ⇒ KeyboardInteractive

Returns a new instance of KeyboardInteractive.



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

def initialize transport, options, variables, authentication_methods
  @logger = Logger.new(self.class.name)
  @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



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

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

#request_authentication(username, service_name) ⇒ Object



31
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
# File 'lib/hrr_rb_ssh/authentication/method/keyboard_interactive.rb', line 31

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.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.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.encode message
    @transport.send payload
    @transport.receive
  else
    payload
  end
end