Warp Drive是一种将整个 Rails 应用程序打包成 Gem 以供其他 Rails 应用程序使用的好方法。我已经让 Warp Drive 为我正在创建的博客引擎工作。只有一个问题 - Authlogic OpenID 身份验证失败。
我创建了一个简单的 OpenID 示例应用程序。我可以毫无问题地编译成 gem:
$ warpify
$ rake warp_drive:compile
然后我在我的系统上安装了编译好的 gem。创建一个空白 Rails 项目,我运行:
$ install_warp_drive rails-openid
你可以在这里得到这个项目。
在我的空白 Rails 项目中,我需要通过environment.rb配置 gems (我可能做错了):
config.gem "authlogic"
config.gem "authlogic-oid", :lib => "authlogic_openid"
config.gem "ruby-openid", :lib => "openid"
为了让空白的 Rails 应用程序正常工作,我运行了 rake db:migrate,然后通过控制台添加了一个用户,该用户的 :openid_identifier 字段设置为我控制的字段。到现在为止还挺好。但是尝试创建新会话失败并出现以下错误:
Processing UserSessionsController#create (for 127.0.0.1 at 2009-12-31 11:35:59) [POST]
Parameters: {"commit"=>"Login", "user_session"=>{"openid_identifier"=>"richdev.myopenid.com"}, "authenticity_token"=>"BcsIKNpumqZrTV/bdSLQ6szBvq6kpaAIxJRmYgxySLU="}
OpenIdAuthentication::Association Load (0.3ms) SELECT * FROM "open_id_authentication_associations" WHERE ("open_id_authentication_associations"."server_url" = 'http://www.myopenid.com/server')
Generated checkid_setup request to http://www.myopenid.com/server with assocication {HMAC-SHA1}{4b3cf228}{mWlzhg==}
Redirected to http://www.myopenid.com/server?openid.assoc_handle=%7BHMAC-SHA1%7D%7B4b3cf228%7D%7BmWlzhg%3D%3D%7D&openid.ax.mode=fetch_request&openid.identity=http%3A%2F%2Frichdev.myopenid.com%2F&openid.mode=checkid_setup&openid.return_to=http%3A%2F%2Flocalhost%3A3001%2Fuser_sessions%2Fcreate%3Ffor_session%3D1%26_method%3Dpost%26open_id_complete%3D1%26openid1_claimed_id%3Dhttp%253A%252F%252Frichdev.myopenid.com%252F%26rp_nonce%3D2009-12-31T19%253A35%253A59ZUEd2eN&openid.trust_root=http%3A%2F%2Flocalhost%3A3001%2F
Completed in 15ms (DB: 0) | 302 Found [http://localhost/user_sessions]
Processing ApplicationController#index (for 127.0.0.1 at 2009-12-31 11:36:00) [POST]
Parameters: {"openid.mode"=>"id_res", "openid.return_to"=>"http://localhost:3001/user_sessions/create?for_session=1&_method=post&open_id_complete=1&openid1_claimed_id=http%3A%2F%2Frichdev.myopenid.com%2F&rp_nonce=2009-12-31T19%3A35%3A59ZUEd2eN", "openid.sig"=>"l+tfFAmeKsskHKlOYRoZF7yHM7Q=", "rp_nonce"=>"2009-12-31T19:35:59ZUEd2eN", "openid.op_endpoint"=>"http://www.myopenid.com/server", "for_session"=>"1", "openid.response_nonce"=>"2009-12-31T19:36:00ZBhX5fE", "openid1_claimed_id"=>"http://richdev.myopenid.com/", "openid.identity"=>"http://richdev.myopenid.com/", "open_id_complete"=>"1", "openid.assoc_handle"=>"{HMAC-SHA1}{4b3cf228}{mWlzhg==}", "openid.signed"=>"assoc_handle,identity,mode,op_endpoint,response_nonce,return_to,signed"}
ActionController::MethodNotAllowed (Only get, put, and delete requests are allowed.):
Rendered rescues/_trace (96.3ms)
Rendered rescues/_request_and_response (0.5ms)
Rendering rescues/layout (method_not_allowed)
问题似乎发生在从 openid 提供程序重定向回来的过程中,此时调用 ApplicationController#index,而不是 UserSessionsController#create。我不确定这是 OpenID 问题还是 Warp Drive 问题。
如何将 Authlogic/OpenID 应用程序捆绑为 Warp Drive Gem 并让身份验证成功运行?
更新:为 user_session 添加显式资源定义可解决问题。在 routes.rb 中:
map.resources :user_sessions
不知道为什么,这似乎不需要任何其他控制器。