Skip to content

Instantly share code, notes, and snippets.

@badosu
Created March 23, 2016 19:00
Show Gist options
  • Save badosu/b40ea12b033178dceaf0 to your computer and use it in GitHub Desktop.
Save badosu/b40ea12b033178dceaf0 to your computer and use it in GitHub Desktop.
Basic HTTP Auth with Roda
auth = Rack::Auth::Basic::Request.new(env)
unless auth.provided?
# env['warden'].custom_failure! # If you're using warden avoid intercepting 401
r.halt [401, {'Content-Type' => 'text/plain', 'Content-Length' => '0', 'WWW-Authenticate' => 'Basic realm="Restricted Area"'}, []]
end
unless auth.basic?
r.halt [400, {'Content-Type' => 'text/plain', 'Content-Length' => '0'}, []]
end
if auth.credentials == %w(admin abc123)
env['REMOTE_USER'] = auth.username
else
# env['warden'].custom_failure! # If you're using warden avoid intercepting 401
r.halt [401, {'Content-Type' => 'text/plain', 'Content-Length' => '0', 'WWW-Authenticate' => 'Basic realm="Restricted Area"'}, []]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment