ページ

2011年4月8日金曜日

[php] livedoor Auth を使用してみる

このエントリーをはてなブックマークに追加



<?php

class Clivedoor{

// REQUEST URL
private $requestURL = "http://auth.livedoor.com/login/?";
// Get User ID URL
private $userIdURL = "http://auth.livedoor.com/rpc/auth";


// 「元データ」と「共通鍵」を合成して、その結果に対しハッシングして ハッシュ値を生成 (HMAC)
// secret_keyはhttp://auth.livedoor.com/でアプリケーションの登録をして取得
private $secret_key = "dofijrg39t8ughvw0(sample)";
// ハッシュの名前
private $hash = "sha1";


// 使用する要素
private $app_key = "app_key";
private $format = "format";
private $perms = "perms";
private $t = "t";
private $token = "token";
private $userdata = "userdata";
private $v = "v";
private $sig = "sig";
// ログインの確認
private $loginFlag = "livedoor";
// $_GET[$this->userdata]の中身
public $callBackParameter = "";
// 要素を格納する配列 (URL)
private $paramURL = Array();
// 要素を格納する配列 (XML)
private $paramXML = Array();



//===========================================
// 初期設定
//===========================================
public function __construct(){

// 要素はアルファベット順に並べてパラメータとつなげてhash_hmac();
// app_keyはhttp://auth.livedoor.com/でアプリケーションの登録をして取得
$this->paramURL[$this->app_key] = "asdw9850tugbiodhf2(sample)";
$this->paramURL[$this->perms] = "id";
$this->paramURL[$this->t] = time();
$this->paramURL[$this->userdata] = $this->loginFlag;
// 浮動小数ではなく文字列 "1.0" / ver 1.0 (2011/3/3)
$this->paramURL[$this->v] = "1.0";
$this->paramURL[$this->sig] = $this->makeSig($this->paramURL);

// Login Success
if(isset($_GET[$this->userdata])){
if($_GET[$this->userdata] === $this->loginFlag){
$this->callBackParameter = $_GET[$this->userdata];
$this->paramXML[$this->app_key] = $_GET[$this->app_key];
$this->paramXML[$this->format] = "xml";
$this->paramXML[$this->t] = $_GET[$this->t];
$this->paramXML[$this->token] = $_GET[$this->token];
$this->paramXML[$this->v] = $_GET[$this->v];
$this->paramXML[$this->sig] = $this->makeSig($this->paramXML);
}
}

}



//===========================================
// シグネチャの作成
// argument :
// $array : パラメータ
// return value :
// シグネチャ(パラメータと値をアルファベット順にソートして, hmac(sha1)でハッシュ化)
//===========================================
private function makeSig($array){

// 元データの作成
$buf = "";
foreach($array as $key=>$value){
$buf .= $key.$value;
}
// hmacによるハッシュの作成
return hash_hmac($this->hash, $buf, $this->secret_key);

}



//===========================================
// REQUEST URLの作成
// return value :
// REQUEST URL
//===========================================
public function makeURL(){

$url = $this->requestURL;

// REQUEST URLの生成
$count = 0;
foreach($this->paramURL as $key=>$value){
if($count){ $url .= "&"; }
if(!$count){ $count++; }
$url .= $key."=".$value;
}

return $url;
}



//===========================================
// User情報が格納されたXMLの取得
// return value :
// XML
//===========================================
public function makeXML(){

$url = $this->userIdURL;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->paramXML);
$userId = curl_exec($ch);
curl_close($ch);

preg_match("/(message>)([^<]*)/", $userId, $message);
preg_match("/(livedoor_id>)([^<]*)/", $userId, $livedoor_id);

// ログインに成功
if($message[2] === "SUCCESS"){
// userIDを返す
return $livedoor_id[2];
}
// ログインに失敗
return "ログインに失敗しました. お手数ですが, もう一度お試しください.";
}


};



// Clivedoorのインスタンス化
$objLivedoor = new Clivedoor();

print('<a href="'.$objLivedoor->makeURL().'">'.$objLivedoor->makeURL().'</a>');

print("<br>");

// もし成功しているならユーザIDを表示する
if($objLivedoor->callBackParameter){print($objLivedoor->makeXML());}

?>

0 件のコメント:

コメントを投稿