Skip to content

Commit

Permalink
use same userstore as connection_factory for authenticator
Browse files Browse the repository at this point in the history
  • Loading branch information
kickster97 committed Jan 22, 2025
1 parent c294d91 commit fc4c424
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
11 changes: 7 additions & 4 deletions src/lavinmq/amqp/connection_factory.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ require "../version"
require "../logger"
require "./client"
require "../client/connection_factory"
require "../auth/chain"

module LavinMQ
module AMQP
class ConnectionFactory < LavinMQ::ConnectionFactory
Log = LavinMQ::Log.for "amqp.connection_factory"

def start(socket, connection_info, vhosts, users, auth_chain) : Client?
def start(socket, connection_info, vhosts, users) : Client?
@auth_chain = LavinMQ::Auth::Chain.new(users)
remote_address = connection_info.src
socket.read_timeout = 15.seconds
metadata = ::Log::Metadata.build({address: remote_address.to_s})
logger = Logger.new(Log, metadata)
if confirm_header(socket, logger)
if start_ok = start(socket, logger)
if user = authenticate(socket, remote_address, users, start_ok, logger, auth_chain)
if user = authenticate(socket, remote_address, users, start_ok, logger)
if tune_ok = tune(socket, logger)
if vhost = open(socket, vhosts, user, logger)
socket.read_timeout = heartbeat_timeout(tune_ok)
Expand Down Expand Up @@ -100,10 +102,11 @@ module LavinMQ
end
end

def authenticate(socket, remote_address, users, start_ok, log, auth_chain)
def authenticate(socket, remote_address, users, start_ok, log)
username, password = credentials(start_ok)
user = users[username]?
return user if user && auth_chain.authenticate(username, password) &&
# TODO: Do another PR that adds a constructor to this class to avoid the nil-guard here
return user if user && @auth_chain.try &.authenticate(username, password) &&
guest_only_loopback?(remote_address, user)

if user.nil?
Expand Down
4 changes: 1 addition & 3 deletions src/lavinmq/server.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ require "./client/client"
require "./client/connection_factory"
require "./amqp/connection_factory"
require "./stats"
require "./auth/chain"

module LavinMQ
class Server
Expand All @@ -39,7 +38,6 @@ module LavinMQ
@vhosts = VHostStore.new(@data_dir, @users, @replicator)
@parameters = ParameterStore(Parameter).new(@data_dir, "parameters.json", @replicator)
@amqp_connection_factory = LavinMQ::AMQP::ConnectionFactory.new
@auth_chain = LavinMQ::Auth::Chain.new(@users)
apply_parameter
spawn stats_loop, name: "Server#stats_loop"
end
Expand Down Expand Up @@ -247,7 +245,7 @@ module LavinMQ
end

def handle_connection(socket, connection_info)
client = @amqp_connection_factory.start(socket, connection_info, @vhosts, @users, @auth_chain)
client = @amqp_connection_factory.start(socket, connection_info, @vhosts, @users)
ensure
socket.close if client.nil?
end
Expand Down

0 comments on commit fc4c424

Please sign in to comment.