ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Consumer.php
Go to the documentation of this file.
1 <?php
2 
3 require_once(dirname(dirname(__FILE__)) . '/libextinc/OAuth.php');
4 
12 {
13  private $consumer;
14  private $signer;
15 
16  public function __construct($key, $secret)
17  {
18  $this->consumer = new OAuthConsumer($key, $secret, null);
19  $this->signer = new OAuthSignatureMethod_HMAC_SHA1();
20  }
21 
22  // Used only to load the libextinc library early
23  public static function dummy() {}
24 
25  public static function getOAuthError($hrh)
26  {
27  foreach ($hrh as $h) {
28  if (preg_match('|OAuth-Error:\s([^;]*)|i', $h, $matches)) {
29  return $matches[1];
30  }
31  }
32  return null;
33  }
34 
35  public static function getContentType($hrh)
36  {
37  foreach ($hrh as $h) {
38  if (preg_match('|Content-Type:\s([^;]*)|i', $h, $matches)) {
39  return $matches[1];
40  }
41  }
42  return null;
43  }
44 
45  /*
46  * This static helper function wraps \SimpleSAML\Utils\HTTP::fetch
47  * and throws an exception with diagnostics messages if it appear
48  * to be failing on an OAuth endpoint.
49  *
50  * If the status code is not 200, an exception is thrown. If the content-type
51  * of the response if text/plain, the content of the response is included in
52  * the text of the Exception thrown.
53  */
54  public static function getHTTP($url, $context = '')
55  {
56  try {
58  } catch (\SimpleSAML_Error_Exception $e) {
59  $statuscode = 'unknown';
60  if (preg_match('/^HTTP.*\s([0-9]{3})/', $http_response_header[0], $matches)) {
61  $statuscode = $matches[1];
62  }
63 
64  $error = $context . ' [statuscode: ' . $statuscode . ']: ';
65  $oautherror = self::getOAuthError($http_response_header);
66 
67  if (!empty($oautherror)) {
68  $error .= $oautherror;
69  }
70 
71  throw new Exception($error . ':' . $url);
72  }
73  // Fall back to return response, if could not reckognize HTTP header. Should not happen.
74  return $response;
75  }
76 
77  public function getRequestToken($url, $parameters = null)
78  {
79  $req_req = OAuthRequest::from_consumer_and_token($this->consumer, null, "GET", $url, $parameters);
80  $req_req->sign_request($this->signer, $this->consumer, null);
81 
82  $response_req = self::getHTTP(
83  $req_req->to_url(),
84  'Contacting request_token endpoint on the OAuth Provider'
85  );
86 
87  parse_str($response_req, $responseParsed);
88 
89  if (array_key_exists('error', $responseParsed)) {
90  throw new Exception('Error getting request token: ' . $responseParsed['error']);
91  }
92 
93  $requestToken = $responseParsed['oauth_token'];
94  $requestTokenSecret = $responseParsed['oauth_token_secret'];
95 
96  return new OAuthToken($requestToken, $requestTokenSecret);
97  }
98 
99  public function getAuthorizeRequest($url, $requestToken, $redirect = true, $callback = null)
100  {
101  $params = array('oauth_token' => $requestToken->key);
102  if ($callback) {
103  $params['oauth_callback'] = $callback;
104  }
105  $authorizeURL = \SimpleSAML\Utils\HTTP::addURLParameters($url, $params);
106  if ($redirect) {
108  exit;
109  }
110  return $authorizeURL;
111  }
112 
113  public function getAccessToken($url, $requestToken, $parameters = null)
114  {
115  $acc_req = OAuthRequest::from_consumer_and_token($this->consumer, $requestToken, "GET", $url, $parameters);
116  $acc_req->sign_request($this->signer, $this->consumer, $requestToken);
117 
118  try {
119  $response_acc = \SimpleSAML\Utils\HTTP::fetch($acc_req->to_url());
120  } catch (\SimpleSAML_Error_Exception $e) {
121  throw new Exception('Error contacting request_token endpoint on the OAuth Provider');
122  }
123 
124  SimpleSAML\Logger::debug('oauth: Reponse to get access token: '. $response_acc);
125 
126  parse_str($response_acc, $accessResponseParsed);
127 
128  if (array_key_exists('error', $accessResponseParsed)) {
129  throw new Exception('Error getting request token: ' . $accessResponseParsed['error']);
130  }
131 
132  $accessToken = $accessResponseParsed['oauth_token'];
133  $accessTokenSecret = $accessResponseParsed['oauth_token_secret'];
134 
135  return new OAuthToken($accessToken, $accessTokenSecret);
136  }
137 
138  public function postRequest($url, $accessToken, $parameters)
139  {
140  $data_req = OAuthRequest::from_consumer_and_token($this->consumer, $accessToken, "POST", $url, $parameters);
141  $data_req->sign_request($this->signer, $this->consumer, $accessToken);
142  $postdata = $data_req->to_postdata();
143 
144  $opts = array(
145  'ssl' => array(
146  'verify_peer' => false,
147  'capture_peer_cert' => true,
148  'capture_peer_chain' => true
149  ),
150  'http' => array(
151  'method' => 'POST',
152  'content' => $postdata,
153  'header' => 'Content-Type: application/x-www-form-urlencoded',
154  ),
155  );
156 
157  try {
159  } catch (\SimpleSAML_Error_Exception $e) {
160  throw new SimpleSAML_Error_Exception('Failed to push definition file to ' . $url);
161  }
162  return $response;
163  }
164 
165  public function getUserInfo($url, $accessToken, $opts = null)
166  {
167  $data_req = OAuthRequest::from_consumer_and_token($this->consumer, $accessToken, "GET", $url, null);
168  $data_req->sign_request($this->signer, $this->consumer, $accessToken);
169 
170  $data = \SimpleSAML\Utils\HTTP::fetch($data_req->to_url(), $opts);
171 
172  return json_decode($data, true);
173  }
174 }
175 
__construct($key, $secret)
Definition: Consumer.php:16
$context
Definition: webdav.php:25
$h
OAuth PECL extension includes an OAuth Exception class, so we need to wrap the definition of this cla...
Definition: OAuth.php:42
static debug($string)
Definition: Logger.php:211
getRequestToken($url, $parameters=null)
Definition: Consumer.php:77
static redirectTrustedURL($url, $parameters=array())
This function redirects to the specified URL without performing any security checks.
Definition: HTTP.php:959
static getContentType($hrh)
Definition: Consumer.php:35
static getOAuthError($hrh)
Definition: Consumer.php:25
getAuthorizeRequest($url, $requestToken, $redirect=true, $callback=null)
Definition: Consumer.php:99
static getHTTP($url, $context='')
Definition: Consumer.php:54
static fetch($url, $context=array(), $getHeaders=false)
Helper function to retrieve a file or URL with proxy support, also supporting proxy basic authorizati...
Definition: HTTP.php:408
exit
Definition: backend.php:16
static from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters=null)
pretty much a helper function to set up the request
Definition: OAuth.php:346
getUserInfo($url, $accessToken, $opts=null)
Definition: Consumer.php:165
getAccessToken($url, $requestToken, $parameters=null)
Definition: Consumer.php:113
$url
The HMAC-SHA1 signature method uses the HMAC-SHA1 signature algorithm as defined in [RFC2104] where t...
Definition: OAuth.php:139
$response
postRequest($url, $accessToken, $parameters)
Definition: Consumer.php:138
$key
Definition: croninfo.php:18
$data
Definition: bench.php:6