OAuthOAuth1ServiceAbstractService

The Gravity Forms Salesforce Add-On OAuth OAuth1 Service AbstractService class.

Defined (1)

The class is defined in the following location(s).

/lib/PHPoAuthLib/src/OAuth/OAuth1/Service/AbstractService.php  
  1. abstract class AbstractService extends BaseAbstractService implements ServiceInterface 
  2. /** @const OAUTH_VERSION */ 
  3. const OAUTH_VERSION = 1; 
  4.  
  5. /** @var SignatureInterface */ 
  6. protected $signature; 
  7.  
  8. /** @var UriInterface|null */ 
  9. protected $baseApiUri; 
  10.  
  11. /** 
  12. * {@inheritDoc} 
  13. */ 
  14. public function __construct( 
  15. CredentialsInterface $credentials,  
  16. ClientInterface $httpClient,  
  17. TokenStorageInterface $storage,  
  18. SignatureInterface $signature,  
  19. UriInterface $baseApiUri = null 
  20. ) { 
  21. parent::__construct($credentials, $httpClient, $storage); 
  22.  
  23. $this->signature = $signature; 
  24. $this->baseApiUri = $baseApiUri; 
  25.  
  26. $this->signature->setHashingAlgorithm($this->getSignatureMethod()); 
  27.  
  28. /** 
  29. * {@inheritDoc} 
  30. */ 
  31. public function requestRequestToken() 
  32. $authorizationHeader = array('Authorization' => $this->buildAuthorizationHeaderForTokenRequest()); 
  33. $headers = array_merge($authorizationHeader, $this->getExtraOAuthHeaders()); 
  34.  
  35. $responseBody = $this->httpClient->retrieveResponse($this->getRequestTokenEndpoint(), array(), $headers); 
  36.  
  37. $token = $this->parseRequestTokenResponse($responseBody); 
  38. $this->storage->storeAccessToken($this->service(), $token); 
  39.  
  40. return $token; 
  41.  
  42. /** 
  43. * {@inheritdoc} 
  44. */ 
  45. public function getAuthorizationUri(array $additionalParameters = array()) 
  46. // Build the url 
  47. $url = clone $this->getAuthorizationEndpoint(); 
  48. foreach ($additionalParameters as $key => $val) { 
  49. $url->addToQuery($key, $val); 
  50.  
  51. return $url; 
  52.  
  53. /** 
  54. * {@inheritDoc} 
  55. */ 
  56. public function requestAccessToken($token, $verifier, $tokenSecret = null) 
  57. if (is_null($tokenSecret)) { 
  58. $storedRequestToken = $this->storage->retrieveAccessToken($this->service()); 
  59. $tokenSecret = $storedRequestToken->getRequestTokenSecret(); 
  60. $this->signature->setTokenSecret($tokenSecret); 
  61.  
  62. $extraAuthenticationHeaders = array( 
  63. 'oauth_token' => $token,  
  64. ); 
  65.  
  66. $bodyParams = array( 
  67. 'oauth_verifier' => $verifier,  
  68. ); 
  69.  
  70. $authorizationHeader = array( 
  71. 'Authorization' => $this->buildAuthorizationHeaderForAPIRequest( 
  72. 'POST',  
  73. $this->getAccessTokenEndpoint(),  
  74. $this->storage->retrieveAccessToken($this->service()),  
  75. $bodyParams 
  76. ); 
  77.  
  78. $headers = array_merge($authorizationHeader, $this->getExtraOAuthHeaders()); 
  79.  
  80. $responseBody = $this->httpClient->retrieveResponse($this->getAccessTokenEndpoint(), $bodyParams, $headers); 
  81.  
  82. $token = $this->parseAccessTokenResponse($responseBody); 
  83. $this->storage->storeAccessToken($this->service(), $token); 
  84.  
  85. return $token; 
  86.  
  87. /** 
  88. * Sends an authenticated API request to the path provided. 
  89. * If the path provided is not an absolute URI, the base API Uri (must be passed into constructor) will be used. 
  90. * @param string|UriInterface $path 
  91. * @param string $method HTTP method 
  92. * @param array $body Request body if applicable (key/value pairs) 
  93. * @param array $extraHeaders Extra headers if applicable. 
  94. * These will override service-specific any defaults. 
  95. * @return string 
  96. */ 
  97. public function request($path, $method = 'GET', $body = null, array $extraHeaders = array()) 
  98. $uri = $this->determineRequestUriFromPath($path, $this->baseApiUri); 
  99.  
  100. /** @var $token StdOAuth1Token */ 
  101. $token = $this->storage->retrieveAccessToken($this->service()); 
  102. $extraHeaders = array_merge($this->getExtraApiHeaders(), $extraHeaders); 
  103. $authorizationHeader = array( 
  104. 'Authorization' => $this->buildAuthorizationHeaderForAPIRequest($method, $uri, $token, $body) 
  105. ); 
  106. $headers = array_merge($authorizationHeader, $extraHeaders); 
  107.  
  108. return $this->httpClient->retrieveResponse($uri, $body, $headers, $method); 
  109.  
  110. /** 
  111. * Return any additional headers always needed for this service implementation's OAuth calls. 
  112. * @return array 
  113. */ 
  114. protected function getExtraOAuthHeaders() 
  115. return array(); 
  116.  
  117. /** 
  118. * Return any additional headers always needed for this service implementation's API calls. 
  119. * @return array 
  120. */ 
  121. protected function getExtraApiHeaders() 
  122. return array(); 
  123.  
  124. /** 
  125. * Builds the authorization header for getting an access or request token. 
  126. * @param array $extraParameters 
  127. * @return string 
  128. */ 
  129. protected function buildAuthorizationHeaderForTokenRequest(array $extraParameters = array()) 
  130. $parameters = $this->getBasicAuthorizationHeaderInfo(); 
  131. $parameters = array_merge($parameters, $extraParameters); 
  132. $parameters['oauth_signature'] = $this->signature->getSignature( 
  133. $this->getRequestTokenEndpoint(),  
  134. $parameters,  
  135. 'POST' 
  136. ); 
  137.  
  138. $authorizationHeader = 'OAuth '; 
  139. $delimiter = ''; 
  140. foreach ($parameters as $key => $value) { 
  141. $authorizationHeader .= $delimiter . rawurlencode($key) . '="' . rawurlencode($value) . '"'; 
  142.  
  143. $delimiter = ', '; 
  144.  
  145. return $authorizationHeader; 
  146.  
  147. /** 
  148. * Builds the authorization header for an authenticated API request 
  149. * @param string $method 
  150. * @param UriInterface $uri The uri the request is headed 
  151. * @param TokenInterface $token 
  152. * @param array $bodyParams Request body if applicable (key/value pairs) 
  153. * @return string 
  154. */ 
  155. protected function buildAuthorizationHeaderForAPIRequest( 
  156. $method,  
  157. UriInterface $uri,  
  158. TokenInterface $token,  
  159. $bodyParams = null 
  160. ) { 
  161. $this->signature->setTokenSecret($token->getAccessTokenSecret()); 
  162. $parameters = $this->getBasicAuthorizationHeaderInfo(); 
  163. if (isset($parameters['oauth_callback'])) { 
  164. unset($parameters['oauth_callback']); 
  165.  
  166. $parameters = array_merge($parameters, array('oauth_token' => $token->getAccessToken())); 
  167.  
  168. $mergedParams = (is_array($bodyParams)) ? array_merge($parameters, $bodyParams) : $parameters; 
  169.  
  170. $parameters['oauth_signature'] = $this->signature->getSignature($uri, $mergedParams, $method); 
  171.  
  172. $authorizationHeader = 'OAuth '; 
  173. $delimiter = ''; 
  174.  
  175. foreach ($parameters as $key => $value) { 
  176. $authorizationHeader .= $delimiter . rawurlencode($key) . '="' . rawurlencode($value) . '"'; 
  177. $delimiter = ', '; 
  178.  
  179. return $authorizationHeader; 
  180.  
  181. /** 
  182. * Builds the authorization header array. 
  183. * @return array 
  184. */ 
  185. protected function getBasicAuthorizationHeaderInfo() 
  186. $dateTime = new \DateTime(); 
  187. $headerParameters = array( 
  188. 'oauth_callback' => $this->credentials->getCallbackUrl(),  
  189. 'oauth_consumer_key' => $this->credentials->getConsumerId(),  
  190. 'oauth_nonce' => $this->generateNonce(),  
  191. 'oauth_signature_method' => $this->getSignatureMethod(),  
  192. 'oauth_timestamp' => $dateTime->format('U'),  
  193. 'oauth_version' => $this->getVersion(),  
  194. ); 
  195.  
  196. return $headerParameters; 
  197.  
  198. /** 
  199. * Pseudo random string generator used to build a unique string to sign each request 
  200. * @param int $length 
  201. * @return string 
  202. */ 
  203. protected function generateNonce($length = 32) 
  204. $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; 
  205.  
  206. $nonce = ''; 
  207. $maxRand = strlen($characters)-1; 
  208. for ($i = 0; $i < $length; $i++) { 
  209. $nonce.= $characters[rand(0, $maxRand)]; 
  210.  
  211. return $nonce; 
  212.  
  213. /** 
  214. * @return string 
  215. */ 
  216. protected function getSignatureMethod() 
  217. return 'HMAC-SHA1'; 
  218.  
  219. /** 
  220. * This returns the version used in the authorization header of the requests 
  221. * @return string 
  222. */ 
  223. protected function getVersion() 
  224. return '1.0'; 
  225.  
  226. /** 
  227. * Parses the request token response and returns a TokenInterface. 
  228. * This is only needed to verify the `oauth_callback_confirmed` parameter. The actual 
  229. * parsing logic is contained in the access token parser. 
  230. * @abstract 
  231. * @param string $responseBody 
  232. * @return TokenInterface 
  233. * @throws TokenResponseException 
  234. */ 
  235. abstract protected function parseRequestTokenResponse($responseBody); 
  236.  
  237. /** 
  238. * Parses the access token response and returns a TokenInterface. 
  239. * @abstract 
  240. * @param string $responseBody 
  241. * @return TokenInterface 
  242. * @throws TokenResponseException 
  243. */ 
  244. abstract protected function parseAccessTokenResponse($responseBody);