Wednesday, July 21, 2010

Google OpenID 導入分享

首先,先到下面這個連結註冊。
https://www.google.com/accounts/ManageDomains

註冊的步驟很簡單,只要填些資料後認證網站就好,我就不說明了,直接來看Google OpenID的說明文件


這次只介紹使用OpenID,所以還蠻單純的

Step 1 : Get OpenID endpoint
首先,對https://www.google.com/accounts/o8/id這個網址Get,正常來說你會下載一個叫做"id"沒有副檔名的檔案(除非你可以吃application/xrds+xml header),用notepad之類的打開後,會發現裡面內容是個類似XML格式的資料,這就是Google回傳的XRDS文件,內容是OpenID提供者(Google)端點位址,在<URI></URI>之中的網址就是我們要的東西。




Step 2 : Request OpenID Identity
這邊,簡單來說只要對剛剛取得的URI丟出規定的Request parameters就好,下面為會用的到參數,詳細的請看Google的文件。

Request OpenID Identity
參數名稱說明
openid.modeGoogle可接受checkid_immediate或checkid_setup,但使用checkid_immediate需另外設別的參數,所以這邊我填入checkid_setup
openid.ns填入http://specs.openid.net/auth/2.0
openid.return_to取得Request Token後導向的網址
openid.assoc_handle可不填
openid.claimed_id可不填,我是填入http://specs.openid.net/auth/2.0/identifier_select
openid.identity可不填,我是填入http://specs.openid.net/auth/2.0/identifier_select
openid.realm可不填,我是填入程式所在的網域

在第一步我取得的endpoint是https://www.google.com/accounts/o8/ud,所以送出的整串需求會是這樣

https://www.google.com/accounts/o8/ud?
openid.ns=http://specs.openid.net/auth/2.0
&openid.claimed_id=http://specs.openid.net/auth/2.0/identifier_select
&openid.identity=http://specs.openid.net/auth/2.0/identifier_select
&openid.return_to=http://yourwebsite.com/AuthSub.aspx
&openid.realm=http://yourwebsite.com/&openid.mode=checkid_setup

送出後,理論上只要你成功,就會看見下面請求確認的畫面(或是先Login Google Account才看到下面的畫面)

GoogleOpenID_2

上圖的確認畫面是順便要email的畫面,如果按照Step 2所說的參數送出是不會有那行的。

確認後,Google會把頁面導回openid.return_to設定的位址

回傳的樣子會長的像這樣

http://yourdomain/AuthSub.aspx?
openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0
&openid.mode=id_res
&openid.op_endpoint=https%3A%2F%2Fwww.google.com%2Faccounts%2Fo8%2Fud
&openid.response_nonce=2010-07-20T08%3A05%3A45Z13jt7jYD8hQSFA
&openid.return_to=http%3A%2F%2Fyourwebsite.com%2FAuthSub.aspx
&openid.assoc_handle=AOQobUeUjv0ABpLSc8OjMpRSJV4It_Wg6ZXjOU93FKv6K_L8Qx92TO4S
&openid.signed=op_endpoint%2Cclaimed_id%2Cidentity%2Creturn_to%2Cresponse_nonce%2Cassoc_handle
&openid.sig=7Qph6mxuOAqYYIocTpc%2FyLFvR6o%3D
&openid.identity=#####identity#####
&openid.claimed_id=#####identity#####

其中的openid.identity就是OpenID用來辨別使用者的唯一值,我們就可以拿這個值去運用了。


Step 3 : Get Addition Info

其實在上一步就可以結束了,不過Google文件中有提到可以在取得使用者識別碼時,同時取得一些資訊,就順便介紹一下。


Get Addition Info
參數名稱說明
openid.ns.ax填入http://openid.net/srv/ax/1.0
openid.ax.mode填入fetch_request
openid.ax.required需求之資訊,有country,email,firstname,language,lastname可選擇,如果同時要求多項則用逗號作為分隔
openid.ax.type.country有使用到的話填入http://axschema.org/contact/country/home
openid.ax.type.email有使用到的話填入http://axschema.org/contact/email或http://schema.openid.net/contact/email
openid.ax.type.firstname有使用到的話填入http://axschema.org/namePerson/first
openid.ax.type.language有使用到的話填入http://axschema.org/pref/language
openid.ax.type.lastname有使用到的話填入http://axschema.org/namePerson/last

使用上就是在Step 2 的Request上加上Step 3所需的參數,我只多加了需求email,所以Request會長的像下面這樣

https://www.google.com/accounts/o8/ud?
openid.ns=http://specs.openid.net/auth/2.0
&openid.claimed_id=http://specs.openid.net/auth/2.0/identifier_select
&openid.identity=http://specs.openid.net/auth/2.0/identifier_select
&openid.return_to=http://yourwebsite.com/AuthSub.aspx
&openid.realm=http://yourwebsite.com/&openid.mode=checkid_setup
&openid.ns.ax=http://openid.net/srv/ax/1.0
&openid.ax.mode=fetch_request
&openid.ax.type.email=http://schema.openid.net/contact/email
&openid.ax.required=email


