Skip to content

Commit

Permalink
Rewrite Puma::Acme::Middleware w/o Sinatra
Browse files Browse the repository at this point in the history
  • Loading branch information
danbernier committed Jul 26, 2024
1 parent 8b4e627 commit 4830158
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions lib/puma/acme/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,37 @@
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/'
if path.start_with?(prefix)
path[prefix.size..-1]
end
end
end
end
Expand Down

0 comments on commit 4830158

Please sign in to comment.