1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137:
<?php
namespace Upwork\API\AuthTypes;
use Upwork\API\Debug as ApiDebug;
use Upwork\API\Config as ApiConfig;
use Upwork\API\Interfaces\Client as ApiClient;
use Upwork\API\Utils as ApiUtils;
use Upwork\API\ApiException as ApiException;
use Upwork\API\AuthTypes\AbstractOAuth as AbstractOAuth;
final class OAuth1 extends AbstractOAuth implements ApiClient
{
public function request($type, $url, $params = array())
{
ApiDebug::p('running request from ' . __CLASS__);
$authType = ($type == 'GET') ? OAUTH_AUTH_TYPE_URI : OAUTH_AUTH_TYPE_FORM;
$oauth = $this->_getOAuthInstance($authType);
$oauth->setToken(self::$_accessToken, self::$_accessSecret);
try {
$oauth->fetch(ApiUtils::getFullUrl($url, self::$_epoint), $params, $type);
} catch (\OAuthException $e) {
}
$data = $oauth->getLastResponse();
ApiDebug::p('got response from server', $data);
return $data;
}
public function setupRequestToken()
{
ApiDebug::p('query request token from server');
$oauth = $this->_getOAuthInstance(OAUTH_AUTH_TYPE_FORM);
$requestTokenInfo = $oauth->getRequestToken(
ApiUtils::getFullUrl(self::URL_RTOKEN, 'api')
);
ApiDebug::p('got request token info', $requestTokenInfo);
self::$_requestToken = $requestTokenInfo['oauth_token'];
self::$_requestSecret = $requestTokenInfo['oauth_token_secret'];
return $requestTokenInfo;
}
protected function _setupAccessToken($verifier)
{
ApiDebug::p('requesting access token');
$oauth = $this->_getOAuthInstance(OAUTH_AUTH_TYPE_FORM);
$oauth->setToken(self::$_requestToken, self::$_requestSecret);
$accessTokenInfo = $oauth->getAccessToken(
ApiUtils::getFullUrl(self::URL_ATOKEN, 'api'),
null,
$verifier
);
ApiDebug::p('got access token info', $accessTokenInfo);
self::$_accessToken = $accessTokenInfo['oauth_token'];
self::$_accessSecret = $accessTokenInfo['oauth_token_secret'];
return $accessTokenInfo;
}
protected function _getOAuthInstance($authType)
{
ApiDebug::p('get OAuth instance');
$oauth = new \OAuth(
self::$_apiKey,
self::$_secret,
self::$_sigMethod,
$authType
);
if (ApiConfig::get('debug')) {
$oauth->enableDebug();
}
if (!self::$_verifySsl) {
$oauth->disableSSLChecks();
}
return $oauth;
}
}