2

sinatra とカスタム omniauth 戦略を使用してhttps://launchpad.net経由でリクエスト トークンを取得しようとしています。

require 'omniauth-oauth'                                                                                                                
require 'oauth/signature/plaintext'                                                                                                     
require 'multi_json'                                                                                                                    
require 'pp'                                                                                                                            

module OmniAuth                                                                                                                         
  module Strategies                                                                                                                     
    class Launchpad < OmniAuth::Strategies::OAuth                                                                                       
      option :name, "launchpad"                                                                                                         
      option :client_options, {                                                                                                         
        :authorize_url => 'https://launchpad.net/+authorize-token',                                                                     
        :request_token_url => 'https://launchpad.net/+request-token',                                                                   
        :site => 'https://api.launchpad.net',                                                                                           
        :consumer_key => 'rubystfu',                                                                                                    
        :signature_method => 'PLAINTEXT',                                                                                               
        :signature => '%26'                                                                                                             
      }                                                                                                                                 

      def request_phase                                                                                                                 
        super                                                                                                                           
      end                                                                                                                               

      uid {                                                                                                                             
        raw_info[:name]                                                                                                                 
      }                                                                                                                                 

      info do                                                                                                                           
        {                                                                                                                               
          :description => raw_info['description'],                                                                                      
          :name => raw_info['name'],                                                                                                    
          :display_name => raw_info['display_name'],                                                                                    
          :karma => raw_info['karma'],                                                                                                  
          :self_link => raw_info['self_link'],                                                                                          
          :email => raw_info['email']                                                                                                   
        }                                                                                                                               
      end                                                                                                                               

      extra do                                                                                                                          
        {                                                                                                                               
          :raw_info => raw_info                                                                                                         
        }                                                                                                                               
      end                                                                                                                               

      def raw_info                                                                                                                      
        raw_info                                                                                                                        
      end                                                                                                                               
    end                                                                                                                                 
  end                                                                                                                                   
end  

この方法でsinatraアプリをセットアップしました

#!/usr/bin/env ruby                                                                                                                     
require 'rubygems'                                                                                                                      
require 'bundler'                                                                                                                       

Bundler.setup :default, :development, :chonk                                                                                            

require 'sinatra'                                                                                                                       
require 'haml'                                                                                                                          
require 'omniauth-launchpad'                                                                                                            

use Rack::Session::Cookie                                                                                                               

use OmniAuth::Builder do                                                                                                                
  provider :launchpad                                                                                                                   
end                                                                                                                                     

set :sessions, true                                                                                                                     
set :logging, true                                                                                                                      

class ChonkApp < Sinatra::Application                                                                                                   
  helpers do                                                                                                                            
    def check_auth                                                                                                                      
      redirect "/" if not session[:user]                                                                                                
    end                                                                                                                                 
  end                                                                                                                                   

  get '/' do                                                                                                                            
    redirect "/app" if session[:user]                                                                                                   
    haml :index                                                                                                                         
  end                                                                                                                                   

  get '/app' do                                                                                                                         
    check_auth                                                                                                                          
    "#{session[:user]} | <a href='/logout'>logout</a>"                                                                                  
  end                                                                                                                                   

  get '/logout' do                                                                                                                      
    session.clear                                                                                                                       
    redirect '/'                                                                                                                        
  end                                                                                                                                   

  %w(get post).each do |method|                                                                                                         
    send(method, "/auth/:name/callback") do                                                                                             
      auth = request.env['omniauth.auth']                                                                                               
      a = auth.info                                                                                                                     
      session[:user] = "%s %s (%s)" % [a.email, a.name, a.nickname]                                                                     
      redirect "/app"                                                                                                                   
    end                                                                                                                                 
  end                                                                                                                                   

  get '/auth/failure' do                                                                                                                
    "I dunno wtfook happened."                                                                                                          
  end                                                                                                                                   
end 

ドキュメントには、これが機能するには 3 つの投稿されたパラメーターが必要であると記載されています。

リクエスト トークンを取得するには、POST リクエストを https://launchpad.net/+request-tokenに送信します。(注: api.launchpad.net ではありません!) これは、Web ブラウザーがフォームを送信するときに行うのと同じ種類の POST です。次の値をフォーム URL エンコード形式で送信する必要があります。

oauth_consumer_key: コンシューマ キー oauth_signature_method: 文字列 "PLAINTEXT" oauth_signature: 文字列 "&"。

したがって、HTTP 要求は次のようになります。

POST /+request-token HTTP/1.1
Host: launchpad.net
Content-type: application/x-www-form-urlencoded

oauth_consumer_key=just+testing&oauth_signature_method=PLAINTEXT&oauth_signature=%26 The response should look something like this:


200 OK

oauth_token=9kDgVhXlcVn52HGgCWxq&oauth_token_secret=jMth55Zn3pbkPGNht450XHNcHVGTJm9Cqf5ww5HlfxfhEEPKFflMqCXHNVWnj2sWgdPjqDJNRDFlt92f

間違っている可能性があると思われる唯一のことは、signature_method および/または署名です。私はルビーにかなり慣れていないので、アドバイスをいただければ幸いです。戻ってきたエラーは「401無許可」であり、リクエスト中にhttps://launchpad.netに正確に渡されたものにデバッグ出力を出力する方法がわかりません。

署名リクエストのドキュメントへのリンクは次のとおりです: https://help.launchpad.net/API/SigningRequests

ありがとう

ps omniauth-launchpad のソースはhttps://github.com/battlemidget/omniauth-launchpadで、実際のアプリケーションはhttps://github.com/battlemidget/chonkです。

4

0 に答える 0