class RSocks::Authenticator

Attributes

password[R]
username[R]

Public Class Methods

new(adaptor = nil) click to toggle source
# File lib/r_socks/authenticator.rb, line 7
def initialize(adaptor = nil)
  @default_user = ENV['RSOCKS_USER'] || 'default'
  @default_password = ENV['RSOCKS_PASSWORD'] || 'default'
  @adaptor = adaptor
end

Public Instance Methods

auth!(data) click to toggle source
# File lib/r_socks/authenticator.rb, line 13
def auth!(data)
  return false if data.unpack('C')[0] != RSocks::AUTH_HEADER
  validate(data[1..-1])
end

Private Instance Methods

get_password(data) click to toggle source
# File lib/r_socks/authenticator.rb, line 37
def get_password(data)
  password_size = data.unpack('C')[0]
  password = data[1..password_size]
  password
end
get_username(data) click to toggle source
# File lib/r_socks/authenticator.rb, line 31
def get_username(data)
  name_size = data.unpack('C')[0]
  username = data[1..name_size]
  [username, data[(name_size + 1)..-1]]
end
validate(data) click to toggle source
# File lib/r_socks/authenticator.rb, line 20
def validate(data)
  @username, remain = get_username(data)
  @password = get_password(remain)

  if @adaptor.nil?
    @username == @default_user && @password == @default_password
  else
    @adaptor.call(@username, @password)
  end
end