Sha256: 4d85c1efd864b54b650c3e751e417661708aa7812f6b71633efd1c5f5eac3f19

Contents?: true

Size: 916 Bytes

Versions: 8

Compression:

Stored size: 916 Bytes

Contents

module RSocks

  class Authenticator

    def initialize(adaptor = nil)
      @default_user = ENV['RSOCKS_USER'] || 'default'
      @default_password = ENV['RSOCKS_PASSWORD'] || 'default'
      @adaptor = nil
    end

    def auth!(data)
      return false if data.unpack('C')[0] != RSocks::AUTH_HEADER
      validate(data[1..-1])
    end

    private

    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

    def get_username(data)
      name_size = data.unpack('C')[0]
      username = data[1..name_size]
      [username, data[(name_size + 1)..-1]]
    end

    def get_password(data)
      password_size = data.unpack('C')[0]
      password = data[1..password_size]
      password
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
r_socks-0.1.8 lib/r_socks/authenticator.rb
r_socks-0.1.7 lib/r_socks/authenticator.rb
r_socks-0.1.6 lib/r_socks/authenticator.rb
r_socks-0.1.5 lib/r_socks/authenticator.rb
r_socks-0.1.4 lib/r_socks/authenticator.rb
r_socks-0.1.3 lib/r_socks/authenticator.rb
r_socks-0.1.2 lib/r_socks/authenticator.rb
r_socks-0.1.1 lib/r_socks/authenticator.rb