-
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 --------- Co-authored-by: Anders Bälter <[email protected]> Co-authored-by: Jon Börjesson <[email protected]>
- Loading branch information
1 parent
5537a67
commit 2a21b8c
Showing
7 changed files
with
99 additions
and
1 deletion.
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,29 @@ | ||
require "./spec_helper" | ||
require "../src/lavinmq/auth/chain" | ||
require "../src/lavinmq/auth/authenticators/basic" | ||
|
||
describe LavinMQ::Auth::Chain do | ||
it "Creates a default authentication chain if not configured" do | ||
with_amqp_server do |s| | ||
chain = LavinMQ::Auth::Chain.create(s.@config, s.@users) | ||
chain.@backends.should be_a Array(LavinMQ::Auth::Authenticator) | ||
chain.@backends.size.should eq 1 | ||
end | ||
end | ||
|
||
it "Successfully authenticates and returns a basic user" do | ||
with_amqp_server do |s| | ||
chain = LavinMQ::Auth::Chain.create(s.@config, s.@users) | ||
user = chain.authenticate("guest", "guest") | ||
user.should_not be_nil | ||
end | ||
end | ||
|
||
it "Does not authenticate when given invalid credentials" do | ||
with_amqp_server do |s| | ||
chain = LavinMQ::Auth::Chain.create(s.@config, s.@users) | ||
user = chain.authenticate("guest", "invalid") | ||
user.should be_nil | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module LavinMQ | ||
module Auth | ||
abstract class Authenticator | ||
Log = LavinMQ::Log.for "auth.handler" | ||
|
||
abstract def authenticate(username : String, password : String) : User? | ||
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 < Authenticator | ||
def initialize(@users : UserStore) | ||
end | ||
|
||
def authenticate(username : String, password : String) : User? | ||
user = @users[username] | ||
return user if user && user.password && user.password.not_nil!.verify(password) | ||
rescue ex : Exception | ||
Log.error { "Basic authentication failed: #{ex.message}" } | ||
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,38 @@ | ||
require "./authenticator" | ||
require "./authenticators/basic" | ||
|
||
module LavinMQ | ||
module Auth | ||
class Chain < Authenticator | ||
@backends : Array(Authenticator) | ||
|
||
def initialize(backends : Array(Authenticator)) | ||
@backends = backends | ||
end | ||
|
||
def self.create(config : Config, users : UserStore) : Chain | ||
backends = config.auth_backends | ||
authenticators = Array(Authenticator).new | ||
if backends.nil? || backends.empty? | ||
authenticators << BasicAuthenticator.new(users) | ||
else | ||
backends.each do |backend| | ||
case backend | ||
when "basic" | ||
authenticators << BasicAuthenticator.new(users) | ||
else | ||
raise "Unsupported authentication backend: #{backend}" | ||
end | ||
end | ||
end | ||
self.new(authenticators) | ||
end | ||
|
||
def authenticate(username : String, password : String) : User? | ||
@backends.find_value do |backend| | ||
backend.authenticate(username, password) | ||
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