module Dalli::Protocol::Binary::SaslAuthentication

Code to support SASL authentication

Constants

PLAIN_AUTH

Public Instance Methods

authenticate_connection() click to toggle source
# File lib/dalli/protocol/binary/sasl_authentication.rb, line 39
def authenticate_connection
  Dalli.logger.info { "Dalli/SASL authenticating as #{username}" }

  status, mechanisms = perform_auth_negotiation
  return Dalli.logger.debug('Authentication not required/supported by server') if status == 0x81

  supported_mechanisms!(mechanisms)
  status, content = authenticate_with_plain

  return Dalli.logger.info("Dalli/SASL: #{content}") if status.zero?

  raise Dalli::DalliError, "Error authenticating: 0x#{status.to_s(16)}" unless status == 0x21

  raise NotImplementedError, 'No two-step authentication mechanisms supported'
  # (step, msg) = sasl.receive('challenge', content)
  # raise Dalli::NetworkError, "Authentication failed" if sasl.failed? || step != 'response'
end
authenticate_with_plain() click to toggle source
# File lib/dalli/protocol/binary/sasl_authentication.rb, line 32
def authenticate_with_plain
  write(RequestFormatter.standard_request(opkey: :auth_request,
                                          key: PLAIN_AUTH,
                                          value: "\x0#{username}\x0#{password}"))
  @response_processor.auth_response
end
perform_auth_negotiation() click to toggle source
# File lib/dalli/protocol/binary/sasl_authentication.rb, line 9
def perform_auth_negotiation
  write(RequestFormatter.standard_request(opkey: :auth_negotiation))

  status, content = response_processor.auth_response
  return [status, []] if content.nil?

  # Substitute spaces for the \x00 returned by
  # memcached as a separator for easier
  content&.tr("\u0000", ' ')
  mechanisms = content&.split
  [status, mechanisms]
end
supported_mechanisms!(mechanisms) click to toggle source
# File lib/dalli/protocol/binary/sasl_authentication.rb, line 24
def supported_mechanisms!(mechanisms)
  unless mechanisms.include?(PLAIN_AUTH)
    raise NotImplementedError,
          'Dalli only supports the PLAIN authentication mechanism'
  end
  [PLAIN_AUTH]
end