Skip to content

Commit

Permalink
Merge pull request #16 from anchordotdev/middleware-wo-sinatra
Browse files Browse the repository at this point in the history
Rewrite Middleware without sinatra
  • Loading branch information
danbernier authored Jul 26, 2024
2 parents 8e35a62 + 9e16680 commit 943bdd9
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 6 deletions.
21 changes: 15 additions & 6 deletions lib/puma/acme/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,34 @@
module Puma
module Acme
# ACME challenge response handler for HTTP-01 challenges.
class Middleware < Sinatra::Base
class Middleware
def initialize(app, manager:)
@app = app
@manager = manager
end

super(app)
def call(env)
dup._call(env)
end

get '/.well-known/acme-challenge/:token' do
if (token = params[:token]).nil?
def _call(env)
if (token = token(env)).nil?
return @app.call(env)
end

if (answer = @manager.answer(type: CHALLENGE_TYPE, token: token)).nil?
return @app.call(env)
end

content_type 'text/plain'
answer.value
[200, {'content-type' => 'text/plain;charset=utf-8'}, [answer.value]]
end

private

def token(env)
path = ::Rack::Request.new(env).path_info
prefix = '/.well-known/acme-challenge/'
path[prefix.size..-1] if path.start_with?(prefix)
end
end
end
Expand Down
1 change: 1 addition & 0 deletions puma-acme.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'minitest', '~> 5.14'
s.add_development_dependency 'minitest-mock_expectations', '~> 1.2'
s.add_development_dependency 'r509', '~> 1.0'
s.add_development_dependency 'rack-test'
s.add_development_dependency 'rake', '~> 13.0'
s.add_development_dependency 'vcr', '~> 6.1'
s.add_development_dependency 'webmock', '~> 3.19'
Expand Down
57 changes: 57 additions & 0 deletions test/puma/acme/middleware_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

require_relative '../.././test_helper'
require "rack/test"

module Puma
module Acme
class MiddlewareTest < Minitest::Test
include ::Rack::Test::Methods

def test_it_returns_the_manager_answer_value_when_present
get '/.well-known/acme-challenge/good-token'
assert_equal '42', last_response.body
assert_equal 'text/plain;charset=utf-8', last_response.headers['content-type']
end

def test_it_passes_through_if_manager_provides_no_answer
get '/.well-known/acme-challenge/unknown-token'
assert_equal 'request not handled by middleware', last_response.body
end

def test_it_passes_unrecognized_requests_through
get '/'
assert_equal 'request not handled by middleware', last_response.body

get '/any-other/path'
assert_equal 'request not handled by middleware', last_response.body
end

class App
def call(env)
[200, {}, ['request not handled by middleware']]
end
end

class FakeManager
class Answer
def initialize(value)
@value = value
end
attr_reader :value
end

def answer(type:, token:)
Answer.new('42') if token == 'good-token'
end
end

def app
Puma::Acme::Middleware.new(
App.new,
manager: FakeManager.new
)
end
end
end
end

0 comments on commit 943bdd9

Please sign in to comment.