Skip to content

Instantly share code, notes, and snippets.

@1gor
Forked from badosu/auth.rb
Created July 30, 2017 04:49
Show Gist options
  • Save 1gor/b24c46b0c741a3f0e7ad0c215962b4ba to your computer and use it in GitHub Desktop.
Save 1gor/b24c46b0c741a3f0e7ad0c215962b4ba 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