Created
September 6, 2021 19:09
-
-
Save Dan-Q/a708b275778a821bc71eb12cfaa72fc8 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
require 'bundler' | |
Bundler.require | |
require 'sinatra' | |
set :bind, '0.0.0.0' | |
set :port, 80 | |
def show_credentials(request) | |
<<-EOF | |
Header: #{request.env['HTTP_AUTHORIZATION']} | |
<br> | |
<a href="/">Home</a> | |
EOF | |
end | |
get '/' do | |
<<-EOF | |
<a href="/optional">Optional auth</a> | |
<br> | |
<a href="/mandatory">Mandatory auth</a> | |
EOF | |
end | |
get '/optional' do | |
show_credentials(request) | |
end | |
get '/mandatory' do | |
if(!request.env["HTTP_AUTHORIZATION"]) | |
response['WWW-Authenticate'] = 'Basic realm="Test Site"' | |
halt 401, "You need to authenticate!" | |
else | |
show_credentials(request) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment