Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/net/ssh/pubkey_verifier.rb
Views: 11780
require 'net/ssh'12module Net3module SSH45class PubkeyVerifier6include Net::SSH::Transport::Constants7include Net::SSH::Authentication::Constants89attr_accessor :connection, :host, :key, :options, :user1011def initialize(host,user, opts)12@host = host13# Parse public key data out into a PKey object14pubkey_data = opts.fetch(:key_data)15@key = Net::SSH::KeyFactory.load_data_public_key(pubkey_data)16@user = user1718# Always set auth methods to ONLY publickey regardless19# of what the user sends20opts[:auth_methods] = ['publickey']21@options = Net::SSH.configuration_for(host, opts.fetch(:config, true)).merge(opts)22end2324def auth_session(transport)25Net::SSH::Authentication::Session.new(transport,options)26end2728def ssh_transport29Net::SSH::Transport::Session.new(host,options)30end3132def verify33transport = ssh_transport34auth = auth_session(transport)3536transport.send_message(transport.service_request("ssh-userauth"))37auth.expect_message(SERVICE_ACCEPT)3839# The initial public key exchange40pubkey_method = Net::SSH::Authentication::Methods::Publickey.new(auth)41pubkey_method.send(:send_request, key, user, "ssh-connection", key.ssh_type)4243# Check the response to see if the public key is good44response_message = auth.next_message45case response_message.type46when USERAUTH_PK_OK47@connection = Net::SSH::Connection::Session.new(transport, options)48true49when USERAUTH_FAILURE50false51else52raise Net::SSH::Exception, "unexpected reply to USERAUTH_REQUEST: #{response_message.type} (#{response_message.inspect})"53end54end555657end58end59end606162