Skip to content

Instantly share code, notes, and snippets.

@Dan-Q
Created September 6, 2021 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Dan-Q/a708b275778a821bc71eb12cfaa72fc8 to your computer and use it in GitHub Desktop.
Save Dan-Q/a708b275778a821bc71eb12cfaa72fc8 to your computer and use it in GitHub Desktop.
#!/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