Skip to content

Old rails integration

clyfe edited this page Nov 1, 2010 · 2 revisions

These ideas are deprecated

Here is how to integrate dav4rack with Warden for authentication.
This is useful for example to serve per user files. Background: http://stackoverflow.com/questions/4009082/rails-3-http-extensions-webdav-and-rack-app-mounting
Minimal rails 3 app working in the above mentioned manner: http://github.com/clyfe/rails3-dav4rack-example

  1. Make sure the dav4rack handler is mounted below warden middleware. Example:
Rack::Builder.new do
  use Rack::Session::Cookie, :secret => "replace this with some secret key"

  use Warden::Manager do |manager|
    manager.default_strategies :password, :basic
    manager.failure_app = BadAuthenticationEndsUpHere
  end
  
  map '/webdav/' do
    run DAV4Rack::Handler.new(
      :root => '/home/somewhere', :resource_class => CustomResource
    )
  end

  map '/' do
    use Webdav::Interceptor, :mappings => {
      '/webdav/' => { :resource_class => CustomResource },
    }
    run SomeApp
  end
end
  1. The @Resource#authenticate@ method, if present, is used for authentication
class CustomResource < DAV4Rack::Resource
    def authenticate(user, pass)
      @user = request.env['warden'].authenticate :scope => :user
      @user.present?
    end
    ...
    # we can use the user object to dinamically fetch files
    def get # example, say User has many :documents
      document = @user.documents.find(path)
      response.body = document.attachment.read
    end
end