authlogic + OpenID でちょっとはまる

GitHub - binarylogic/authlogic_openid: Extension of the Authlogic library to add OpenID support. の通りにやるとエラーになります。困ります。すぐに修正されそうですが、とりあえず対処。

Rack::OpenID 関連

http://github.com/binarylogic/authlogic_openid/issues#issue/15
とのことなので、http://github.com/mreinsch/authlogic_openid を使います。gem の作り方がよくわからなかったのでソースを直接置いてごまかします。

$ sudo gem uninstall authlogic-oid
$ git clone http://github.com/mreinsch/authlogic_openid.git vendor/plugins/mreinsch-authlogic_openid

Rack::OpenID を入れます。

# config/environment.rb
Rails::Initializer.run do |config|
  # ...
  config.gem "rack-openid", :lib => 'rack/openid'
end
$ sudo rake gems:install

relative_url_root

Rails 2.2 以降では ActionController::Request#relative_url_root がないよっていうエラーが出ます。適当な場所で作ってください。

class ActionController::Request
  def relative_url_root
    return ActionController::Base.relative_url_root || ''
  end
end

require_fields など

あとそのままだと OpenID::SReg の require_fields, optional_fields に空の Array を渡してエラーになるので、使わない場合は acts_as_authentic の config で false に設定しましょう。[] や nil だと駄目です。

class User < ActiveRecord::Base
  acts_as_authentic do |c|
    c.openid_mreinsch = false
    c.openid_optional_fields = false
  end
end

モデルに name, first_name などのカラムがあると困る

mreinsch さんが OpenID の情報から name などを自動で設定するような修正を入れているのですが、require_fields などを適切に設定していない場合エラーが出ます。問題であれば AuthlogicOpenid::ActsAsAuthentic#map_openid_registration の中身をコメントアウトするなどしましょう。

authlogic_openid のこなれてなさ具合から OpenID の普及度を感じますね。以上です。

追記

んー、使ってみるといろいろとよくない。binarylogic-authlogic_openid を使って http://github.com/mreinsch/authlogic_openid/commit/9b802c347f5addebcbce945af3b5f80b3ee7b214 これだけマージした方が素直に動いて良いと思う。新しく作るなら authlogic でない認証を使うとか。

追記2

OpenID 2.0 では claimed_id の fragment (#以降の部分) を捨ててはいけないらしいのだけど、上ので使っている OpenID.normalize_url は fragment を捨てているので、fragment 付きの claimed_id が帰ってくる Yahoo の認証で問題が出たりする。ちゃんとやると修正箇所が多そうなので、とりあえずは fragment 捨て統一で事をうやむやにする方針で。