Class: SocksHandler::UsernamePasswordAuthenticator

Inherits:
Object
  • Object
show all
Defined in:
lib/socks_handler/username_password_authenticator.rb

Overview

An implementation of www.ietf.org/rfc/rfc1929.txt

Constant Summary collapse

SUBNEGOTIATION_VERSION =

Returns:

  • (Integer)
0x01

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ UsernamePasswordAuthenticator

Returns a new instance of UsernamePasswordAuthenticator.

Parameters:

  • username (String)
  • password (String)

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
# File 'lib/socks_handler/username_password_authenticator.rb', line 12

def initialize(username, password)
  raise ArgumentError, "Username is too long" if username.bytesize > 256
  raise ArgumentError, "Password is too long" if password.bytesize > 256

  @username = username
  @password = password
end

Instance Method Details

#authenticate(socket) ⇒ nil

Parameters:

  • socket (Socket, TCPSocket)

Returns:

  • (nil)


22
23
24
25
26
27
28
29
# File 'lib/socks_handler/username_password_authenticator.rb', line 22

def authenticate(socket)
  socket.write([SUBNEGOTIATION_VERSION, @username.bytesize, @username, @password.bytesize, @password].pack("CCa*Ca*"))
  _version, status = socket.recv(2).unpack("C*")
  if status != 0x00
    raise AuthenticationFailure, "username: #{@username}, status: #{status}"
  end
  nil
end