Skip to content

Instantly share code, notes, and snippets.

@arpitchauhan
Last active September 11, 2018 19:56
Show Gist options
  • Save arpitchauhan/2b74926c6c7381e9421b67d04e0d230d to your computer and use it in GitHub Desktop.
Save arpitchauhan/2b74926c6c7381e9421b67d04e0d230d to your computer and use it in GitHub Desktop.
sinatra-app-to-host-files > basic_auth.rb
class BasicAuth
class << self
def enable_if_possible
return unless on?
Sinatra::Application.use(Rack::Auth::Basic) do |username, password|
valid_credentials?(username, password)
end
end
private
# Basic auth is on only if both username and password are set
def on?
correct_username && correct_password
end
def valid_credentials?(username, password)
raise 'Arguments missing' unless username && password
username == correct_username && password == correct_password
end
def correct_username
ENV['BASIC_AUTH_USERNAME']
end
def correct_password
ENV['BASIC_AUTH_PASSWORD']
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment