-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
376 additions
and
2 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,18 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rack | ||
# @api public | ||
# @since 0.1.0 | ||
class BlastWave::BlackList < BlastWave::CheckList | ||
BlastWave::CheckList::Builder.build(self) | ||
|
||
# @param env [Hash] | ||
# @return [Object] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def call(env) | ||
check!(env) ? generate_fail_response! : super | ||
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,14 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rack | ||
# @api public | ||
# @since 0.1.0 | ||
BlastWave::CheckList = Class.new(BlastWave::Middleware) | ||
|
||
require_relative 'check_list/matcher' | ||
require_relative 'check_list/matcher_registry' | ||
require_relative 'check_list/fail_response' | ||
require_relative 'check_list/checkable' | ||
require_relative 'check_list/checker' | ||
require_relative 'check_list/builder' | ||
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,73 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rack | ||
# @api public | ||
# @since 0.1.0 | ||
module BlastWave::CheckList::Builder | ||
# @return [Proc] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
INSTRUCTIONS = proc do | ||
# @since 0.1.0 | ||
include Qonfig::Configurable | ||
# @since 0.1.0 | ||
include BlastWave::CheckList::Checkable | ||
|
||
# @since 0.1.0 | ||
configuration do | ||
setting :check_all, false | ||
|
||
setting :fail_response do | ||
setting :body, BlastWave::CheckList::FailResponse::DEFAULT_BODY | ||
setting :status, BlastWave::CheckList::FailResponse::DEFAULT_STATUS | ||
setting :headers, BlastWave::CheckList::FailResponse::DEFAULT_HEADERS | ||
end | ||
end | ||
|
||
private | ||
|
||
# @return [Qonfig::Settings] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def options | ||
self.class.config.settings | ||
end | ||
|
||
# @param env [Hash] | ||
# @return [Boolean] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def check!(env) | ||
request = Rack::Request.new(env) | ||
should_check_all = options.check_all? | ||
checker.check!(request, check_all: should_check_all) | ||
end | ||
|
||
# @return [Rack::Response] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def generate_fail_response! | ||
FailResponse.build( | ||
status: options.fail_response.status, | ||
body: options.fail_response.body, | ||
headers: options.fail_response.headers | ||
) | ||
end | ||
end | ||
|
||
class << self | ||
# @param empty_middleware_klass [Class<BlastWave::MiddleWare>] | ||
# @return [Class<BlastWave::MiddleWare>] | ||
# | ||
# @api public | ||
# @since 0.1.0 | ||
def build(empty_middleware_klass = Class.new(BlastWave::Middleware)) | ||
empty_middleware_klass.class_eval(&INSTRUCTIONS) | ||
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,46 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rack | ||
# @api private | ||
# @since 0.1.0 | ||
module BlastWave::CheckList::Checkable | ||
class << self | ||
# @param base_klass [Class] | ||
# @return [void] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def included(base_klass) | ||
base_klass.extend(ClassMethods) | ||
base_klass.include(InstanceMethods) | ||
base_klass.instance_variable_set(:@checker, BlastWave::CheckList::Checker.new) | ||
base_klass.singleton_class.class_eval { attr_reader :checker } | ||
end | ||
end | ||
|
||
# @api private | ||
# @since 0.1.0 | ||
module ClassMethods | ||
# @param block [Proc] | ||
# @return [void] | ||
# | ||
# @api public | ||
# @since 0.1.0 | ||
def match_by(&block) | ||
checker.register(&block) | ||
end | ||
end | ||
|
||
# @api private | ||
# @since 0.1.0 | ||
module InstanceMethods | ||
# @return [BlastWave::CheckLis::Checker] | ||
# | ||
# @api private | ||
# @sicne 0.1.0 | ||
def checker | ||
self.class.checker | ||
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,60 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rack | ||
# @api private | ||
# @since 0.1.0 | ||
class BlastWave::CheckList::Checker | ||
# @return [void] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def initialize | ||
@matcher_registry = BlastWave::CheckList::MatcherRegistry.new | ||
end | ||
|
||
# @param request [Rack::Request] | ||
# @option check_all [Boolean] | ||
# @return [Boolean] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def check!(request, check_all: false) | ||
check_all ? all?(request) : any?(request) | ||
end | ||
|
||
# @param block [Proc] | ||
# @return [void] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def register(&block) | ||
matcher_registry.register(BlastWave::CheckList::Matcher.new(block)) | ||
end | ||
|
||
private | ||
|
||
# @return [BlastWave::CheckList::MatcherRegistry] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
attr_reader :matcher_registry | ||
|
||
# @param request [Rack::Request] | ||
# @return [Boolean] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def any?(request) | ||
matcher_registry.all?(request) | ||
end | ||
|
||
# @param request [Rack::Request] | ||
# @return [Boolean] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def all?(request) | ||
matcher_registry.any?(request) | ||
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,49 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rack | ||
# @api private | ||
# @since 0.1.0 | ||
class BlastWave::CheckList::FailResponse < Rack::Response | ||
# @return [Array<String>] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
DEFAULT_BODY = ['Forbidden'].freeze | ||
|
||
# @return [Hash] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
DEFAULT_HEADERS = { 'Content-Type' => 'text/plain' }.freeze | ||
|
||
# @return [Integer] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
DEFAULT_STATUS = 403 | ||
|
||
class << self | ||
# @option status [Integer] | ||
# @option body [Array<String>] | ||
# @option headers [Hash] | ||
# @return [Rack::Response] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def build(status: DEFAULT_STATUS, body: DEFAULT_BODY, headers: DEFAULT_HEADERS) | ||
new(body, status, headers) | ||
end | ||
end | ||
|
||
# @param body [Array<String>] | ||
# @param status [Integer] | ||
# @param header [Hash] | ||
# @return [void] | ||
# | ||
# @since 0.1.0 | ||
# @api private | ||
def initialize(body = DEFAULT_BODY, status = DEFAULT_STATUS, header = DEFAULT_HEADERS) | ||
super | ||
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,31 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rack | ||
# @api private | ||
# @since 0.1.0 | ||
class BlastWave::CheckList::Matcher | ||
# @return [Object] #call-able object | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
attr_reader :matcher | ||
|
||
# @param matcher [Object] #call-able object | ||
# @return [void] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def initialize(matcher) | ||
@matcher = matcher | ||
end | ||
|
||
# @param request [Rack::Request] | ||
# @return [Boolean] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def match?(request) | ||
!!matcher.call(request) | ||
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,51 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rack | ||
# @api private | ||
# @since 0.1.0 | ||
class BlastWave::CheckList::MatcherRegistry | ||
# @since 0.1.0 | ||
include Enumerable | ||
|
||
# @return [void] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def initialize | ||
@list = [] | ||
@lock = Mutex.new | ||
end | ||
|
||
# @param matcher [BlastWave::CheckList::Matcher] | ||
# @return [void] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def register(matcher) | ||
lock.synchronize { list << matcher } | ||
end | ||
|
||
# @param block [Proc] | ||
# @return [void] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def each(&block) | ||
block_given? ? list.each(&block) : list.each | ||
end | ||
|
||
private | ||
|
||
# @return [Array] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
attr_reader :list | ||
|
||
# @return [Mutex] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
attr_reader :lock | ||
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,18 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rack | ||
# @api public | ||
# @since 0.1.0 | ||
class BlastWave::WhiteList < BlastWave::CheckList | ||
BlastWave::CheckList::Builder.build(self) | ||
|
||
# @param env [Hash] | ||
# @return [Object] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def call(env) | ||
check!(env) ? super : generate_fail_response! | ||
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,5 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe 'Rack::BlastWave::BlackList Middleware' do | ||
let!(:app) { build_fake_rack_app(Rack::BlastWave::BlackList) } | ||
end |
Oops, something went wrong.