class Vines::Stream::Client::Auth

Constants

AUTH
EXTERNAL
MAX_AUTH_ATTEMPTS
MECHANISM
NS
PLAIN
SUCCESS

Public Class Methods

new(stream, success=BindRestart) click to toggle source
Calls superclass method
# File lib/vines/stream/client/auth.rb, line 15
def initialize(stream, success=BindRestart)
  super
  @attempts = 0
  @sasl = SASL.new(stream)
end

Public Instance Methods

node(node) click to toggle source
# File lib/vines/stream/client/auth.rb, line 21
def node(node)
  raise StreamErrors::NotAuthorized unless auth?(node)
  if node.text.empty?
    send_auth_fail(SaslErrors::MalformedRequest.new)
  elsif stream.authentication_mechanisms.include?(node[MECHANISM])
    case node[MECHANISM]
    when PLAIN    then plain_auth(node)
    when EXTERNAL then external_auth(node)
    end
  else
    send_auth_fail(SaslErrors::InvalidMechanism.new)
  end
end

Private Instance Methods

auth?(node) click to toggle source
# File lib/vines/stream/client/auth.rb, line 37
def auth?(node)
  node.name == AUTH && namespace(node) == NS
end
external_auth(node) click to toggle source
# File lib/vines/stream/client/auth.rb, line 48
def external_auth(node)
  @sasl.external_auth(node.text)
  send_auth_success
rescue => e
  send_auth_fail(e)
  stream.write('</stream:stream>')
  stream.close_connection_after_writing
end
plain_auth(node) click to toggle source
# File lib/vines/stream/client/auth.rb, line 41
def plain_auth(node)
  stream.user = @sasl.plain_auth(node.text)
  send_auth_success
rescue => e
  send_auth_fail(e)
end
send_auth_fail(condition) click to toggle source
# File lib/vines/stream/client/auth.rb, line 63
def send_auth_fail(condition)
  @attempts += 1
  if @attempts >= MAX_AUTH_ATTEMPTS
    stream.error(StreamErrors::PolicyViolation.new("max authentication attempts exceeded"))
  else
    stream.error(condition)
  end
end
send_auth_success() click to toggle source
# File lib/vines/stream/client/auth.rb, line 57
def send_auth_success
  stream.write(SUCCESS)
  stream.reset
  advance
end