Skip to content

Instantly share code, notes, and snippets.

@yaotti
Created March 14, 2012 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaotti/2035065 to your computer and use it in GitHub Desktop.
Save yaotti/2035065 to your computer and use it in GitHub Desktop.
sinatraでBasic認証 ref: http://qiita.com/items/3170
helpers do
def protected!
unless authorized?
response['WWW-Authenticate'] = %(Basic realm="Restricted Area")
throw(:halt, [401, "Not authorized\n"])
end
end
def authorized?
@auth ||= Rack::Auth::Basic::Request.new(request.env)
@auth.provided? && @auth.basic? && @auth.credentials && @auth.credentials == ['changeme', 'changeme']
end
end
get '/protected' do
protected!
"Protected page"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment