Files

215 lines
5.9 KiB
PHP
Executable File

<?php
namespace JR\NetSuite\Lib;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
class Oauth
{
const SIGNATURE_METHOD_HMACSHA256 = 'HMAC-SHA256';
const OAUTH_SCRIPT = 'jr_netsuite/apidetail/script';
const OAUTH_VERSION = '1.0';
const NONCE_STRING = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
const XML_PATH_OAUTH_URL = 'jr_netsuite/apidetail/api_url';
const XML_PATH_OAUTH_REALM = 'jr_netsuite/apidetail/oauth_realm';
const XML_PATH_OAUTH_CONSUMER_KEY = 'jr_netsuite/apidetail/oauth_consumer_key';
const XML_PATH_OAUTH_CONSUMER_SECRET = 'jr_netsuite/apidetail/oauth_consumer_secret';
const XML_PATH_OAUTH_TOKEN = 'jr_netsuite/apidetail/oauth_token';
const XML_PATH_OAUTH_TOKEN_SECRET = 'jr_netsuite/apidetail/oauth_token_secret';
/**
* @var StoreManagerInterface
*/
protected $storeManager;
/**
* @var ScopeConfigInterface
*/
protected $scopeConfig;
/**
* @var string
*/
protected $script;
/**
* @param StoreManagerInterface $storeManager
* @param ScopeConfigInterface $scopeConfig
**/
public function __construct(
StoreManagerInterface $storeManager,
ScopeConfigInterface $scopeConfig
) {
$this->scopeConfig = $scopeConfig;
$this->storeManager = $storeManager;
}
/**
* consumer key
* @return string
*/
public function getConsumerKey()
{
return $this->getConfigValue(self::XML_PATH_OAUTH_CONSUMER_KEY);
}
/**
* consumer Secret
* @return string
*/
public function getConsumerSecret()
{
return $this->getConfigValue(self::XML_PATH_OAUTH_CONSUMER_SECRET);
}
/**
* Token access key
* @return string
*/
public function getToken()
{
return $this->getConfigValue(self::XML_PATH_OAUTH_TOKEN);
}
/**
* Token Secret
* @return string
*/
public function getTokenSecret()
{
return $this->getConfigValue(self::XML_PATH_OAUTH_TOKEN_SECRET);
}
/**
* Token Secreat
* @return string
*/
public function getRealm()
{
return $this->getConfigValue(self::XML_PATH_OAUTH_REALM);
}
/**
* Set Token Script
* @return string
*/
public function setScript($script)
{
$this->script = $script;
}
/**
* Token script
* @return string
*/
public function getScript()
{
if ($this->script) {
return $this->script;
}
return $this->getConfigValue(self::OAUTH_SCRIPT);
}
/**
* Token Secreat
* @return string
*/
public function getApiUrl()
{
return $this->getConfigValue(self::XML_PATH_OAUTH_URL);
}
/**
* Calculate header for request
* @return string
*/
public function getOauthParams($method = 'POST')
{
$time = time();
$nonce = rawurlencode($this->generateNonceString(11));
$oauthparams = $this->getParamObject($nonce, $time);
$baseString = $this->createBaseString($method, $oauthparams);
$signature = $this->getSignature('sha256', $baseString);
return $this->buildAuthorizationHeader($nonce, $time, $signature);
}
/**
* Calculate signature for request
* @return string
*/
public function getSignature($algo, $baseString)
{
$key = rawurlencode($this->getConsumerSecret()) . '&' . rawurlencode($this->getTokenSecret());
return rawurlencode(base64_encode(hash_hmac($algo, $baseString, $key, true)));
}
/**
* Builds the Authorization header for a request
* @return string
*/
private function buildAuthorizationHeader($nonce, $time, $signature)
{
return 'OAuth realm="' . $this->getRealm() . '",oauth_consumer_key="' . $this->getConsumerKey() . '",oauth_token="' . $this->getToken() . '",oauth_signature_method="HMAC-SHA256",oauth_timestamp="' . $time . '",oauth_nonce="' . $nonce . '",oauth_version="1.0",oauth_signature="' . $signature . '"';
}
/**
* Creates the Signature Base String.
* The Signature Base String is a consistent reproducible concatenation of
* the request elements into a single string. The string is used as an
* input in hashing or signing algorithms.
* @return string Returns the base string
*/
private function createBaseString($method, $params)
{
$url = $this->getApiUrl();
$query = http_build_query($params, '', '&', PHP_QUERY_RFC3986);
return strtoupper($method)
. '&' . rawurlencode($url)
. '&' . rawurlencode($query);
}
/**
* generating nonce value
* @return string
*/
public function generateNonceString($length = 11)
{
$characters = self::NONCE_STRING;
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
/**
* generating param value
* @return array
*/
public function getParamObject($nonce, $time)
{
return [
'deploy' => 1,
"oauth_consumer_key" => $this->getConsumerKey(),
"oauth_nonce" => $nonce,
"oauth_signature_method" => self::SIGNATURE_METHOD_HMACSHA256,
"oauth_timestamp" => $time,
"oauth_token" => $this->getToken(),
"oauth_version" => self::OAUTH_VERSION,
'script' => $this->getScript()
];
}
/**
* get config value
* @return string
*/
public function getConfigValue($path)
{
$store = $this->storeManager->getStore()->getStoreId();
$config_data = $this->scopeConfig->getValue($path, ScopeInterface::SCOPE_STORE, $store);
return $config_data;
}
}