Implements the “publickey” SSH authentication method.
Attempts to perform public-key authentication for the given username, trying each identity known to the key manager. If any of them succeed, returns true, otherwise returns false. This requires the presence of a key manager.
# File lib/net/ssh/authentication/methods/publickey.rb, line 16 16: def authenticate(next_service, username, password=nil) 17: return false unless key_manager 18: 19: key_manager.each_identity do |identity| 20: return true if authenticate_with(identity, next_service, username) 21: end 22: 23: return false 24: end
Attempts to perform public-key authentication for the given username, with the given identity (public key). Returns true if successful, or false otherwise.
# File lib/net/ssh/authentication/methods/publickey.rb, line 49 49: def authenticate_with(identity, next_service, username) 50: debug { "trying publickey (#{identity.fingerprint})" } 51: send_request(identity, username, next_service) 52: 53: message = session.next_message 54: 55: case message.type 56: when USERAUTH_PK_OK 57: buffer = build_request(identity, username, next_service, true) 58: sig_data = Net::SSH::Buffer.new 59: sig_data.write_string(session_id) 60: sig_data.append(buffer.to_s) 61: 62: sig_blob = key_manager.sign(identity, sig_data) 63: 64: send_request(identity, username, next_service, sig_blob.to_s) 65: message = session.next_message 66: 67: case message.type 68: when USERAUTH_SUCCESS 69: debug { "publickey succeeded (#{identity.fingerprint})" } 70: return true 71: when USERAUTH_FAILURE 72: debug { "publickey failed (#{identity.fingerprint})" } 73: 74: raise Net::SSH::Authentication::DisallowedMethod unless 75: message[:authentications].split(/,/).include? 'publickey' 76: 77: return false 78: else 79: raise Net::SSH::Exception, 80: "unexpected server response to USERAUTH_REQUEST: #{message.type} (#{message.inspect})" 81: end 82: 83: when USERAUTH_FAILURE 84: return false 85: 86: else 87: raise Net::SSH::Exception, "unexpected reply to USERAUTH_REQUEST: #{message.type} (#{message.inspect})" 88: end 89: end
Builds a packet that contains the request formatted for sending a public-key request to the server.
# File lib/net/ssh/authentication/methods/publickey.rb, line 30 30: def build_request(pub_key, username, next_service, has_sig) 31: blob = Net::SSH::Buffer.new 32: blob.write_key pub_key 33: 34: userauth_request(username, next_service, "publickey", has_sig, 35: pub_key.ssh_type, blob.to_s) 36: end
Builds and sends a request formatted for a public-key authentication request.
# File lib/net/ssh/authentication/methods/publickey.rb, line 40 40: def send_request(pub_key, username, next_service, signature=nil) 41: msg = build_request(pub_key, username, next_service, !signature.nil?) 42: msg.write_string(signature) if signature 43: send_message(msg) 44: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.