Open Journal Systems  3.3.0
OauthPlugin.php
1 <?php
2 
4 
10 use Guzzle\Http\Url;
12 
18 {
22  const REQUEST_METHOD_HEADER = 'header';
23  const REQUEST_METHOD_QUERY = 'query';
24 
26  protected $config;
27 
44  public function __construct($config)
45  {
46  $this->config = Collection::fromConfig($config, array(
47  'version' => '1.0',
48  'request_method' => self::REQUEST_METHOD_HEADER,
49  'consumer_key' => 'anonymous',
50  'consumer_secret' => 'anonymous',
51  'signature_method' => 'HMAC-SHA1',
52  'signature_callback' => function($stringToSign, $key) {
53  return hash_hmac('sha1', $stringToSign, $key, true);
54  }
55  ), array(
56  'signature_method', 'signature_callback', 'version',
57  'consumer_key', 'consumer_secret'
58  ));
59  }
60 
61  public static function getSubscribedEvents()
62  {
63  return array(
64  'request.before_send' => array('onRequestBeforeSend', -1000)
65  );
66  }
67 
75  public function onRequestBeforeSend(Event $event)
76  {
77  $timestamp = $this->getTimestamp($event);
78  $request = $event['request'];
79  $nonce = $this->generateNonce($request);
80  $authorizationParams = $this->getOauthParams($timestamp, $nonce);
81  $authorizationParams['oauth_signature'] = $this->getSignature($request, $timestamp, $nonce);
82 
83  switch ($this->config['request_method']) {
85  $request->setHeader(
86  'Authorization',
87  $this->buildAuthorizationHeader($authorizationParams)
88  );
89  break;
91  foreach ($authorizationParams as $key => $value) {
92  $request->getQuery()->set($key, $value);
93  }
94  break;
95  default:
96  throw new \InvalidArgumentException(sprintf(
97  'Invalid consumer method "%s"',
98  $this->config['request_method']
99  ));
100  }
101 
102  return $authorizationParams;
103  }
104 
112  private function buildAuthorizationHeader($authorizationParams)
113  {
114  $authorizationString = 'OAuth ';
115  foreach ($authorizationParams as $key => $val) {
116  if ($val) {
117  $authorizationString .= $key . '="' . urlencode($val) . '", ';
118  }
119  }
120 
121  return substr($authorizationString, 0, -2);
122  }
123 
133  public function getSignature(RequestInterface $request, $timestamp, $nonce)
134  {
135  $string = $this->getStringToSign($request, $timestamp, $nonce);
136  $key = urlencode($this->config['consumer_secret']) . '&' . urlencode($this->config['token_secret']);
137 
138  return base64_encode(call_user_func($this->config['signature_callback'], $string, $key));
139  }
140 
150  public function getStringToSign(RequestInterface $request, $timestamp, $nonce)
151  {
152  $params = $this->getParamsToSign($request, $timestamp, $nonce);
153 
154  // Convert booleans to strings.
155  $params = $this->prepareParameters($params);
156 
157  // Build signing string from combined params
158  $parameterString = clone $request->getQuery();
159  $parameterString->replace($params);
160 
161  $url = Url::factory($request->getUrl())->setQuery('')->setFragment(null);
162 
163  return strtoupper($request->getMethod()) . '&'
164  . rawurlencode($url) . '&'
165  . rawurlencode((string) $parameterString);
166  }
167 
175  protected function getOauthParams($timestamp, $nonce)
176  {
177  $params = new Collection(array(
178  'oauth_consumer_key' => $this->config['consumer_key'],
179  'oauth_nonce' => $nonce,
180  'oauth_signature_method' => $this->config['signature_method'],
181  'oauth_timestamp' => $timestamp,
182  ));
183 
184  // Optional parameters should not be set if they have not been set in the config as
185  // the parameter may be considered invalid by the Oauth service.
186  $optionalParams = array(
187  'callback' => 'oauth_callback',
188  'token' => 'oauth_token',
189  'verifier' => 'oauth_verifier',
190  'version' => 'oauth_version'
191  );
192 
193  foreach ($optionalParams as $optionName => $oauthName) {
194  if (isset($this->config[$optionName]) == true) {
195  $params[$oauthName] = $this->config[$optionName];
196  }
197  }
198 
199  return $params;
200  }
201 
214  public function getParamsToSign(RequestInterface $request, $timestamp, $nonce)
215  {
216  $params = $this->getOauthParams($timestamp, $nonce);
217 
218  // Add query string parameters
219  $params->merge($request->getQuery());
220 
221  // Add POST fields to signing string if required
222  if ($this->shouldPostFieldsBeSigned($request))
223  {
224  $params->merge($request->getPostFields());
225  }
226 
227  // Sort params
228  $params = $params->toArray();
229  uksort($params, 'strcmp');
230 
231  return $params;
232  }
233 
243  public function shouldPostFieldsBeSigned($request)
244  {
245  if (!$this->config->get('disable_post_params') &&
246  $request instanceof EntityEnclosingRequestInterface &&
247  false !== strpos($request->getHeader('Content-Type'), 'application/x-www-form-urlencoded'))
248  {
249  return true;
250  }
251 
252  return false;
253  }
254 
263  public function generateNonce(RequestInterface $request)
264  {
265  return sha1(uniqid('', true) . $request->getUrl());
266  }
267 
275  public function getTimestamp(Event $event)
276  {
277  return $event['timestamp'] ?: time();
278  }
279 
287  protected function prepareParameters($data)
288  {
289  ksort($data);
290  foreach ($data as $key => &$value) {
291  switch (gettype($value)) {
292  case 'NULL':
293  unset($data[$key]);
294  break;
295  case 'array':
296  $data[$key] = self::prepareParameters($value);
297  break;
298  case 'boolean':
299  $data[$key] = $value ? 'true' : 'false';
300  break;
301  }
302  }
303 
304  return $data;
305  }
306 }
Guzzle\Plugin\Oauth\OauthPlugin\getParamsToSign
getParamsToSign(RequestInterface $request, $timestamp, $nonce)
Definition: OauthPlugin.php:217
Guzzle\Http\Message\RequestInterface
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/RequestInterface.php:16
Guzzle\Plugin\Oauth\OauthPlugin\onRequestBeforeSend
onRequestBeforeSend(Event $event)
Definition: OauthPlugin.php:78
Guzzle\Http\Message\RequestInterface\getUrl
getUrl($asObject=false)
Guzzle\Plugin\Oauth\OauthPlugin\REQUEST_METHOD_QUERY
const REQUEST_METHOD_QUERY
Definition: OauthPlugin.php:23
Guzzle\Plugin\Oauth\OauthPlugin\generateNonce
generateNonce(RequestInterface $request)
Definition: OauthPlugin.php:266
Guzzle\Plugin\Oauth\OauthPlugin\getTimestamp
getTimestamp(Event $event)
Definition: OauthPlugin.php:278
Symfony\Component\EventDispatcher\EventSubscriberInterface
Definition: lib/vendor/symfony/event-dispatcher/EventSubscriberInterface.php:25
Guzzle\Http\QueryString
Definition: QueryString.php:14
Guzzle\Http\Message\EntityEnclosingRequestInterface
Definition: EntityEnclosingRequestInterface.php:12
Guzzle\Plugin\Oauth\OauthPlugin\$config
$config
Definition: OauthPlugin.php:29
Guzzle\Http\Url
Definition: Url.php:10
Guzzle\Plugin\Oauth\OauthPlugin\shouldPostFieldsBeSigned
shouldPostFieldsBeSigned($request)
Definition: OauthPlugin.php:246
Guzzle\Plugin\Oauth\OauthPlugin\getSubscribedEvents
static getSubscribedEvents()
Definition: OauthPlugin.php:64
Guzzle\Plugin\Oauth\OauthPlugin\getOauthParams
getOauthParams($timestamp, $nonce)
Definition: OauthPlugin.php:178
Guzzle\Common\Event
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Common/Event.php:10
Guzzle\Http\Message\RequestInterface\getMethod
getMethod()
Guzzle\Http\Message\RequestInterface\getQuery
getQuery()
Guzzle\Plugin\Oauth
Definition: OauthPlugin.php:3
Guzzle\Common\Collection\fromConfig
static fromConfig(array $config=array(), array $defaults=array(), array $required=array())
Definition: paymethod/paypal/lib/vendor/guzzle/guzzle/src/Guzzle/Common/Collection.php:37
Guzzle\Plugin\Oauth\OauthPlugin\prepareParameters
prepareParameters($data)
Definition: OauthPlugin.php:290
Guzzle\Plugin\Oauth\OauthPlugin\getStringToSign
getStringToSign(RequestInterface $request, $timestamp, $nonce)
Definition: OauthPlugin.php:153
Guzzle\Plugin\Oauth\OauthPlugin\REQUEST_METHOD_HEADER
const REQUEST_METHOD_HEADER
Definition: OauthPlugin.php:22
Guzzle\Plugin\Oauth\OauthPlugin
Definition: OauthPlugin.php:17
Guzzle\Plugin\Oauth\OauthPlugin\__construct
__construct($config)
Definition: OauthPlugin.php:47
Guzzle\Common\Collection
Definition: paymethod/paypal/lib/vendor/guzzle/guzzle/src/Guzzle/Common/Collection.php:11
Guzzle\Plugin\Oauth\OauthPlugin\getSignature
getSignature(RequestInterface $request, $timestamp, $nonce)
Definition: OauthPlugin.php:136
Guzzle\Http\Url\factory
static factory($url)
Definition: Url.php:34