SlideShare una empresa de Scribd logo
1 de 58
Descargar para leer sin conexión
Ruby on Railsで作る
OpenID対応サイト
Identity Conference #3
MATSUOKA Kohei <kohei@machu.jp>



                                  1
今日のテーマ

RailsでOpenID対応サイト (RP) を作るには?

Railsをつかって駆け足でRPを作成します

セキュリティやSREGでの属性取得も少々


そろそろマイミク認証について一言いっておくか




                                2
背景

2007年以降、OpenID発行サイトが充実

 はてな、livedoor、Yahoo! Japan、そしてmixi

でも、対応サイトが少ない

Railsを使ってRPサイトを作ってみよう



                                     3
宣伝

gihyo.jp

いますぐ使えるOpenID

  http://gihyo.jp/dev/
  feature/01/openid




                         4
作成するアプリ
簡単なチャット
 OpenIDでログイン
 mixiのニックネーム
 を取得
 マイミク認証
Mac OS X +
NetBeans 6.1で開発

                  5
NetBeans

統合開発環境

 JRuby, CRuby, Rails

 サーバの起動停止

 コード補完

なかなか便利
ライブラリのインストール
OpenIDを利用するために必要な
ライブラリとプラグインを導入します



                    7
アプリの構成

Ruby 1.8.6-p111, Ruby on Rails 2.1

 ruby-openidライブラリ

 Restful Authenticationプラグイン

 OpenID Authenticationプラグイン



                                     8
ruby-openid

sudo gem install ruby-openid



RubyでOpenIDを扱うための基本的なライブラリ

 http://openidenabled.com/ruby-openid/

 PHP, Pythonにも同様のライブラリあり


                                         9
Railsプラグインの導入
プロジェクトを右クリックして、
「Railsプラグイン」を選択   10
RESTful Authentication

認証機能の枠組み          処理    メソッド      パス

RESTfulな設計      ユーザ登録    POST    /users

OpenID Authプラ
                ログイン     POST   /sessions
グインと組み合わせ
て利用
                ログアウト DELETE /sessions


                                            11
OpenID Authentication
 ruby_openidライブラリのRails向けラッパー

   OpenID Providerとのやりとりを担当

authenticate_with_open_id do ¦result, identity_url¦
  if result.successful?
    # 認証成功
  else
    # 認証失敗
 end
                                                      12
5分でできる(ry
サンプルの認証機能以外を作成します。
よくあるRailsチュートリアルと同じです。



                         13
チャットのひな形を作成



script/generate scaffold message title:string

rake db:migrate

script/server -> Railsサーバ (mongrel) が起動


                                               14
チャットのひな形を作成



画面をチャット風に修正




              15
認証機能の組み込み
RESTful Authenticationプラグインを使って
アプリに認証機能を組み込みます



                                  16
RESTful Authentication

認証のためのひながたを作成できる



./script/generate authenticated user sessions




                                                17
自動生成されるファイル
  種別           名称             役割

 モデル           User         ユーザ情報を保存

          UsersController    ユーザ登録
コントローラ
         SessionsController ログイン、ログアウト

            users/new       ユーザ登録画面
 ビュー
           sessions/new      ログイン画面

                                         18
ユーザ登録

画面遷移
             /users/new




チャット
 /messages

                    ログイン
                    /sessions/new




                                    19
認証と認可
class ApplicationController < ActionController::Base
  include AuthenticatedSystem


class MessagesController < ApplicationController
  before_filter :login_required,
                :except => [:index, :show]


  チャットの書き込みにはログインが必要

     AuthenticatedSystemをインクルード

     フィルタでlogin_requiredメソッドを呼び出す

                                                       20
AuthenticatedSystem
コントローラに        login_required
includeして使用     authorized?
                    認可
認証と認可に必要なメ      logged_in?
ソッドを定義         ログインの有無を判定

メソッドをオーバーラ
                      NG
イドすることでカスタ
               access_denied
マイズ可能
              アクセス拒否時の動作を定義


                                21
OpenID認証への対応
OpenID Authenticationプラグインを使って
パスワード認証からOpenID認証へ変更します



                                 22
OpenID認証への対応
RESTful Authenticationプラグインで作成した
ひながたを元にOpenID認証に対応します

変更箇所

 データベース

 ログイン処理(コントローラ)

 ログイン画面、ユーザ登録画面(ビュー)
データベースの更新

OpenIDライブラリが使用するテーブルを自動生成

rake open_id_authentication:db:create

 open_id_authentication_associations

 open_id_authentication_nonces



                                        24
データベースの更新

usersテーブルにidentity_url属性を追加

 OpenIDのVerified Identifierを保存するため



rake db:migrateを実行してDBスキーマを更新



                                   25
ログイン
画面遷移

 チャット


            ログイン
                    ユーザ登録
             処理




※ ログイン→ユーザ登録となる
                            26
ログイン処理の修正
パスワード認証をOpenID認証へ

Sessionsコントローラのcreateメソッドを修正

 OpenID AuthenticationのREADMEを参考

認証成功時に処理を分岐

 ユーザ登録済 → セッションCookie発行

 ユーザ未登録 → ユーザ登録画面へ

                                   27
ログイン処理の修正
authenticate_with_open_id do ¦result, identity_url¦
  if result.successful?
    if @current_user = User.find_by_identity_url(identity_url)
                                                                認証成功
      successful_login
    else
                                                                → Cookie発行
      # identity_url (Verified Identifier) をセッションに保存
      session[:identity_url] = identity_url                     認証成功(未登録)
      # ユーザ登録画面へリダイレクトする
      redirect_to new_user_path                                 → ユーザ登録へ
    end
  else
    # 認証失敗                                                      認証失敗
    flash[:notice] = result.message
    render :action => 'new'                                     → ログイン画面へ
  end
 end




※ OPへのリダイレクト処理は記述不要                                                          28
ログイン画面の修正


OpenIDのアカウント
名を入力

お好みで「○○でログ
イン」ボタンを追加
ユーザ登録画面の修正

ニックネームのみ入力し
てもらう

identity_urlはセッショ
ンCookieから取得する

セッションCookieを持
たないユーザを拒否


                    30
セキュリティ
RPの基本的なセキュリティ対策である
SSL対応(サーバ証明書の検証)について



                       31
http://d.hatena.ne.jp/ZIGOROu/20080805/1217933206
                                                    32
SSL対応の発行サイト
mixi.jp でログイン → ログに警告が出力

WARNING: making https request to https://mixi.jp/xrds_server.pl
 without verifying server certificate; no CA path was specified.



サーバ証明書を発行したCAを信頼していないため

でも警告だけで使えてしまう><

 DNS Cache Poisoningされるとアウト


                                                                  33
証明書を検証しないSSLは
ほとんど無意味


cf. オレオレ証明書
SSLを 正しく 利用する
信頼していないCAへの接続は拒否する

OpenID.fetcher.ca_fileにCAのリストを設定

 config/environment.rb あたりに記述

 cURLのCAリストを使用した
 OpenID.fetcher.ca_file = '/usr/share/curl/curl-ca-bundle.crt'




                                                                35
SSLを 正しく 利用する
OpenID.fetcher.ca_fileを設定

mixi.jpにログイン

 警告ではなくエラーで停止

信頼していない証明書は受け入
れない

 [OPENID] Failed to fetch Yadis URL https://mixi.jp/xrds_server.pl :
    Error connecting to SSL URL https://mixi.jp/xrds_server.pl:
                       certificate verify failed
                                                                       36
mixiの場合
id.mixi.jpとmixi.jpでルート証明書が異なる

 id.mixi.jp … GTE CyberTrust Global Root

 mixi.jp … AddTrust External CA Root

Mac OS XのcURLはAddTrustが含まれていない

 Netscape Communicator 4.7の証明書リスト

他のOSでは入っている証明書リストが異なる
証明書リストに追加(1)
「信頼できる経路」を使っ
てルート証明書を取得

 今回はfirefoxを使う

mixi.jpに接続

CA (AddTrust) の証明書
を選択し、「書き出す」


                     38
証明書リストに追加(2)
BEGIN CERTIFICATEと   -----BEGIN CERTIFICATE-----
                     MIIENjCCAx6gAwIBAgIBATANBgkqhk
書かれたファイル
                     <略>

 これがCAの証明書           1Z5jJh5VkpTYghdae9C8x49OhgQ=
                     -----END CERTIFICATE-----
CAリストの末尾にこのテ
キストを貼り付ける



警告もエラーも出ずにログインできるようになる!

                                                      39
参考
ruby で mixi OpenID を呼ぶには
 http://d.hatena.ne.jp/n_shuyo/20080911/mixi_openid

php-openid で mixi のコミュニティ認証を使う
 http://www.machu.jp/diary/20080918.html#p01




面倒でもちゃんと設定しましょう


                                                      40
SREGで属性情報を取得
SREG拡張でmixiユーザのニックネームを
取得します



                         41
mixiのニックネームを取得


ユーザ登録時のニック
ネームの初期値に利用

メールアドレスが取得で
きると便利なのに…




                 42
mixiのニックネームを取得
引数に :optional => :nickname を追加するだけ
  authenticate_with_open_id(params[:openid_url],
   :optional => :nickname) do ¦result, identity_url, registration¦
   if result.successful?
     if @current_user = User.find_by_identity_url(identity_url)
       successful_login
     else
       # identity_url (Verified Identifier) をセッションに保存
       session[:identity_url] = identity_url
       session[:nickname] = registration['nickname']
       # ユーザ登録画面へリダイレクトする
       redirect_to new_user_path
     end
                                                                     43
マイミク認証

mixi独自のマイミク認証を使ってみましょう




                         44
マイミク認証とは?

 自分が誰かのマイミクであることを認証

 普通のOpenIDと同じ仕組みで実現!


まちゅさんのマイミク
https://id.mixi.jp/1741395/friends/




                                      45
マイミク認証の例
マイミク認証の手順
identity_urlからmixiのユーザIDを取得

 https://id.mixi.jp/1741395/

後ろにfriends/を付与してClaimed Identityを生成

 https://id.mixi.jp/1741395/friends/



authenticate_with_openidメソッドでマイミク認証

                                       47
認証処理のコード
identity_urlの生成以外は普通のOpenID認証と同じ

class FriendsController < ApplicationController
 def show
   @user = User.find(params[:user_id])
   if @user.identity_url = %r¦^https://id.mixi.jp/d+¦
     identity_url = "#{@user.identity_url}/friends"
     authenticate_with_open_id(identity_url) do ¦result, identity_url¦
       unless result.successful?
        flash[:notice] = "あなたはマイミクではないようです!"
        redirect_to '/'
       end
     end

                                                                         48
マイミク認証の考察
マイミク認証&コミュニティ認証の
落とし穴・注意点を提起します



                   49
マイミク認証のおさらい
マイミク認証
 まちゅさんのマイミク
https://id.mixi.jp/1741395/friends/

コミュニティ認証
ニコニコ動画コミュニティの参加者
https://id.mixi.jp/community/1651291/




RPからは普通のOpenIDアカウントと同じにみえる


                                        50
コミュニティIDで
ユーザ登録してみる
 https://id.mixi.jp/community/1651291/




                                         51
登録できた…




         52
これって、みんなで使える
共通アカウントが作れる?
そんなことはありません
        User Supplied Identifier
ログイン前   https://id.mixi.jp/community/1651291/




        User Claimed Identifier           (openid.claimed_id)

        https://id.mixi.jp/community/1651291/1741395

ログイン後                                                個人のID
        User Verified Identifier (openid.identity)
        https://id.mixi.jp/1741395
Claimed ID or Verified ID
  サービス          Claimed ID                     Verified ID

  mixi認証      https://id.mixi.jp/1741395     https://id.mixi.jp/1741395



             https://id.mixi.jp/community/
コミュニティ認証          1651291/1741395
                                             https://id.mixi.jp/1741395



               https://id.mixi.jp/29704/
 マイミク認証            friends/1741395
                                             https://id.mixi.jp/1741395




  delegate      http://www.machu.jp/         https://id.mixi.jp/1741395




                                                                          55
参考

Relying PartyとIdentityの関連づけについて
 http://d.hatena.ne.jp/ZIGOROu/20080204/1202115281




RPはClaimed IDを紐づけるのが正解との結論




                                                     56
結論
間違ってもUser Supplied Identifierは記録しない

 OpenID Authenticationプラグインを使うとUser
 Claimed IDが記録される

コミュニティ認証やマイミク認証のIDでのユーザ登録
を拒否したければ、RPで個別対応

 ここだけClaimed IDではなくVerified IDを記録
おしまい



       58

Más contenido relacionado

Más de Kohei MATSUOKA

PStore and Memcached benchmark
PStore and Memcached benchmarkPStore and Memcached benchmark
PStore and Memcached benchmarkKohei MATSUOKA
 
How does the Waterfall model survive?
How does the Waterfall model survive?How does the Waterfall model survive?
How does the Waterfall model survive?Kohei MATSUOKA
 
Secured Authentication Method for Managing Consumer-Generated Information in ...
Secured Authentication Method for Managing Consumer-Generated Information in ...Secured Authentication Method for Managing Consumer-Generated Information in ...
Secured Authentication Method for Managing Consumer-Generated Information in ...Kohei MATSUOKA
 
about Shibuya.trac naming
about Shibuya.trac namingabout Shibuya.trac naming
about Shibuya.trac namingKohei MATSUOKA
 

Más de Kohei MATSUOKA (7)

PStore and Memcached benchmark
PStore and Memcached benchmarkPStore and Memcached benchmark
PStore and Memcached benchmark
 
How does the Waterfall model survive?
How does the Waterfall model survive?How does the Waterfall model survive?
How does the Waterfall model survive?
 
OpenID Introduction
OpenID IntroductionOpenID Introduction
OpenID Introduction
 
Secured Authentication Method for Managing Consumer-Generated Information in ...
Secured Authentication Method for Managing Consumer-Generated Information in ...Secured Authentication Method for Managing Consumer-Generated Information in ...
Secured Authentication Method for Managing Consumer-Generated Information in ...
 
about Shibuya.trac naming
about Shibuya.trac namingabout Shibuya.trac naming
about Shibuya.trac naming
 
ID Management
ID ManagementID Management
ID Management
 
yet another TDD
yet another TDDyet another TDD
yet another TDD
 

Último

【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)Hiroki Ichikura
 
スマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システムスマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システムsugiuralab
 
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略Ryo Sasaki
 
論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNetToru Tamaki
 
SOPを理解する 2024/04/19 の勉強会で発表されたものです
SOPを理解する       2024/04/19 の勉強会で発表されたものですSOPを理解する       2024/04/19 の勉強会で発表されたものです
SOPを理解する 2024/04/19 の勉強会で発表されたものですiPride Co., Ltd.
 
TSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdfTSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdftaisei2219
 
論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A surveyToru Tamaki
 
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...Toru Tamaki
 
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介Yuma Ohgami
 

Último (9)

【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
 
スマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システムスマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システム
 
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
 
論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet
 
SOPを理解する 2024/04/19 の勉強会で発表されたものです
SOPを理解する       2024/04/19 の勉強会で発表されたものですSOPを理解する       2024/04/19 の勉強会で発表されたものです
SOPを理解する 2024/04/19 の勉強会で発表されたものです
 
TSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdfTSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdf
 
論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey
 
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
 
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
 

OpenID with Rails