Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/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