title: How do I do a really simple Sinatra LDAP authentication?
body: <p>I looked at the Sinatra docs and they only seem to reference HTTP authentication. I'm looking for a really simple way to control access to routes based on a user being authorised/authenticated via an LDAP server. </p>
  
  <p>I've already built a class that does the LDAP bit and returns an LDAP object if the user has successfully authenticated and nil if they haven't:</p>
  
  <pre><code>&gt;&gt;DirectoryUser.authenticate('user', 'password')
  #&lt;DirectoryUser:0x007ffb589a2328&gt;
  </code></pre>
  
  <p>I can use this to determine if they've successfully authenticated or not. </p>
  
  <p>As a next step I want to splice this into a simple Sinatra app that provides a form to collect the LDAP user and password:</p>
  
  <pre><code>require 'directoryUser'
  require 'sinatra'
  
  enable :sessions
  
    get '/form' do
      username        = params[:username]
      password     = params[:password]
      haml :form
    end
  </code></pre>
  
  <p>Then I want to only allow routes if the 'DirectoryUser' object exists:</p>
  
  <pre><code>get '/protected' do # Only if DirectoryUser object exists 
      "This route is protected"
  end
  
  get '/unprotected' do  
      "This route is unprotected"
  end
  </code></pre>
  
  <p>I've spent hours trying to find an answer to this but so far and can't seem to find anything that works for me. </p>