Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Static Public Member Functions
Zend_Oauth_Http_Utility Class Reference

Public Member Functions

 assembleParams ( $url, Zend_Oauth_Config_ConfigInterface $config, array $serviceProviderParams=null)
 
 toEncodedQueryString (array $params, $customParamsOnly=false)
 
 toAuthorizationHeader (array $params, $realm=null, $excludeCustomParams=true)
 
 sign (array $params, $signatureMethod, $consumerSecret, $tokenSecret=null, $method=null, $url=null)
 
 parseQueryString ($query)
 
 generateNonce ()
 
 generateTimestamp ()
 

Static Public Member Functions

static urlEncode ($value)
 

Detailed Description

Definition at line 34 of file Utility.php.

Member Function Documentation

◆ assembleParams()

assembleParams (   $url,
Zend_Oauth_Config_ConfigInterface  $config,
array  $serviceProviderParams = null 
)

Assemble all parameters for a generic OAuth request - i.e. no special params other than the defaults expected for any OAuth query.

Parameters
string$url
Zend_Oauth_Config_ConfigInterface$config
null | array$serviceProviderParams
Returns
array

Definition at line 45 of file Utility.php.

49  {
50  $params = array(
51  'oauth_consumer_key' => $config->getConsumerKey(),
52  'oauth_nonce' => $this->generateNonce(),
53  'oauth_signature_method' => $config->getSignatureMethod(),
54  'oauth_timestamp' => $this->generateTimestamp(),
55  'oauth_version' => $config->getVersion(),
56  );
57 
58  if ($config->getToken()->getToken() != null) {
59  $params['oauth_token'] = $config->getToken()->getToken();
60  }
61 
62 
63  if ($serviceProviderParams !== null) {
64  $params = array_merge($params, $serviceProviderParams);
65  }
66 
67  $params['oauth_signature'] = $this->sign(
68  $params,
69  $config->getSignatureMethod(),
70  $config->getConsumerSecret(),
71  $config->getToken()->getTokenSecret(),
72  $config->getRequestMethod(),
73  $url
74  );
75 
76  return $params;
77  }
$config
Definition: fraud_order.php:17
sign(array $params, $signatureMethod, $consumerSecret, $tokenSecret=null, $method=null, $url=null)
Definition: Utility.php:144
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18

◆ generateNonce()

generateNonce ( )

Generate nonce

Returns
string

Definition at line 190 of file Utility.php.

191  {
192  return md5(uniqid(rand(), true));
193  }

◆ generateTimestamp()

generateTimestamp ( )

Generate timestamp

Returns
int

Definition at line 200 of file Utility.php.

201  {
202  return time();
203  }

◆ parseQueryString()

parseQueryString (   $query)

Parse query string

Parameters
mixed$query
Returns
array

Definition at line 168 of file Utility.php.

169  {
170  $params = array();
171  if (empty($query)) {
172  return array();
173  }
174 
175  // Not remotely perfect but beats parse_str() which converts
176  // periods and uses urldecode, not rawurldecode.
177  $parts = explode('&', $query);
178  foreach ($parts as $pair) {
179  $kv = explode('=', $pair);
180  $params[rawurldecode($kv[0])] = rawurldecode($kv[1]);
181  }
182  return $params;
183  }
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18

◆ sign()

sign ( array  $params,
  $signatureMethod,
  $consumerSecret,
  $tokenSecret = null,
  $method = null,
  $url = null 
)

Sign request

Parameters
array$params
string$signatureMethod
string$consumerSecret
null | string$tokenSecret
null | string$method
null | string$url
Returns
string

Definition at line 144 of file Utility.php.

146  {
147  $className = '';
148  $hashAlgo = null;
149  $parts = explode('-', $signatureMethod);
150  if (count($parts) > 1) {
151  $className = 'Zend_Oauth_Signature_' . ucfirst(strtolower($parts[0]));
152  $hashAlgo = $parts[1];
153  } else {
154  $className = 'Zend_Oauth_Signature_' . ucfirst(strtolower($signatureMethod));
155  }
156 
157  #require_once str_replace('_', '/', $className) . '.php';
158  $signatureObject = new $className($consumerSecret, $tokenSecret, $hashAlgo);
159  return $signatureObject->sign($params, $method, $url);
160  }
$method
Definition: info.phtml:13
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31

◆ toAuthorizationHeader()

toAuthorizationHeader ( array  $params,
  $realm = null,
  $excludeCustomParams = true 
)

Cast to authorization header

Parameters
array$params
null | string$realm
bool$excludeCustomParams
Returns
void

Definition at line 114 of file Utility.php.

115  {
116  $headerValue = array(
117  'OAuth realm="' . $realm . '"',
118  );
119 
120  foreach ($params as $key => $value) {
121  if ($excludeCustomParams) {
122  if (!preg_match("/^oauth_/", $key)) {
123  continue;
124  }
125  }
126  $headerValue[] = self::urlEncode($key)
127  . '="'
128  . self::urlEncode($value) . '"';
129  }
130  return implode(",", $headerValue);
131  }
static urlEncode($value)
Definition: Utility.php:211
$value
Definition: gender.phtml:16
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18

◆ toEncodedQueryString()

toEncodedQueryString ( array  $params,
  $customParamsOnly = false 
)

Given both OAuth parameters and any custom parametere, generate an encoded query string. This method expects parameters to have been assembled and signed beforehand.

Parameters
array$params
bool$customParamsOnlyIgnores OAuth params e.g. for requests using OAuth Header
Returns
string

Definition at line 88 of file Utility.php.

89  {
90  if ($customParamsOnly) {
91  foreach ($params as $key=>$value) {
92  if (preg_match("/^oauth_/", $key)) {
93  unset($params[$key]);
94  }
95  }
96  }
97  $encodedParams = array();
98  foreach ($params as $key => $value) {
99  $encodedParams[] = self::urlEncode($key)
100  . '='
102  }
103  return implode('&', $encodedParams);
104  }
static urlEncode($value)
Definition: Utility.php:211
$value
Definition: gender.phtml:16
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18

◆ urlEncode()

static urlEncode (   $value)
static

urlencode a value

Parameters
string$value
Returns
string

Definition at line 211 of file Utility.php.

212  {
213  $encoded = rawurlencode($value);
214  $encoded = str_replace('%7E', '~', $encoded);
215  return $encoded;
216  }
$value
Definition: gender.phtml:16

The documentation for this class was generated from the following file: