ラベル OpenID4Java関連 の投稿を表示しています。 すべての投稿を表示
ラベル OpenID4Java関連 の投稿を表示しています。 すべての投稿を表示

2012年11月11日日曜日

OpenID Simple Registration Extension


OpenID Attribute Exchange」では、OpenID Attribute Exchange 1.0を使用し、ユーザの属性情報をOPから取得した。
ここでは、Simple Registration Extensionを使用して取得してみる。

■ 「consumer_redirect.jsp」の改造
「consumer_redirect.jsp」において、SRegRequestクラスを作成し、認証要求のクラスであるAuthRequestにアタッチする。

-------------------------------------------------------------------------------------------------------------

            // create Simple Registration Exchange
            SRegRequest sregReq = SRegRequest.createFetchRequest();
            // set attribute
            sregReq.addAttribute("gender", true);
            // attach the extension to the authentication request
            authReq.addExtension(sregReq);
-------------------------------------------------------------------------------------------------------------

さあ、確認しようと思ったら、どうやらYahoo!Japanは、Attribute Exchangeはサポートしているが、Simple Registration Extensionまではサポートしていないらしい。
がっくり。。。。

http://developer.yahoo.co.jp/other/openid/extension.html

OpenID Attribute Exchange

OpenID4Java付属のサンプルアプリを使用し、Yahoo!JapanとOpenID認証する」で使用したRPサンプリアプリケーションでは、OPに認証要求しか実施していない。
今回は、OPからユーザの属性情報も取得できるように、RPサンプルアプリケーションをちょこっと改造する。

OpenID2.0規約として、ユーザの属性情報を取得する仕様は、以下の2つが規定されている。

・Simple Registration Extension
→認証要求のプロトコルの中に、要求する属性名を指定する方法。

・Attribute Exchange
→認証要求とは別のプロトコルを使用し、属性取得を要求する方法。

OPとして使用するYahoo!Japanでは、以下のユーザの属性情報を取得できる。

http://developer.yahoo.co.jp/other/openid/extension.html

今回は、「Attribute Exchange」を使用し、性別情報を取得する。

■ OpenID4Java RPサンプルアプリケーションのロジック
サンプルアプリケーションでは、いくつかjspファイルがあり、「consumer_redirect.jsp」で認証要求を作成している。ソースをみてみると、以下のようになっている。
Attribute Exchangeを実装するには、認証要求の作成後に、ロジックを追加する必要がある。

【consumer_redirect.jspから一部抜粋】
---------------------------------------------------------------

(・・・中略・・・)

// RPの機能を提供するConsumerManagerをnew
ConsumerManager newmgr=new ConsumerManager();
// アソシエーションフェーズで使用するオブジェクトをセット
newmgr.setAssociations(new InMemoryConsumerAssociationStore());
// リプレイ攻撃を防ぐNonce値をセット
newmgr.setNonceVerifier(new InMemoryNonceVerifier(5000));


(・・・中略・・・)


// perform discovery on the user-supplied identifier
// OPのディスカバリ
List discoveries = manager.discover(openid);

// attempt to associate with an OpenID provider
// and retrieve one service endpoint for authentication
// OPとのアソシエーション
DiscoveryInformation discovered = manager.associate(discoveries);

// store the discovery information in the user's session
session.setAttribute("openid-disco", discovered);

// obtain a AuthRequest message to be sent to the OpenID provider
// 認証要求の作成
AuthRequest authReq = manager.authenticate(discovered, returnToUrl);

(・・・中略・・・)
---------------------------------------------------------------

