railsã§openid試ãã¦ã¿ãããopenid_login_generatorã§ãã£ã¦ã¿ãã
gemå ¥ãã
$ gem install ruby-openid $ gem install openid_login_generator $ gem install ruby-yadis $ gem list --local (ç¥) openid_login_generator (0.1) rails (2.0.2) rake (0.8.1) ruby-openid (2.0.3) ruby-yadis (0.3.4)
æ°ããRailsããã¸ã§ã¯ãä½ã
$ rails openidtest -d mysql (ç¥) $ cd openidtest $ rake db:create
ã§ãããã¸ã§ã¯ãã§ãã¦developmentã®DBãä½ããã
generate
$ ./script/generate openid_login account create lib/openid_login_system.rb create app/controllers/account_controller.rb create test/functional/account_controller_test.rb create app/helpers/account_helper.rb create app/models/user.rb create test/unit/user_test.rb create test/fixtures/users.yml create app/views/layouts/scaffold.rhtml No such file or directory - /opt/rubygems/gems/rails-2.0.2/lib/rails_generator/generators/components/scaffold/templates/layout.rhtml
æãããã
ã¨ããããæ¶ã
$ ./script/destroy openid_login account
generatorãã¢ã
ãã£ãlayout.rhtmlããã¼ãã¨æãããã®ã§ã/opt/rubygems/gems/rails-2.0.2/lib/rails_generator/generators/components/scaffold/templates/ãã¿ãã
$ ls /opt/rubygems/gems/rails-2.0.2/lib/rails_generator/generators/components/scaffold/templates/ controller.rb helper.rb style.css view_index.html.erb view_show.html.erb functional_test.rb layout.html.erb view_edit.html.erb view_new.html.erb
rails2.0ãªã®ã§html.erbãªã®ã§
vi /opt/rubygems/gems/openid_login_generator-0.1/openid_login_generator.rb
ã®ãã®ã¸ããç´ãã
# Layout and stylesheet. # m.template "scaffold:layout.rhtml", "app/views/layouts/scaffold.rhtml" m.template "scaffold:layout.html.erb", "app/views/layouts/scaffold.html.erb" m.template "scaffold:style.css", "public/stylesheets/scaffold.css" # Views. m.directory File.join("app/views", class_path, file_name) login_views.each do |action| # m.template "view_#{action}.rhtml", # File.join("app/views", class_path, file_name, "#{action}.rhtml") m.template "view_#{action}.html.erb", File.join("app/views", class_path, file_name, "#{action}.html.erb")
ã§ãå度ãããã£ããä»åº¦ã¯ã
No such file or directory - /opt/rubygems/gems/openid_login_generator-0.1/templates/view_welcome.html.erb
ã¨ãããããããããã
Viewã®æ¹ã¯rhtmlã®ã¾ã¾ã§ãããã
$ ls /opt/rubygems/gems/openid_login_generator-0.1/templates/ README controller_test.rb user.rb view_login.rhtml controller.rb helper.rb user_test.rb view_logout.rhtml controller.rb~ openid_login_system.rb users.yml view_welcome.rhtml
ãªã®ã§ããã£ãã®generatorã®ãã¡ã¤ã«ã¯Viewsã®æ¹ã¯å
ã«æ»ãã
ã§ãgenerateã§ãã
$ ./script/generate openid_login account create lib/openid_login_system.rb create app/controllers/account_controller.rb create test/functional/account_controller_test.rb create app/helpers/account_helper.rb create app/models/user.rb create test/unit/user_test.rb create test/fixtures/users.yml create app/views/layouts/scaffold.html.erb create public/stylesheets/scaffold.css exists app/views/account create app/views/account/welcome.rhtml create app/views/account/login.rhtml create app/views/account/logout.rhtml create README_LOGIN
ãã¼ãã«ã¨ãä½ã
app/models/user.rb
ãã§ããã®ã§ãusersãä½ã
modelã¨ãã¯ããããããn
$ script/generate model user openid_url:string exists app/models/ exists test/unit/ exists test/fixtures/ overwrite app/models/user.rb? (enter "h" for help) [Ynaqdh] n skip app/models/user.rb overwrite test/unit/user_test.rb? (enter "h" for help) [Ynaqdh] n skip test/unit/user_test.rb overwrite test/fixtures/users.yml? (enter "h" for help) [Ynaqdh] n skip test/fixtures/users.yml create db/migrate create db/migrate/001_create_users.rb
migrateãã¡ã¤ã«ã§ãããããmigration
$ rake db:migrate (in /home/coek/dev/rails/form_storm) == 1 CreateUsers: migrating =================================================== -- create_table(:users) -> 0.0921s == 1 CreateUsers: migrated (0.0953s) ==========================================
ã§æºåå®äº
ã¢ã¯ã»ã¹ãã¦ã¿ã
http://localhost:3000/account/loginã«ã¢ã¯ã»ã¹ãã¦ã¿ã
NoMethodError in AccountController#login undefined method `require_gem' for main:Object RAILS_ROOT: /home/coek/dev/rails/form_storm Application Trace | Framework Trace | Full Trace app/controllers/account_controller.rb:7
require_gemããã¡
ããã¨ããrails2ã§ãã¡ã«ãªã£ãã¨ããç´ãã
controllerã¯
require_gemã®ä¿®æ£ã¨
@params,@request,@sessionã@å¤ã
# require_gem "ruby-openid", ">= 1.0.2" gem "ruby-openid", ">= 1.0.2"
modelã¯find_firstãfind_by_openid_url
- find_first(["openid_url = ?", openid_url]) + find_by_openid_url(openid_url)
viewã¯<%= start_form_tag :action=> "login" %>ã
<% form_tag :action=> "login" do %> #ä¸ç¥ <% end %>
ã«ä¿®æ£
å度http://localhost:3000/account/loginã«ã¢ã¯ã»ã¹
loginç»é¢ã表示ãããã
ãããã
ã¯ã¦ãªã®ã¢ã«ã¦ã³ãã§è©¦ãã¦ã¿ããã©
ãªãããã¾ãããããã
OpenID2.0ã©ã¤ãã©ãªã«åããã¦ãaccount_controller.rbãä¿®æ£
gemã®requireã®VERSIONç´ã
# load the openid library begin require "rubygems" # require_gem "ruby-openid", ">= 1.0.2" gem "ruby-openid", ">= 2.0.3" rescue LoadError require "openid" end
âã ã¨ãã¡ã¿ãããã
exampleã«åããã¦
require 'pathname' require "openid" require 'openid/extensions/sreg' require 'openid/extensions/pape' require 'openid/store/filesystem'
loginã¯å¤§å¹
ã«å¤æ´
http://www.mashupedia.jp/docs/view/79
def loginã®ä¸èº«ã¯ã»ã¼ç·å ¥ãæ¿ããã§ã以ä¸ã®ããã«ãªãã¾ãã
def login openid_url = params[:openid_url] if request.post? begin request = consumer.begin(openid_url) return_to = url_for(:action=>'complete') trust_root = url_for(:controller=>"") url = request.redirect_url(trust_root, return_to) redirect_to(url) rescue OpenID::OpenIDError flash[:notice] = "Authorization failed." end end end
request.statusã¨ãerrorã«ãªã£ãããã ãã¶APIã®ä¸èº«ãå¤ãã£ã¦ãã®ããªãã
completeã®
consumer.completeã®å¼æ°ãï¼ã¤ã«ãªã£ã¦ã
#response = consumer.complete(params) current_url = url_for(:action => 'complete', :only_path => false) parameters = params.reject{|k,v|request.path_parameters[k]} response = consumer.complete(parameters, current_url)
OpenID::SUCCESSã¨ããOpenID::Consumer::SUCCESSã«ãã
OpenID::Consumer::SUCCESS
consumerã®storeãç´ã
def consumer # create the OpenID store for storing associations and nonces, # putting it in your app's db directory store_dir = Pathname.new(RAILS_ROOT).join('db').join('openid-store') #store = OpenID::FilesystemStore.new(store_dir) store = OpenID::Store::Filesystem.new(store_dir) return OpenID::Consumer.new(session, store) end
ããã¯AR使ãããã«ãã§ããã¿ããã
ã§ãä¸å¿åããã
/opt/rubygems/gems/ruby-openid-2.0.3/example
ã¯ãããã¡ãã£ã¨ãéããã¨ããã£ã¦ãã£ã½ãã®ã§ãã£ã¡ãè¦ã¦ã¿ãã
åè
ã£ã¼ãã¾ãã¾åããã ãã¿ãããªã
Railsã§OpenIDã使ã(ã³ã³ã·ã¥ã¼ãç·¨)
http://www.machu.jp/diary/20071124.html#p01
Rails 㧠OpenID ã使ã (OpenID::Consumer#begin ãèªããç·¨)
http://www.machu.jp/diary/20071125.html#p01
ããã¿ããªOpenIDã«ã
http://d.hatena.ne.jp/yune_kotomi/20071111/1194796903
æãèªã¿ OpenID Simple Registration Extension 1.0
http://d.hatena.ne.jp/ZIGOROu/20070323/1174631045
OpenIDã©ã¤ãã©ãªã®å°å
¥
http://www.mashupedia.jp/docs/view/79