예제 #1
0
 /**
  * {@inheritDoc}
  *
  * @throws InvalidArgumentException
  */
 public function preSend(RequestInterface $request)
 {
     // Skip by default
     if (null === $this->method) {
         return;
     }
     switch ($this->method) {
         case Client::AUTH_HTTP_TOKEN:
             $request->addHeader('PRIVATE-TOKEN: ' . $this->token);
             if (!is_null($this->sudo)) {
                 $request->addHeader('SUDO: ' . $this->sudo);
             }
             break;
         case Client::AUTH_URL_TOKEN:
             $url = $request->getUrl();
             $query = array('private_token' => $this->token);
             if (!is_null($this->sudo)) {
                 $query['sudo'] = $this->sudo;
             }
             $url .= (false === strpos($url, '?') ? '?' : '&') . utf8_encode(http_build_query($query, '', '&'));
             $request->fromUrl(new Url($url));
             break;
         case Client::AUTH_OAUTH_TOKEN:
             $request->addHeader('Authorization: Bearer ' . $this->token);
             if (!is_null($this->sudo)) {
                 $request->addHeader('SUDO: ' . $this->sudo);
             }
             break;
     }
 }
예제 #2
0
 /**
  * {@inheritDoc}
  *
  * @throws InvalidArgumentException
  */
 public function preSend(RequestInterface $request)
 {
     if (null === $this->method) {
         return;
     }
     switch ($this->method) {
         case Client::AUTH_HTTP_PASSWORD:
             if (!isset($this->options['login'], $this->options['password'])) {
                 throw new InvalidArgumentException('You need to set username with password!');
             }
             $request->addHeader('Authorization: Basic ' . base64_encode($this->options['login'] . ':' . $this->options['password']));
             break;
         case Client::AUTH_HTTP_TOKEN:
             if (!isset($this->options['token'])) {
                 throw new InvalidArgumentException('You need to set OAuth token!');
             }
             $request->addHeader('Authorization: token ' . $this->options['token']);
             break;
         case Client::AUTH_URL_CLIENT_ID:
             if (!isset($this->options['login'], $this->options['password'])) {
                 throw new InvalidArgumentException('You need to set client_id and client_secret!');
             }
             if ('GET' === $request->getMethod()) {
                 $url = $request->getUrl();
                 $parameters = array('client_id' => $this->options['login'], 'client_secret' => $this->options['password']);
                 $url .= (false === strpos($url, '?') ? '?' : '&') . utf8_encode(http_build_query($parameters, '', '&'));
                 $request->fromUrl(new Url($url));
             }
             break;
         case Client::AUTH_URL_TOKEN:
             if (!isset($this->options['token'])) {
                 throw new InvalidArgumentException('You need to set OAuth token!');
             }
             if ('GET' === $request->getMethod()) {
                 $url = $request->getUrl();
                 $parameters = array('access_token' => $this->options['token']);
                 $url .= (false === strpos($url, '?') ? '?' : '&') . utf8_encode(http_build_query($parameters, '', '&'));
                 $request->fromUrl(new Url($url));
             }
             break;
     }
 }
예제 #3
0
 /**
  * PreSend event
  * @param RequestInterface $request Request object
  */
 public function preSend(RequestInterface $request)
 {
     $url = $request->getUrl();
     $pos = strpos($url, '?');
     if ($pos !== false) {
         $url .= '&' . utf8_encode(http_build_query($this->params, '', '&'));
     } else {
         $url .= '?' . utf8_encode(http_build_query($this->params, '', '&'));
     }
     $request->fromUrl(new Url($url));
 }
 /**
  * @param \Buzz\Message\RequestInterface $oRequest
  */
 public function preSend(\Buzz\Message\RequestInterface $oRequest)
 {
     // Skip by default
     if (!$this->hasMethod()) {
         return;
     }
     switch ($this->getMethod()) {
         case \GitlabCI\Client::AUTH_HTTP_TOKEN:
             $oRequest->addHeader('PRIVATE-TOKEN: ' . $this->getToken());
             $sUrl = $oRequest->getUrl();
             $aQuery = array('url' => $this->getGitlabUrl());
             $sUrl .= (false === strpos($sUrl, '?') ? '?' : '&') . utf8_encode(http_build_query($aQuery, '', '&'));
             $oRequest->fromUrl(new \Buzz\Util\Url($sUrl));
             break;
         case \GitlabCI\Client::AUTH_URL_TOKEN:
             $aQuery = array('private_token' => $this->getToken(), 'url' => $this->getGitlabUrl());
             $sUrl = $oRequest->getUrl();
             $sUrl .= (false === strpos($sUrl, '?') ? '?' : '&') . utf8_encode(http_build_query($aQuery, '', '&'));
             $oRequest->fromUrl(new \Buzz\Util\Url($sUrl));
             break;
     }
 }
예제 #5
0
 /**
  * {@inheritDoc}
  */
 public function preSend(RequestInterface $request)
 {
     switch ($this->method) {
         case Client::AUTH_HTTP_PASSWORD:
             $request->addHeader('Authorization: Basic ' . base64_encode($this->options['login'] . ':' . $this->options['password']));
             break;
         case Client::AUTH_HTTP_TOKEN:
             $request->addHeader('Authorization: token ' . $this->options['token']);
             break;
         case Client::AUTH_URL_TOKEN:
         default:
             $url = $request->getUrl();
             if ('GET' === $request->getMethod()) {
                 $parameters = array('access_token' => $this->options['token']);
                 $url .= '?' . utf8_encode(http_build_query($parameters, '', '&'));
             }
             $request->fromUrl(new Url($url));
             break;
     }
 }
