代码之家  ›  专栏  ›  技术社区  ›  Joe Harris

如何持续覆盖由rack::builder初始化的属性?

  •  0
  • Joe Harris  · 技术社区  · 14 年前

    我正在尝试使用Omniauth来处理一个小型的ishSinatra应用程序的OAuth流。我可以让37个信号OAuth完美工作,但是我也在尝试为Freshbooks OAuth创建一个策略。

    不幸的是,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
    
    1 回复  |  直到 14 年前
        1
  •  0
  •   Joe Harris    14 年前

      #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
    

    https://github.com/joeharris76/omniauth