[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/vendor/lusitanian/oauth/src/OAuth/OAuth1/Service/ -> AbstractService.php (source)

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


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1