而收到的值會長的像下面這樣

http://yourdomain/AuthSub.aspx?
openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0
&openid.mode=id_res
&openid.op_endpoint=https%3A%2F%2Fwww.google.com%2Faccounts%2Fo8%2Fud
&openid.response_nonce=2010-07-20T08%3A22%3A44Zg6DFJKBUAvwT8w
&openid.return_to=http%3A%2F%2Fyourwebsite.com%2FAuthSub.aspx
&openid.assoc_handle=AOQobUcXKwMlCGaFohdD6hPXVzUUtNMJWPhYeoeFoJ3Kt0CaGJqP5k1h
&openid.signed=op_endpoint%2Cclaimed_id%2Cidentity%2Creturn_to%2Cresponse_nonce%2Cassoc_handle%2Cns.ext1%2Cext1.mode%2Cext1.type.email%2Cext1.value.email
&openid.sig=zZYOqWnLSrheQW31gsCVBq50BaE%3D
&openid.identity=#####identity#####
&openid.claimed_id=#####identity#####
&openid.ns.ext1=http%3A%2F%2Fopenid.net%2Fsrv%2Fax%2F1.0
&openid.ext1.mode=fetch_response
&openid.ext1.type.email=http%3A%2F%2Fschema.openid.net%2Fcontact%2Femail
&openid.ext1.value.email=user_email

這樣就可以在取得OpenID識別的同時,取得一些使用者資訊了 (當然要使用者按下同意才有用)

18 comments:

  1. 又來請教一下。
    之前因為我把留言檢視功能關掉,所以人家的留言可以直接顯示。
    最近有廣告留言,可是我找不到哪裡可以刪除該則留言耶~
    可以教一下路徑嗎?謝謝唷

    ReplyDelete
  2. 登入後看那篇文章,該留言的時間後面有個垃圾桶,按下去就是了。

    ReplyDelete
  3. 您好,因為文章沒有看到刪除的地方(或垃圾桶)所以就沒法刪除。(該不會我掛留言分類語法時不小心把垃圾桶語法刪了吧?)
    因為對方每天都來留,實在很煩,所以現在有開審核才可以公開,目前新留言我可以在後台直接刪除,但之前他留的那則就不知道怎麼刪?
    可以封鎖特定人士留言嗎?

    ReplyDelete
  4. 刪除的方式有點複雜,所以我用email的方式給你了,試試看如果有問題請再跟我說。
    封鎖特定人士留言我猜是不行的。

    ReplyDelete
  5. 你好
    我是OPENID最近剛開始研究的新手
    我想請問一下我所送出去的參數
    就是在您第二步的地方
    檔案格式也是xml檔嗎??
    我是新手還在摸索麻煩請您解惑了@@

    ReplyDelete
  6. 不是xml,只是送個web post(我忘記get能不能) , 你看"所以送出的整串需求會是這樣"這句話下面那個框框就是了

    ReplyDelete
  7. 我取的了 identity
    是一串網址

    我該如何 取得後面的參數??

    ReplyDelete
  8. 您好~

    小弟 想請教大大您 關於後續的 check_authentication 要怎麼實做呢?


    參考資料
    http://zh.wikipedia.org/wiki/OpenID

    ReplyDelete
  9. 請問一下 check_authentication 會需要在哪邊用到呢 ?

    ReplyDelete
  10. 從google 轉址回來 openid.return_to 會有帶參數的資料, 可是我哪知道 那串網址是從 google 導向回來的呢? 或許是直接 複製貼上 那串從google 收到的值

    所以需要 check_authentication 將得到的資料 跟 google 端做驗證 才能確保使用者 確實是經由google提供的資料

    參考資料
    http://www.sozkan.com/blog/2010/09/24/very-quick-openid-integration-tutorial/

    但是我一直實做不成功 Orz

    ReplyDelete
  11. 你可以看來源,這應該很好判斷。

    ReplyDelete
  12. btw我沒跟google做過check_authentication這件事,也許每家後續的動作不一樣,或是各家的說法不同,但是要認定它是從哪來的倒是很簡單。

    ReplyDelete
  13. 或許可以透過 HTTP REFERER 得知來源吧~

    可是 OpenID 裡有提到 11.4.2.1.

    http://openid.net/specs/openid-authentication-2_0.html#verifying_signatures

    check_authentication

    我覺得還是怪怪的... 如果沒有這步驟 他怎麼知道 收到的值正確的呢?

    ReplyDelete
  14. 我使用 PHP 實做出來了 , 您參考一下 關於 check_authentication 模式

    http://test.com2am.com/openid/

    ReplyDelete
  15. 感謝你!! 我會再找時間研究 :)

    ReplyDelete
  16. This comment has been removed by the author.

    ReplyDelete