예제 #6
0
 /**
  * {@inheritDoc}
  */
 public function preSend(RequestInterface $request)
 {
     $url = $request->getUrl();
     $url .= (false === strpos($url, '?') ? '?' : '&') . utf8_encode(http_build_query($this->options, '', '&'));
     $request->fromUrl(new Url($url));
 }
예제 #7
0
 /**
  * @param RequestInterface $request
  * @param array            $parameters
  *
  * @return Url
  */
 private function setRequestUrl(RequestInterface $request, array $parameters = array())
 {
     $url = $request->getUrl();
     $url .= (false === strpos($url, '?') ? '?' : '&') . utf8_encode(http_build_query($parameters, '', '&'));
     $request->fromUrl(new Url($url));
 }
예제 #8
0
 /**
  * @param AntiMattr\OAuth\OAuth         $oauth
  * @param Buzz\Message\RequestInterface $request
  *
  * @throws RuntimeException
  */
 protected function attachOAuthSignature(OAuth $oauth, RequestInterface $request)
 {
     $method = $request->getMethod();
     $url = new Url($request->getUrl());
     $parameters = $url->query->getData();
     if ($request instanceof FormRequestInterface) {
         $fields = $request->getFields();
         $parameters = array_merge($fields, $parameters);
     }
     // Parameters are sorted by name, using lexicographical byte value ordering.
     // Ref: Spec: 9.1.1 (1)
     uksort($parameters, 'strcmp');
     // Validate required parameters
     foreach (array('oauth_consumer_key', 'oauth_timestamp', 'oauth_nonce', 'oauth_version', 'oauth_signature_method') as $parameter) {
         if (!isset($parameters[$parameter])) {
             throw new RuntimeException(sprintf('Parameter "%s" must be set.', $parameter));
         }
     }
     // Remove oauth_signature if present
     // Ref: Spec: 9.1.1 ("The oauth_signature parameter MUST be excluded.")
     if (isset($parameters['oauth_signature'])) {
         unset($parameters['oauth_signature']);
     }
     // Remove default ports
     // Ref: Spec: 9.1.2
     $explicitPort = $url->has('port') ? $url->get('port') : null;
     if ('https' === $url->get('scheme') && 443 === $explicitPort || 'http' === $url->get('scheme') && 80 === $explicitPort) {
         $explicitPort = null;
     }
     // Remove query params from URL
     // Ref: Spec: 9.1.2
     $urlString = sprintf('%s://%s%s%s', $url->get('scheme'), $url->get('host'), $explicitPort ? ':' . $explicitPort : '', $url->has('path') ? $url->get('path') : '');
     // Parameters are sorted by name, using lexicographical byte value ordering.
     // Ref: Spec: 9.1.1 (1)
     uksort($parameters, 'strcmp');
     // http_build_query should use RFC3986
     $parts = array(strtoupper($method), rawurlencode($urlString), rawurlencode(str_replace(array('%7E', '+'), array('~', '%20'), http_build_query($parameters, '', '&'))));
     $baseString = implode('&', $parts);
     $signatureMethod = $oauth->getSignatureMethod();
     $consumerSecret = $oauth->getConsumerSecret();
     $oAuthTokenSecret = $oauth->getOAuthTokenSecret();
     switch ($signatureMethod) {
         case OAuth::SIGNATURE_METHOD_HMAC:
             $keyParts = array(rawurlencode($consumerSecret), rawurlencode($oAuthTokenSecret));
             $signature = hash_hmac('sha1', $baseString, implode('&', $keyParts), true);
             break;
         case OAuth::SIGNATURE_METHOD_RSA:
             if (!function_exists('openssl_pkey_get_private')) {
                 throw new RuntimeException('RSA-SHA1 signature method requires the OpenSSL extension.');
             }
             $privateKey = openssl_pkey_get_private(file_get_contents($consumerSecret), $oAuthTokenSecret);
             $signature = false;
             openssl_sign($baseString, $signature, $privateKey);
             openssl_free_key($privateKey);
             break;
         case OAuth::SIGNATURE_METHOD_PLAINTEXT:
             $signature = $baseString;
             break;
         default:
             throw new RuntimeException(sprintf('Unknown signature method selected %s.', $signatureMethod));
     }
     $encoded = base64_encode($signature);
     if ($request instanceof FormRequestInterface) {
         $fields = $request->getFields();
         $fields['oauth_signature'] = $encoded;
         // Parameters are sorted by name, using lexicographical byte value ordering.
         // Ref: Spec: 9.1.1 (1)
         uksort($fields, 'strcmp');
         $request->setFields($fields);
     } else {
         $parameters['oauth_signature'] = $encoded;
         // Parameters are sorted by name, using lexicographical byte value ordering.
         // Ref: Spec: 9.1.1 (1)
         uksort($parameters, 'strcmp');
         $url->query->setData($parameters);
         $url = $url->getUrl();
         $request->fromUrl($url);
     }
 }