2011-08-09 44 views
1

當我們在Sinatra應用程序中的一組受保護的URL(管理部分)後面重定向時,我偶然發現了一些問題。這很可能是一個愚蠢的錯誤,但我沒有發現任何網上幫助。Sinatra的Post/Redirect Auth問題

這是爲助手顯示的密碼保護區域,用戶可以在其中創建新事件。用戶第一次嘗試訪問管理員時,會提示他們輸入密碼,然後留下後續頁面。我遇到的問題是,當應用在成功創建新事件後嘗試重定向時,用戶必須重新認證自己......這似乎有點冗餘。

這也適用於刪除和編輯過程,當嘗試重定向時總是提示用戶。我試過路過在第二個參數用於不同的HTTP代碼,但無濟於事

總之,這裏的代碼,如有問題/幫助將不勝感激

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 == ['admin', 'admin'] 
    end 
end 
... 
get "/admin/events/:id" do 
    protected! 
    conf = Conference.where(:_id => params[:id]).first 
    not_found unless conf 
    haml :admin_event_edit, :layout => :admin_layout, :locals => { :event => conf } 
end 

post "/admin/events/new/" do 
    protected! 
    conf = Conference.new(params[:event]) 
    if conf.save! 
     redirect "/admin/events/" 
    else 
     "Something went horribly wrong creating the new event, heres the form contents #{params.inspect}" 
    end 
end 

get "/admin/events/" do 
    protected! 
    haml :admin_events, :layout => :admin_layout, :locals => { :our_events => Conference.where(:made => true).order_by(:start_date.asc).limit(15), :other_events => Conference.where(:made => false).order_by(:start_date.asc).limit(15)} 
end 

回答

0

這是唯一發生在Safari?

我已經使用了上面的代碼,並且它只在Safari,Chrome和FireFox中按預期進行重新驗證。

看來,如果你除非你檢查了「記住我的用戶名/密碼」,Safari會發送每個後續請求,而不需要在頭部的授權(一個偉大的工具看標題等是Charles)。如果您確實檢查了它,Apple會正確地在標題中發送Auth,即使您退出Safari,它也會繼續記住在重新啓動時發送Auth。

所以這是蘋果傻不是你:)

+0

要解決這個問題,你可以使用基於cookie認證http://ididitmyway.heroku.com/past/2011/2/22/really_simple_authentication_in_sinatra/獎金是餅乾可以設置爲超時,在基本身份驗證僅在退出時清除(或僅通過Safari的Keychain Access情況)。 – kreek

+0

啊該死的,這是一個恥辱,感謝您的照顧,雖然,是啊它在Chrome和FF的作品。它的唯一內部,所以我會花一點時間看着他們留下它的想法 – redroot