public function testGenetateAuthZHeader() { // sample at http://tools.ietf.org/html/draft-hammer-oauth-v2-mac-token-02 $testauthzheader = OAuth2MacTokenUtil::genetateAuthZHeader('h480djs93hd8', '489dks293j39', 'hmac-sha-1', strtotime("Thu, 02 Dec 2010 21:39:45 GMT"), '264095:dj83hs9s', 'GET', 'http://example.com:80/resource/1?b=1&a=2'); $this->assertEquals('Authorization: MAC id="h480djs93hd8",nonce="264095:dj83hs9s",mac="SLDJd4mg43cjQfElUs3Qub4L6xE="', $testauthzheader); $testauthzheader = OAuth2MacTokenUtil::genetateAuthZHeader('jd93dh9dh39D', '8yfrufh348h', 'hmac-sha-1', strtotime("Thu, 02 Dec 2010 21:39:45 GMT"), '273156:di3hvdf8', 'POST', 'http://example.com:80/request', 'hello=world%21'); $this->assertEquals('Authorization: MAC id="jd93dh9dh39D",nonce="273156:di3hvdf8",bodyhash="k9kbtCIy0CkI3/FEfpS/oIDjk6k=",mac="W7bdMZbv9UWOTadASIQHagZyirA="', $testauthzheader); $authzheader_array = array(); $authzheader_array2 = array(); for ($i = 0; $i < 10000; $i++) { $authzheader_array[] = OAuth2MacTokenUtil::genetateAuthZHeader('h480djs93hd8', '489dks293j39', 'hmac-sha-1', strtotime("Thu, 02 Dec 2010 21:39:45 GMT"), '', 'GET', 'http://example.com:80/resource/1?b=1&a=2'); $authzheader_array2[] = OAuth2MacTokenUtil::genetateAuthZHeader('jd93dh9dh39D', '8yfrufh348h', 'hmac-sha-1', strtotime("Thu, 02 Dec 2010 21:39:45 GMT"), '', 'POST', 'http://example.com:80/request', 'hello=world%21'); } $this->assertEquals($authzheader_array, array_unique($authzheader_array)); $this->assertEquals($authzheader_array2, array_unique($authzheader_array2)); }
public function sendRequest($method, $url, $entitybody = null, $headers = array()) { $headers[] = OAuth2MacTokenUtil::genetateAuthZHeader($this->_token, $this->_secret, $this->_algorithm, $this->_timestamp, $this->_nonce, $method, $url, $entitybody); $this->_http_info = array(); $this->_http_code = null; $this->_http_body = null; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_USERAGENT, $this->_useragent); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->_connecttimeout); curl_setopt($ch, CURLOPT_TIMEOUT, $this->_timeout); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->_ssl_verifypeer); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $this->_ssl_verifyhost); curl_setopt($ch, CURLOPT_HEADER, $this->_responseheader); curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE); switch ($method) { case 'POST': curl_setopt($ch, CURLOPT_POST, TRUE); if (!empty($entitybody)) { curl_setopt($ch, CURLOPT_POSTFIELDS, $entitybody); } break; case 'DELETE': curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); if (!empty($entitybody)) { $url = "{$url}?{$entitybody}"; } } $this->_http_body = curl_exec($ch); $this->_http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); $this->_http_info = array_merge($this->_http_info, curl_getinfo($ch)); curl_close($ch); return ($this->_http_code = 200) ? true : false; }
=== Input Parameters === EOF; print "\n"; print OAuth2MacTokenUtil::genetateAuthZHeader($key_id, $key, $algorithm, $iss, $nonce, $method, $url, $entitybody, $ext); print "\n\n"; $key_id = "samplekeyid"; $key = "samplekey"; $algorithm = "hmac-sha-1"; $iss = time() - 1; // dummy $nonce = ""; $method = "GET"; $url = "http://example.com:80/request?foo=var"; $entitybody = ""; $ext = "a,b,c"; print <<<EOF === Input Parameters === key_id = "{$key_id}"; key = "{$key}"; algorithm = "{$algorithm}"; iss = {$iss}; // dummy nonce = "{$nonce}"; method = "{$method}"; url = "{$url}"; entitybody = "{$entitybody}"; ext = "{$ext}"; === Input Parameters === EOF; print "\n"; print OAuth2MacTokenUtil::genetateAuthZHeader($key_id, $key, $algorithm, $iss, $nonce, $method, $url, $entitybody, $ext); print "\n\n";