ruby - How do I do a really simple Sinatra LDAP authentication? -


i looked @ sinatra docs , seem reference http authentication. i'm looking simple way control access routes based on user being authorised/authenticated via ldap server.

i've built class ldap bit , returns ldap object if user has authenticated , nil if haven't:

>>directoryuser.authenticate('user', 'password') #<directoryuser:0x007ffb589a2328> 

i can use determine if they've authenticated or not.

as next step want splice simple sinatra app provides form collect ldap user , password:

require 'directoryuser' require 'sinatra'  enable :sessions    '/form'     username        = params[:username]     password     = params[:password]     haml :form   end 

then want allow routes if 'directoryuser' object exists:

get '/protected' # if directoryuser object exists      "this route protected" end  '/unprotected'       "this route unprotected" end 

i've spent hours trying find answer far , can't seem find works me.

i'd go this:

require 'directoryuser' require 'sinatra'  enable :sessions  helpers   def authorize!     redirect(to('/login')) unless session[:user_id]   end end  '/login'   haml :login # login form end  post '/login'   user = directoryuser.authenticate(params[:username], params[:password])    if user     session[:user_id] = user.id     # or: session[:logged_in] = true, depending on needs.     redirect to('/protected')   else     redirect to('/login')   end end  '/protected'   authorize!   'this route protected' end  '/unprotected'     'this route unprotected' end 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -