Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
059ed64805 | |||
6f4b7335f8 | |||
9f222725d1 |
61
app.rb
61
app.rb
@ -1,6 +1,5 @@
|
||||
require "bundler/setup"
|
||||
require "sinatra"
|
||||
require "sinatra/json"
|
||||
require "sinatra/cookies"
|
||||
require "sinatra/multi_route"
|
||||
require "time"
|
||||
@ -209,23 +208,50 @@ before do
|
||||
request.session_options[:skip] = !request.path_info.start_with?("/session")
|
||||
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
|
||||
"hello there!\n"
|
||||
end
|
||||
|
||||
get "/env", provides: "json" do
|
||||
content_type :json
|
||||
pretty = params.key? :pretty
|
||||
|
||||
return JSON.pretty_generate ENV.sort.to_h if params.key? "pretty"
|
||||
|
||||
JSON.generate ENV.sort.to_h
|
||||
json ENV.sort.to_h, pretty:
|
||||
end
|
||||
|
||||
get "/headers", provides: "json" do
|
||||
pretty = params.key? :pretty
|
||||
h = req_headers
|
||||
return JSON.pretty_generate h if params.key? "pretty"
|
||||
|
||||
JSON.generate h
|
||||
json h, pretty:
|
||||
end
|
||||
|
||||
get "/livez" do
|
||||
@ -239,6 +265,7 @@ end
|
||||
get "/livez/uptime" do
|
||||
tt = TickTock.new
|
||||
x = {started_at: tt.started_at, seconds: tt.uptime.to_i, human: human_time(tt.uptime.to_i)}
|
||||
|
||||
json x
|
||||
end
|
||||
|
||||
@ -301,7 +328,9 @@ post "/halt" do
|
||||
end
|
||||
|
||||
get "/pid" do
|
||||
JSON.generate({puma: master_pid, pid: Process.pid})
|
||||
pretty = params.key? :pretty
|
||||
|
||||
json({puma: master_pid, pid: Process.pid}, pretty:)
|
||||
end
|
||||
|
||||
get "/token" do
|
||||
@ -310,23 +339,27 @@ get "/token" do
|
||||
expires_at = Time.at(exp).to_datetime
|
||||
token = JWT.encode payload, JWT_SECRET, "HS256"
|
||||
x = {token: token, expires_at: expires_at}
|
||||
|
||||
json x
|
||||
end
|
||||
|
||||
get "/token/validate" do
|
||||
token = req_headers["authorization"].split[1]
|
||||
payload = JWT.decode token, JWT_SECRET, true, algorithm: "HS256"
|
||||
|
||||
json payload
|
||||
end
|
||||
|
||||
post "/session" do
|
||||
session.merge! params
|
||||
|
||||
json session.to_hash
|
||||
end
|
||||
|
||||
get "/session" do
|
||||
j = session.to_hash
|
||||
j[:hostname] = ENV["HOSTNAME"]
|
||||
|
||||
json j
|
||||
end
|
||||
|
||||
@ -372,3 +405,15 @@ get "/chunked/:delay" do
|
||||
out << "Hello, world!\n"
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user