ruby-openid付属のサンプル実行
ruby-openid (OpenIDのRuby用ライブラリ) のインストールと付属のサンプルを試してみたので、動かすまでの作業をメモっておきます。
動作環境
- Mac OS 10.6 (Snow Leopard)
- Ruby 1.8.7-p248
- RubyGems 1.3.5
- Ruby on Rails 2.3.5
サンプルコード
ruby-openidがインストールされたディレクトリの下に、example ディレクトリがあり、この下にRails用のOpenID Provider/Relying Partyのサンプル (Webアプリケーションフレームワーク) 用のOpenID Provier/Relying Partyのサンプルがあります。
/usr/local/lib/ruby/gems/1.8/gems/ruby-openid-2.1.7/examples> ls README discover active_record_openid_store/ rails_openid/ /usr/local/lib/ruby/gems/1.8/gems/ruby-openid-2.1.7/examples> ls rails_openid README app/ doc/ script/ Rakefile config/ public/ test/
以下では、作業用に、サンプルコードをホームディレクトリ以下にコピーします。
/usr/local/lib/ruby/gems/1.8/gems/ruby-openid-2.1.7/examples> cp -R rails_openid ~/work/openid-server
なお、OpenID 2.0におけるProvider, Relying Partyは、歴史的な経緯からか、ruby-openidではそれぞれserver, consumerという名称になっています。作業ディレクトリ名はこちらに合わせています。
サンプルコード実行 (プロバイダ編)
まずは、作業用ディレクトリにコピーしたサンプルコードを、Railsのサーバとして起動してみます。このサンプルは多少古いRailsをベースに作られたようで、サーバを起動しようとすると以下のようなメッセージが出て失敗します。
~/work/openid-server> ruby script/server
=> Booting WEBrick
=> Rails 2.3.5 application starting on http://0.0.0.0:3000
Your config/boot.rb is outdated: Run "rake rails:update".
とりあえず言われた通りに rake rails:update を実行。
~/work/openid-server> rake rails:update (in /Users/masahiro/work/openid-server) install -c -m 0755 /usr/local/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/tasks/../../bin/dbconsole script/dbconsole /Users/masahiro/work/openid-server/app/controllers/application.rb has been renamed to /Users/masahiro/work/openid-server/app/controllers/application_controller.rb, update your SCM as necessary
改めてサーバ起動。
~/work/openid-server> ruby script/server => Booting WEBrick => Rails 2.3.5 application starting on http://0.0.0.0:3000 ******************************************************************* * config.breakpoint_server has been deprecated and has no effect. * ******************************************************************* => Call with -d to detach => Ctrl-C to shutdown server [2010-02-10 00:57:36] INFO WEBrick 1.3.1 [2010-02-10 00:57:36] INFO ruby 1.8.7 (2009-12-24) [i686-darwin10.2.0] [2010-02-10 00:57:36] INFO WEBrick::HTTPServer#start: pid=467 port=3000
この状態でブラウザから http://localhost:3000/ を見に行くと、以下のエラーが出ます。
A key is required to write a cookie containing the session data. Use config.action_controller.session = { :key => "_myapp_session", :secret => "some secret phrase" } in config/environment.rb
セッション情報を格納するクッキーの設定がないと怒られたので、config/environment.rb に設定を追加します。
Rails::Initializer.run do |config| # ... config.action_controller.session = { :key => "_openid_session", :secret => "ec129ef31d6bff0aa084023e8933e283a80c3a597ed05f1dee3541525330" } end
ここで、secretの値は、以下の方法で生成しています。
~/work/openid-server> ruby -rsecurerandom -e 'puts SecureRandom.hex(30)' ec129ef31d6bff0aa084023e8933e283a80c3a597ed05f1dee3541525330
再びサーバを立ち上げ、ブラウザから http://localhost:3000/ を見に行きます。
~/work/openid-server> ruby script/server
この状態で適当な文字列をIDとして入力すると、新規のOpenID URLが発行されます。
サンプルコード実行 (Relying Party編)
先ほど出てきた画面の指示では、プロバイダと別ポートで新規のサーバを起動すると。
念のため、プロバイダ用ディレクトリをコピーして、親近のディレクトリを作ります。
~/work> cp -R openid-server openid-consumer
ポート番号を3001に変えて、サーバを起動します。
~/work/openid-consumer> ruby script/server --port=3001 => Booting WEBrick => Rails 2.3.5 application starting on http://0.0.0.0:3001 ******************************************************************* * config.breakpoint_server has been deprecated and has no effect. * ******************************************************************* => Call with -d to detach => Ctrl-C to shutdown server [2010-02-10 01:24:35] INFO WEBrick 1.3.1 [2010-02-10 01:24:35] INFO ruby 1.8.7 (2009-12-24) [i686-darwin10.2.0] [2010-02-10 01:24:35] INFO WEBrick::HTTPServer#start: pid=596 port=3001
ブラウザから、http://localhost:3001/consumer に接続すると、以下の画面が出ます。
ここで、先ほどプロバイダから出力されたOpenID URLを入力して「Verify」を押すと、プロバイダ側にリダイレクトされ、以下の確認画面が出ます。
ここで「Yes」を押すと、入力したURLが検証されたと判断されて、再び元のサービス (=RP) にリダイレクトされます。この時点で、Open IDプロバイダによる認証処理は完成したことになります。