■ 「consumer_redirect.jsp」の改造
「consumer_redirect.jsp」において、属性情報の要求クラスに相当するFetchRequestクラスを使用する。Yahoo!Japan(OP)からは、性別(http://axschema.org/person/gender)を取得するために、addAttributeメソッドで要求対象の属性情報を設定する。

---------------------------------------------------------------

// Attribute Exchangeリクエストを作成
FetchRequest fetch = FetchRequest.createFetchRequest();
// 属性情報の設定
fetch.addAttribute("gender",                                           // attribute alias
                         "http://axschema.org/person/gender",    // type URI
                          true);                                                // required

// attach the extension to the authentication request
// 認証要求にセット
 authReq.addExtension(fetch);

---------------------------------------------------------------

上記を改造した後、marvenコマンドでwarファイルを再作成する。
OPで認証した後に、以下のような、OpenIDに加えて、属性情報をRPに通知してよいかの同意画面が表示される。


また、OPからRPに通知されたURLをみてみると、以下のように属性情報(gender)が通知されていることがわかる(下線部分)。

http://localhost:8080/simple-openid/consumer_returnurl.jsp?openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.mode=id_res&openid.return_to=http%3A%2F%2Flocalhost%3A8080%2Fsimple-openid%2Fconsumer_returnurl.jsp&openid.claimed_id=https%3A%2F%2Fme.yahoo.co.jp%2Fa%2F***&openid.identity=https%3A%2F%2Fme.yahoo.co.jp%2Fa%2F***&openid.assoc_handle=***&openid.realm=http%3A%2F%2Flocalhost%3A8080%2Fsimple-openid%2Fconsumer_returnurl.jsp&openid.ns.ax=http%3A%2F%2Fopenid.net%2Fsrv%2Fax%2F1.0&openid.ax.mode=fetch_response&openid.ax.value.gender=M&openid.response_nonce=***&openid.signed=assoc_handle%2Cclaimed_id%2Cidentity%2Cmode%2Cns%2Cop_endpoint%2Cresponse_nonce%2Creturn_to%2Csigned%2Cax.value.gender%2Cax.type.gender%2Cns.ax%2Cax.mode%2Cns.pape%2Cpape.auth_policies%2Cpape.auth_level.ns.nist%2Cpape.auth_level.nist&openid.op_endpoint=https%3A%2F%2Fopen.login.yahooapis.jp%2Fopenid%2Fop%2Fauth&openid.ax.type.gender=http%3A%2F%2Faxschema.org%2Fperson%2Fgender&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.pape.auth_policies=http%3A%2F%2Fschemas.openid.net%2Fpape%2Fpolicies%2F2007%2F06%2Fnone&openid.pape.auth_level.ns.nist=http%3A%2F%2Fcsrc.nist.gov%2Fpublications%2Fnistpubs%2F800-63%2FSP800-63V1_0_2.pdf&openid.pape.auth_level.nist=0&openid.sig=***


2012年11月10日土曜日

OpenID4Java付属のサンプルアプリを使用し、Yahoo!JapanとOpenID認証する

OpenID4Javaでは、OpenID2.0認証用のJavaライブラリを提供する。
このライブラリに付属するRPサンプリアプリケーションを使用し、Yahoo!Japan(OP)と認証してみる。

おおざっぱなシステム構成およびシーケンスは以下になる。
■前提条件
・以下のURLにアクセスし、OpenIDを予め発行しておく。
http://openid.yahoo.com/

・RPの構築環境に、JavaおよびApache marvenをインストールしていること。


■設定手順
①以下のサイトからOpenID4Java媒体をダウンロードする(openid4java-full-0.9.5.593.tar.gzを使用)。ダウンロードした媒体を任意のディレクトリに解凍する。

http://code.google.com/p/openid4java/

②コマンドプロンプトを起動し、以下のmavernコマンドを実行して、RPサンプリアプリケーションのwarファイルを作成する。

--------------------------------------------------------------------------------

d:\>cd d:\openid4java-full-0.9.6.662\samples\simple-openid
d:\openid4java-full-0.9.5.593\samples\simple-openid>d:\apache-maven-3.0.4\bin\mvn war:war
--------------------------------------------------------------------------------

③RPサンプリアプリケーションはJetty上で動かす。mavernコマンドを実行してJettyを起動する。

--------------------------------------------------------------------------------
d:\openid4java-full-0.9.5.593\samples\simple-openid>d:\apache-maven-3.0.4\bin\mvn jetty:run
--------------------------------------------------------------------------------

以下にアクセスし、正常にアクセスできることを確認する。
http://localhost:8080/simple-openid/


④RP画面のOpenID欄に、Yahooで発行したOpenIDを入力し、「送信」ボタンを押下する。押下後、Yahoo(OP)に対して、OPのディスカバリとアソシエーションのプロトコルが実行される。

--------------------------------------------------------------------------------

[INFO] Discovery - Starting discovery on URL identifier: https://me.yahoo.co.jp/a/*********
[INFO] YadisResolver - Yadis discovered 1 endpoints from: https://me.yahoo.co.jp/a/********
[INFO] Discovery - Discovered 1 OpenID endpoints.  ★OP検索完了!
[INFO] ConsumerManager - Trying to associate with https://open.login.yahooapis.jp/openid/op/auth attempts left: 4
[WARN] HttpMethodBase - Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
[INFO] ConsumerManager - Associated with https://open.login.yahooapis.jp/openid/op/auth handle: *******  ★アソシエーション完了!
[INFO] ConsumerManager - Creating authentication request for OP-endpoint: https://open.login.yahooap is.jp/openid/op/auth claimedID: https://me.yahoo.co.jp/a/****    OP-specific ID: https://me.yahoo.co.jp/a/******* ★認証要求の作成完了!
--------------------------------------------------------------------------------


⑤RPからOPに認証要求をPOSTするタイミングで、画面に「Continue」ボタンが表示される。「continue」ボタンを押下する。


⑥Yahoo!Japan側のログイン画面が表示されるため、ID/PWを入力する。

⑦認証に成功すれば、OpenIDをRPに通知してよいかを求める同意画面が表示される。「同意する」ボタンを押下する。


⑧OPからRPへ認証応答が通知され、RP側でレスポンスの内容を検証する。検証OKであれば、以下のように画面が表示される。



--------------------------------------------------------------------------------

[INFO] ConsumerManager - Verifying authentication response...
[INFO] ConsumerManager - Received positive auth response.
[INFO] ConsumerManager - Found association: ****-- verifying signature locally...
[INFO] ConsumerManager - Verification succeeded for: https://me.yahoo.co.jp/a/*********
--------------------------------------------------------------------------------

感想:Yahoo!Japanとすごく簡単につながった!!!