-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce auth_chain, abstract authenticator and the basic authenticator
- Loading branch information
1 parent
6bd90cf
commit c294d91
Showing
6 changed files
with
93 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module LavinMQ | ||
module Auth | ||
abstract class Authenticator | ||
Log = LavinMQ::Log.for "auth.handler" | ||
@log = Logger.new(Log) | ||
property successor : Authenticator? | ||
|
||
abstract def authenticate(username : String, password : String) | ||
|
||
def set_successor(service : Authenticator) : Authenticator | ||
@successor = service | ||
service | ||
end | ||
|
||
def try_next(username : String, password : String) | ||
if successor = @successor | ||
successor.authenticate(username, password) | ||
else | ||
nil | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require "../authenticator" | ||
require "../../server" | ||
|
||
module LavinMQ | ||
module Auth | ||
class BasicAuthenticator < LavinMQ::Auth::Authenticator | ||
def initialize(@users : UserStore) | ||
end | ||
|
||
def authenticate(username : String, password : String) | ||
user = @users[username] | ||
return user if user && user.password && user.password.not_nil!.verify(password) | ||
@log.info { "Basic authentication failed" } | ||
try_next(username, password) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require "./authenticator" | ||
require "./authenticators/basic" | ||
|
||
module LavinMQ | ||
module Auth | ||
class Chain | ||
@first : Authenticator? | ||
|
||
def initialize(users : UserStore) | ||
backends = Config.instance.auth_backends | ||
if backends.nil? || backends.size == 0 | ||
add_handler(BasicAuthenticator.new(users)) | ||
else | ||
backends.each do |backend| | ||
case backend | ||
when "basic" | ||
add_handler(BasicAuthenticator.new(users)) | ||
else | ||
raise "Unsupported authentication backend: #{backend}" | ||
end | ||
end | ||
end | ||
end | ||
|
||
def add_handler(auth : Authenticator) | ||
if first = @first | ||
current = first | ||
while successor = current.@successor | ||
current = successor | ||
end | ||
current.set_successor(auth) | ||
else | ||
@first = auth | ||
end | ||
self | ||
end | ||
|
||
def authenticate(username : String, password : String) | ||
@first.try &.authenticate(username, password) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters