Compare commits

..

No commits in common. "6f4b7335f8fc68165386eb0551ae5f34df0d68a4" and "dfe9b1a90ff1b1a240c2b4457bf610c2a0d9993e" have entirely different histories.

41
app.rb
View File

@ -209,35 +209,6 @@ before do
request.session_options[:skip] = !request.path_info.start_with?("/session") request.session_options[:skip] = !request.path_info.start_with?("/session")
end end
helpers do
def json(obj, opts: nil, pretty: false)
if pretty
JSON.pretty_generate obj, opts:
else
JSON.generate(obj, opts:)
end
end
def protected! hidden = false
return if authorized?
if hidden
halt 404, "Not Found"
else
headers["WWW-Authenticate"] = 'Basic realm="Restricted Area"'
halt 401, "Unauthorized"
end
end
def authorized?
@auth ||= Rack::Auth::Basic::Request.new(request.env)
@auth.provided? and
@auth.basic? and
@auth.credentials and
@auth.credentials == ["qwer", "asdf"]
end
end
get "/" do get "/" do
"hello there!\n" "hello there!\n"
end end
@ -401,15 +372,3 @@ get "/chunked/:delay" do
out << "Hello, world!\n" out << "Hello, world!\n"
end end
end end
route :delete, :get, :patch, :post, :put, "/auth/basic", provides: "json" do
pretty = params.key? :pretty
if params.key? :hidden
protected! hidden: true
else
protected!
end
json({authenticated: true, user: @auth.username}, pretty:)
end