Openpearã§HTTP_OAuthProviderãå ¬éãã¦ã¿ã
å
¬éããã
ã
Openpearã§ããã±ã¼ã¸ãå
¬éããã¨ã¡ã¼ã«ã¢ãã¬ã¹ãå
¬éãããã¨ããäºããã£ãç¥ã£ãã
ãããããã¨æã£ã¦ãæä¸ã®Pearæºæ ã®ã³ã¡ã³ãé¨åã§æ¬åãæããã
ã
ããã°ã©ã ã¯å»å¹´æ¸ãããã®ããã¼ã¹ã«ãããã ãã©ãçµæ§æ¸ãæ¹ãå¤ãã£ãã
ãªããªãããæãã®æ¸ãæ¹ãåºæ¥ãããããªããã¨æã£ã¦ãã
ã
HTTP_OAuthProvider - Openpear
http://openpear.org/package/HTTP_OAuthProvider
ã
/HTTP_OAuthProvider/trunk - ãªãã¸ããªãã©ã¦ã¶ - Openpear
http://openpear.org/repository/HTTP_OAuthProvider/trunk
ã
æ©è½
OAuthãªã¯ã¨ã¹ãã®èªè¨¼ãè¡ããµã¼ãã¹ãããã¤ãå´ã®ã©ã¤ãã©ãªã
2Legged OAuthã¨3Legged OAuthã®ä¸¡æ¹ã«å¯¾å¿ã
対å¿ãã¦ããã·ã°ããã£æ¹å¼ã¯ãHMAC-SHA1ã¨RSA-SHA1ã
ãã¼ã¯ã³ã®ä¿æã«ãCache_Liteã使ç¨ã
ãã¼ã¯ã³ä¿æã®æ¹æ³ã¯ä»å¾ããã¤ãå¢ããäºå®ã
ã
ã¤ã³ã¹ãã¼ã«
Openpearã®ãã£ã³ãã«è¿½å ã
[root@localhost ~]# pear channel-discover openpear.org Adding Channel "openpear.org" succeeded Discovery of channel "openpear.org" succeeded
ã
HTTP_OAuthProviderã®ã¤ã³ã¹ãã¼ã«ã
[root@localhost ~]# pear install openpear/HTTP_OAuthProvider downloading HTTP_OAuthProvider-1.0.0.tgz ... Starting to download HTTP_OAuthProvider-1.0.0.tgz (6,719 bytes) .....done: 6,719 bytes install ok: channel://openpear.org/HTTP_OAuthProvider-1.0.0
ã
ãµã³ãã«
ç¾æç¹ã§ããã«æ¸ãã¦ãããã®ã¨åãã ãã©â¦ã
ã
/HTTP_OAuthProvider/trunk/example - ãªãã¸ããªãã©ã¦ã¶ - Openpear
http://openpear.org/repository/HTTP_OAuthProvider/trunk/example
ã
2Legged OAuthèªè¨¼ãè¡ã
<?php require_once('HTTP/OAuthProvider.php'); require_once('config.php'); $o = new HTTP_OAuthProvider(); try { $o->setFetchConsumerHandler('fetchConsumer'); $o->authenticate(); echo "Auth OK!!!\n"; printf("consumer: %s\n", $o->getConsumer()->getKey()); } catch(Exception $e) { header(sprintf('HTTP/1.0 %d', $e->getCode())); echo $e->getMessage(); }
ã
ãªã¯ã¨ã¹ããã¼ã¯ã³ãçºè¡ãã
<?php require_once('HTTP/OAuthProvider.php'); require_once('config.php'); $o = new HTTP_OAuthProvider(); try { $o->setFetchConsumerHandler('fetchConsumer'); echo $o->issueRequestToken(); } catch(Exception $e) { header(sprintf('HTTP/1.0 %d', $e->getCode())); echo $e->getMessage(); }
ã
ã¦ã¼ã¶ã®èªå¯ãä¿åãã(ãã©ã¼ã ã表示)
<?php require_once('HTTP/OAuthProvider.php'); require_once('config.php'); $content = "<b>this is provider's page !!</b><br />\n"; $o = new HTTP_OAuthProvider(); try { $o->setFetchConsumerHandler('fetchConsumer'); // check request token if (!$o->existsRequestToken()) { throw new Exception('not found request token', 200); } // show callback url if (isset($_REQUEST['authorize_confirm'])) { if ($_REQUEST['authorize_confirm']) { $callback = $o->authorizeToken($user_id, true); $content .= "you choose agree<br />\n"; } else { $callback = $o->authorizeToken($user_id, false); $content .= "you choose disagree<br />\n"; } $content .= "return to consumer's page<br />\n"; $content .= sprintf('<a href="%s">%s</a>', $callback, $callback)."<br />\n"; // show form } else { $content .= sprintf("hello. user id %s !<br />\n", $user_id); $content .= "do you authorize the consumer?<br />\n"; $content .= sprintf("consumer is %s<br />\n", $o->getConsumer()->getKey()); // agree form $content .= '<form action="?" method="post">'."\n"; $content .= sprintf('<input type="hidden" name="oauth_token" value="%s" />', $_REQUEST['oauth_token'])."\n"; $content .= '<input type="hidden" name="authorize_confirm" value="1" />'."\n"; $content .= sprintf('<input type="submit" value="agree">')."\n"; $content .= "</form>\n"; // disagree form $content .= '<form action="?" method="post">'."\n"; $content .= sprintf('<input type="hidden" name="oauth_token" value="%s" />', $_REQUEST['oauth_token'])."\n"; $content .= '<input type="hidden" name="authorize_confirm" value="0" />'."\n"; $content .= sprintf('<input type="submit" value="disagree">')."\n"; $content .= "</form>\n"; } } catch(Exception $e) { header(sprintf('HTTP/1.0 %d', $e->getCode())); $content .= $e->getMessage(); } ?> <html> <head> <title>provider's page</title> </head> <body> <?php echo $content; ?> </body> <html>
ã
èªå¯ããããªã¯ã¨ã¹ããã¼ã¯ã³ãã¢ã¯ã»ã¹ãã¼ã¯ã³ã«äº¤æãã
<?php require_once('HTTP/OAuthProvider.php'); require_once('config.php'); $o = new HTTP_OAuthProvider(); try { $o->setFetchConsumerHandler('fetchConsumer'); echo $o->exchangeAccessToken(); } catch(Exception $e) { header(sprintf('HTTP/1.0 %d', $e->getCode())); echo $e->getMessage(); }
ã
ä¿è·ããããªã½ã¼ã¹ã¸ã®ã¢ã¯ã»ã¹ã®èªè¨¼ãè¡ã
<?php require_once('HTTP/OAuthProvider.php'); require_once('config.php'); $o = new HTTP_OAuthProvider(); try { $o->setFetchConsumerHandler('fetchConsumer'); $o->authenticate3L(); echo "this is provider's protected resource !!\n"; printf("consumer: %s\n", $o->getConsumer()->getKey()); printf("user_id: %s\n", $o->getStore()->getUserID()); } catch(Exception $e) { header(sprintf('HTTP/1.0 %d', $e->getCode())); echo $e->getMessage(); }
ã
è¨å®ãã¡ã¤ã«
ã³ã³ã·ã¥ã¼ããã¦ã¼ã¶ãåºå®ã
ã³ã³ã·ã¥ã¼ãã¯1人ã ãããæ³å®ã
ã¦ã¼ã¶ã¯ID12345ããã°ã¤ã³æ¸ã¿ã¨ããæ³å®ã
<?php // consumer function fetchConsumer($consumer_key) { $row = array( 'id' => 1, 'key' => 'testconsumer', 'secret' => 'testpass', 'publickey' => null ); if ($consumer_key==$row['key']) { $consumer = new HTTP_OAuthProvider_Consumer($row); return $consumer; } } // user $user_id = 12345;
ã
ãã¹ãç¨ã³ã³ã·ã¥ã¼ã
<?php // require HTTP_OAuth require_once('HTTP/OAuth/Consumer.php'); /* èªè¨¼æ å ±ãæ ¼ç´ããããã«ã»ãã·ã§ã³ãéå§ */ session_start(); /* Consumer key */ $consumer_key = 'testconsumer'; /* Consumer Secret */ $consumer_secret = 'testpass'; /* ãããã¤ãããã® Callback url */ $callback_url = sprintf('http://%s%s', $_SERVER['HTTP_HOST'], $_SERVER['SCRIPT_NAME']); $provider_base = 'http://example.com/example/'; $request_url = $provider_base.'3legged_request.php'; $authorize_url = $provider_base.'3legged_authorize.php'; $access_url = $provider_base.'3legged_access.php'; $resource_url = $provider_base.'3legged_resource.php'; /* Set up placeholder */ $content = ''; /* ã»ãã·ã§ã³ã®ã¯ãªã¢ */ if (@$_REQUEST['test'] === 'clear') { session_destroy(); session_start(); } try { // -- init HTTP_OAuth_Consumer $oauth = new HTTP_OAuth_Consumer($consumer_key, $consumer_secret); // ssléä¿¡ãå¯è½ã« $http_request = new HTTP_Request2(); $http_request->setConfig('ssl_verify_peer', false); $consumer_request = new HTTP_OAuth_Consumer_Request; $consumer_request->accept($http_request); $oauth->accept($consumer_request); if (!empty($_REQUEST['oauth_token']) && $_SESSION['oauth_state'] === 'start') { // -- ãããã¤ãããèªè¨¼å¾ã«æ»ã£ã¦ããå ´å (callbackå¦ç) $_SESSION['oauth_state'] = 'returned'; if (empty($_SESSION['oauth_access_token']) || empty($_SESSION['oauth_access_token_secret'])) { // -- access_tokenãæªåå¾ã®å ´å /* request tokenãã»ãã */ $oauth->setToken($_SESSION['oauth_request_token']); $oauth->setTokenSecret($_SESSION['oauth_request_token_secret']); /* ãããã¤ãããæ»ã£ã¦ãã oauth_verifierãã»ãã */ $oauth_verifier = $_REQUEST['oauth_verifier']; /* Access token ããªã¯ã¨ã¹ã */ $oauth->getAccessToken($access_url, $oauth_verifier); /* Acces tokenãä¿å (å®éã®ã¢ããªã±ã¼ã·ã§ã³ã§ã¯ãããDBçã«ä¿åãã¦ããã¾ãã) */ $_SESSION['oauth_access_token'] = $oauth->getToken(); $_SESSION['oauth_access_token_secret'] = $oauth->getTokenSecret(); } } if (!empty($_SESSION['oauth_access_token']) && !empty($_SESSION['oauth_access_token_secret'])) { // -- èªè¨¼æ¸ã¿ã®å ´å /* access_tokenãã»ãã */ $oauth->setToken($_SESSION['oauth_access_token']); $oauth->setTokenSecret($_SESSION['oauth_access_token_secret']); /* ã¦ã¼ã¶æ å ±ãåå¾ãããªã¯ã¨ã¹ããçºè¡. */ $result = $oauth->sendRequest($resource_url, array(), 'GET'); /* ãã¼ã¿ãåå¾ */ $content = $result->getBody(); } else { // -- ååå¼ã³åºãæ /* ãããã¤ãããrequest_tokenã®åå¾ */ $oauth->getRequestToken($request_url, $callback_url); /* tokenãã»ãã·ã§ã³ã«ä¿å */ $_SESSION['oauth_request_token'] = $oauth->getToken(); $_SESSION['oauth_request_token_secret'] = $oauth->getTokenSecret(); /* ã¹ãã¼ã¿ã¹ãstartã«ã»ãã */ $_SESSION['oauth_state'] = "start"; /* authorization URL ãåå¾ */ $request_link = $oauth->getAuthorizeURL($authorize_url); /* authorization URLã®ãªã³ã¯ãä½æ */ $content = 'Click on the link to go to provider to authorize your account.'; $content .= '<br /><a href="'.$request_link.'">'.$request_link.'</a>'; } } catch (Exception $e) { $content = $e->getMessage(); } ?> <html> <head> <title>OAuth in PHP</title> </head> <body> <h2>Welcome to a OAuth PHP example.</h2> <p><a href='<?php echo $_SERVER['PHP_SELF']; ?>?test=clear'>clear sessions</a></p> <p><pre><?php print_r($content); ?><pre></p> </body> </html>
ã
3Legged OAuthã®ã³ã³ã·ã¥ã¼ãå´ã®ãµã³ãã«ã¯ãããããæåããã
ã
HTTP_OAuthãå©ç¨ãã¦Twitterã®OAuthèªè¨¼ãè¡ã @ php-tips
http://php-tips.com/php/2009/11/http_oauth%E3%82%92%E5%88%A9%E7%94%A8%E3%81%97%E3%81%A6twitter%E3%81%AEoauth%E8%AA%8D%E8%A8%BC%E3%82%92%E8%A1%8C%E3%81%86
ã
OAuthã®ä»æ§ããã解ããªã人ç¨URL
OAuthãããã³ã«ã®ä¸èº«ããã£ãã解説ãã¦ã¿ãã - ããããæ¥è¨
http://d.hatena.ne.jp/yuroyoro/20100506/1273137673
ã
PHPãèªãã ãã¨ãã人ã®ããã®OAuthã®Signature解説 - r-weblife
http://d.hatena.ne.jp/ritou/20090912/1252776563
ã
OpenSocialã®ãµã¼ãã¹ãããã¤ãã£ã½ãã®ãä½ã£ã¦ã¿ã 5.OAuth Core 1.0a(3legged OAuth)ã®ä»æ§ - eth0jpã®æ¥è¨
http://d.hatena.ne.jp/eth0jp/20090826/1251228344
ã
OpenSocialã®ãµã¼ãã¹ãããã¤ãã£ã½ãã®ãä½ã£ã¦ã¿ã 6.OAuth Core 1.0a(3legged OAuth)ãå®è£
ãã¦ã¿ã - eth0jpã®æ¥è¨
http://d.hatena.ne.jp/eth0jp/20090829/1251497285
ã
ãOAuth Core 1.0 Revision Aãæ¥æ¬èªè¨³ãã¤ãã£ã¦ã¿ã - tzmtkã®ããã°
http://d.hatena.ne.jp/tzmtk/20090723/p1
ã
OAuth Core 1.0a
http://oauth.net/core/1.0a/
ã