PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/oauth.php

https://github.com/calvinfroedge/codeigniter-oauth
PHP | 215 lines | 121 code | 27 blank | 67 comment | 15 complexity | 49824f4a5bf449be6000e175aed36633 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. class OAuth
  3. {
  4. /*
  5. * The CodeIgniter Instance
  6. */
  7. public $ci;
  8. /*
  9. * The Type of Auth Being Used
  10. */
  11. public $type;
  12. /*
  13. * The Constructor Method
  14. */
  15. public function __construct()
  16. {
  17. $this->ci = &get_instance();
  18. $this->ci->load->config('oauth', TRUE);
  19. $this->ci->lang->load('messages');
  20. $this->ci->load->library('session');
  21. $this->ci->load->helper('url');
  22. }
  23. /**
  24. * Make a call. Uses other helper methods to make the request.
  25. *
  26. * @param string The login method to use
  27. * @param array $params[0] is the login method, $params[1] are the params for the request
  28. * @return object Should return a success or failure, along with a response.
  29. */
  30. public function __call($method, $params)
  31. {
  32. $loaded = $this->_load_module($params[0], $method);
  33. if($loaded)
  34. {
  35. return (isset($params[1]))
  36. ? $this->type->$method($params[1])
  37. : $this->type->$method();
  38. }
  39. else
  40. {
  41. return $this->response('failure', array(
  42. 'error' => $this->ci->lang->line('error_request_denied')
  43. )
  44. );
  45. }
  46. }
  47. /**
  48. * Try to load an authentication module
  49. *
  50. * @param string The authentication module to load
  51. * @return mixed Will return bool if file is not found. Will return file as object if found.
  52. */
  53. private function _load_module($module, $method)
  54. {
  55. $module_location = dirname(__FILE__).'/providers/'.$module.'.php';
  56. if (!is_file($module_location))
  57. {
  58. return FALSE;
  59. }
  60. if(!class_exists($module))
  61. {
  62. ob_start();
  63. include $module_location;
  64. ob_get_clean();
  65. $this->type = new $module($this);
  66. }
  67. if(method_exists($this->type, $method))
  68. {
  69. return TRUE;
  70. }
  71. }
  72. /**
  73. * Build the request to the Oauth Provider
  74. *
  75. * @param array Params to set
  76. * @param string The method to use (such as POST)
  77. * @param string The endpoint for the reuest
  78. * @param string The secret key
  79. * @return string Response from the _http call
  80. */
  81. public function make_request($params, $method, $endpoint, $secret)
  82. {
  83. // BUILD SIGNATURE
  84. // encode params keys, values, join and then sort.
  85. $keys = $this->_urlencode_rfc3986(array_keys($params));
  86. $values = $this->_urlencode_rfc3986(array_values($params));
  87. $params = array_combine($keys, $values);
  88. uksort($params, 'strcmp');
  89. // convert params to string
  90. foreach ($params as $k => $v) {$pairs[] = $this->_urlencode_rfc3986($k).'='.$this->_urlencode_rfc3986($v);}
  91. $concatenatedParams = implode('&', $pairs);
  92. // form base string (first key)
  93. $baseString= "$method&".$this->_urlencode_rfc3986($endpoint)."&".$this->_urlencode_rfc3986($concatenatedParams);
  94. // form secret (second key)
  95. $secret = $this->_urlencode_rfc3986($secret)."&";
  96. // make signature and append to params
  97. $params['oauth_signature'] = $this->_urlencode_rfc3986(base64_encode(hash_hmac('sha1', $baseString, $secret, TRUE)));
  98. // BUILD URL
  99. // Resort
  100. uksort($params, 'strcmp');
  101. // convert params to string
  102. foreach ($params as $k => $v) {$urlPairs[] = $k."=".$v;}
  103. $concatenatedUrlParams = implode('&', $urlPairs);
  104. // form url
  105. $url = $endpoint."?".$concatenatedUrlParams;
  106. // Send to cURL
  107. return $this->_http($url);
  108. }
  109. /**
  110. * Make the Curl Call to the Oauth Proivder
  111. *
  112. * @param string The URL to make the call to
  113. * @param array Array of post fields
  114. * @return mixed Could vary per provider
  115. */
  116. private function _http($url, $post_data = null)
  117. {
  118. $ch = curl_init();
  119. curl_setopt($ch, CURLOPT_URL, $url);
  120. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  121. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  122. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  123. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  124. if(isset($post_data))
  125. {
  126. curl_setopt($ch, CURLOPT_POST, 1);
  127. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  128. }
  129. $response = curl_exec($ch);
  130. $this->http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  131. $this->last_api_call = $url;
  132. return $response;
  133. }
  134. /**
  135. * URL Encode the request
  136. *
  137. * @param mixed The input array / string
  138. * @return mixed
  139. */
  140. private function _urlencode_rfc3986($input)
  141. {
  142. if (is_array($input)) {
  143. return array_map(array('Oauth', '_urlencode_rfc3986'), $input);
  144. }
  145. else if (is_scalar($input)) {
  146. return str_replace('+',' ',str_replace('%7E', '~', rawurlencode($input)));
  147. }
  148. else{
  149. return '';
  150. }
  151. }
  152. /**
  153. * Parses an XML response and creates an object using SimpleXML
  154. *
  155. * @param string raw xml string
  156. * @return object response object
  157. */
  158. public function parse_xml($xml_str)
  159. {
  160. $xml_str = trim($xml_str);
  161. $xml_str = preg_replace('/xmlns="(.+?)"/', '', $xml_str);
  162. if($xml_str[0] != '<')
  163. {
  164. $xml_str = explode('<', $xml_str);
  165. unset($xml_str[0]);
  166. $xml_str = '<'.implode('<', $xml_str);
  167. }
  168. $xml = new SimpleXMLElement($xml_str);
  169. return $xml;
  170. }
  171. /**
  172. * Normalize the response
  173. *
  174. * @param string The reponse status (success or failure)
  175. * @return object The response status and details
  176. */
  177. public function response($status, $details)
  178. {
  179. $return = array(
  180. 'status' => $status
  181. );
  182. if(isset($details['token'])) $return['token'] = $details['token'];
  183. if(isset($details['token2'])) $return['token2'] = $details['token2'];
  184. if(isset($details['error'])) $return['error'] = $details['error'];
  185. if(isset($details['user'])) $return['user'] = $details['user'];
  186. return (object) $return;
  187. }
  188. }