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