1

I'd like to use HTTP Digest authentication for a specific route in my modular Sinatra App.

The examples listed within the sinatra recipes website simply describe how to enable digest auth for an entire app. The solution presented for getting this to work for specific routes is to create two separate apps, (one with digest auth and another without) placing the protected and unprotected routes in their respective applications.

That would require something like the following:

require 'sinatra/base'

class Protected < Sinatra::Base
  use Rack::Auth::Basic, "Protected Area" do |username, password|
    username == 'foo' && password == 'bar'
  end

  get '/' do
    "secret"
  end
end

class Public < Sinatra::Base
  get '/' do
    "public"
  end
end

This seems like overkill to me.

Is there a way to protect a single route in a modular sinatra app without having to create an entirely new app?

There is an example in the FAQ which references creating an instance of Rack::Auth::Basic::Request and passing in the environment but doing something like this with digest auth would differ greatly and be much more of a manual authentication procedure.

Here is the basic authentication example:

def authorized?
  @auth ||=  Rack::Auth::Basic::Request.new(request.env)
  @auth.provided? && 
    @auth.basic? && 
    @auth.credentials &&
    @auth.credentials == ['admin', 'admin']
end

Does anyone have thoughts on how this could be done?

Tyler Brock
  • 29,626
  • 15
  • 79
  • 79

1 Answers1

0

Using a before Filters. Do you need different behaviour for the same route?

vlain
  • 712
  • 7
  • 13