2010-11-06 17 views
0

我正在嘗試使用OmniAuth處理小型Sinatra應用程序的OAuth流程。我可以得到37signals Oauth完美的工作,但是我正在嘗試爲Freshbooks Oauth創建一個策略。我如何持久地覆蓋由Rack :: Builder初始化的屬性?

不幸的是,Freshbooks需要OAuth請求才能轉到用戶特定的子域。我將獲取子域作爲輸入,然後我需要持續地爲所有請求使用客戶特定的站點URL。

這是我到現在爲止所嘗試的。問題是新的站點值不會持續超過第一個請求。

有一個簡單的方法來實現這一點,但我很難過。

#Here's the setup - 
    def initialize(app, consumer_key, consumer_secret, subdomain='api') 
    super(app, :freshbooks, consumer_key, consumer_secret, 
       :site    => "https://"+subdomain+".freshbooks.com", 
       :signature_method => 'PLAINTEXT', 
       :request_token_path => "/oauth/oauth_request.php", 
       :access_token_path => "/oauth/oauth_access.php", 
       :authorize_path  => "/oauth/oauth_authorize.php" 
     ) 
    end 


    def request_phase 
    #Here's the overwrite - 
    consumer.options[:site] = "https://"+request.env["rack.request.form_hash"]["subdomain"]+".freshbooks.com" 
    request_token = consumer.get_request_token(:oauth_callback => callback_url) 
    (session[:oauth]||={})[name.to_sym] = {:callback_confirmed => request_token.callback_confirmed?, 
              :request_token => request_token.token, 
              :request_secret => request_token.secret} 
    r = Rack::Response.new 
    r.redirect request_token.authorize_url 
    r.finish 
    end 
+0

忘了提及我必須將:attr_reader中的消費者更改爲omniauth/oauth.rb中的attr_accessor以獲得上述工作。 – 2010-11-06 10:28:08

回答

0

好吧,這裏的我就是這樣做的人誰通過谷歌遇到這樣的總結。

我沒有按照我問的方式解決問題,而是將子域推入會話,然後在需要使用站點值時覆蓋它。

下面的代碼:

#Monkeypatching to inject user subdomain 
    def request_phase 
    #Subdomain is expected to be submitted as <input name="subdomain"> 
    session[:subdomain] = request.env["rack.request.form_hash"]["subdomain"] 
    consumer.options[:site] = "https://"+session[:subdomain]+".freshbooks.com" 
    super 
    end 

    #Monkeypatching to inject subdomain again 
    def callback_phase 
    consumer.options[:site] = "https://"+session[:subdomain]+".freshbooks.com" 
    super 
    end 

請注意,您還必須設置爲東西時,它的初始化的網站,否則你不會使用SSL發出請求獲得由於OAuth錯誤。

如果你想看到我使用的實際代碼,它在:https://github.com/joeharris76/omniauth一旦我對這個解決方案進行了更多的戰鬥測試,我會把叉子推到主項目上。