自動目錄
此文分為幾個部分:
[PHP] OPENID CONNECT #0 簡介及取得URL
[PHP] OPENID CONNECT #1 取得ACCESS TOKEN
[PHP] OPENID CONNECT #2 取得USERINFO及PROFILE
[PHP] OPENID CONNECT #3 驗證id_token
[PHP] OPENID CONNECT #4 程式下載及安裝
上一篇已經有取得ACCESS TOKEN並把access token記到SESSION中,以下是取得access token的函數
PHP
public function getAccessToken($token_ep='' ,$code='', $redir_uri='' ,$rtn_array=false){
$hash = base64_encode( CLIENT_ID . ":" . CLIENT_SECRET);
$data = array('grant_type' => 'authorization_code', 'code'=> $code,
'redirect_uri' => $redir_uri);
$header= array( "Content-type: application/x-www-form-urlencoded",
"Authorization: Basic $hash" ) ;
$options = array(
'http' => array(
'header' => $header,
'method' => 'POST',
'content' => http_build_query($data)
));
$context = stream_context_create($options);
$result = file_get_contents($token_ep, false, $context);
$j= json_decode($result, $rtn_array);
return $j;
}
此函數回傳的陣列內容範例
<==BASE64編碼,部分內容省略==>
[token_type] => Bearer [expires_in] => 3600 )
其中有幾個欄位,簡略說明如下:
access_token 存取用權杖
refresh_token 刷新用權杖
scope 此權杖可使用的權限。openid為必要,其餘為選用,申請時OP有註記可使用scope。多筆scope用空白分格,順序不重要。
id_token id_token 為使用者有關的基本資訊,下一篇OPENID CONNECT #3 驗證id_token會介紹
token_type 使用 Bearer,Base64 encoded
expires_in 有效期限,單位秒
取得USERINFO
接下來可以使用儲存在 session 中的 access token來取回使用者的userinfo,userinfo的內容由 access token定義的scope來決定,輸出的結果是JSON格式,一個userinfo的範例為:
其中有幾個欄位這些欄位稱為 CLAIMS,簡略說明如下:
sub 帳號唯一識別碼
preferred_name openid帳號,也就是本部帳號
name 中文姓名
email 第二電子郵件信箱
PHP
userinfo.php
<?php
//驗證 access token
if( !isset($_SESSION['access_token'])){
die ("無存取用權杖,無法取回使用者資料!");
}
//取回access token
include "config.php";
include "library.class.php";
$obj= new openid();
$token_ep=USERINFO_ENDPOINT;
if(DYNAMICAL_ENDPOINT){
$token_ep=$ep->getEndPoint()->userinfo_endpoint;
}
$userinfo = $obj->getUserinfo($token_ep ,$_SESSION['access_token'], true);
if( !$userinfo) {
die ("無法取得 USER INFO");
}
// 把access token記到session中
print_r($userinfo);
第18行的函數getUserinfo內容如下:
public function getUserinfo($token_ep='' ,$accesstoken='',$rtn_array=false){
$header= array( "Authorization: Bearer $accesstoken" );
$options = array(
'http' => array(
'header' => $header,
'method' => 'GET',
'content' => ''
));
$context = stream_context_create($options);
$result = file_get_contents($token_ep, false, $context);
$u= json_decode($result,$rtn_array);
return $u;
}
取得PROFILE
目前此功能未來可能會整合至profile的scope,因此只暫時列出來參考,取得使用者的profile,只需將 profile的endpoint代入上方函式即可:
$profile = $obj->getUserinfo(PROFILE_ENDPOINT ,$_SESSION['access_token'], true);
profile的內容範例
Array ( [fullname] => 張〇✖ [schoolid] => 193526 [guid] => CC9FEA71B4A386F02349021---30CEA894A6AA78770D1D8D9B6 [sub] => c1b6b0be-b0d3-452a-9dc1-78793cc97eac [titles] => Array ( [0] => Array ( [schoolid] => 193526 [title] => Array ( [0] => 學生 ) ) ) [roles] => Array ( [servicename] => edumail [role] => Array ( [0] => 學生 ) ) [ncnu] => )
下一篇: [PHP] OPENID CONNECT #3 驗證id_token(撰寫中)
