private function fetchResumeUri()
 {
     $result = null;
     $body = $this->request->getBody();
     if ($body) {
         $headers = array('content-type' => 'application/json; charset=UTF-8', 'content-length' => $body->getSize(), 'x-upload-content-type' => $this->mimeType, 'x-upload-content-length' => $this->size, 'expect' => '');
         foreach ($headers as $key => $value) {
             $this->request = $this->request->withHeader($key, $value);
         }
     }
     $response = $this->client->execute($this->request, false);
     $location = $response->getHeaderLine('location');
     $code = $response->getStatusCode();
     if (200 == $code && true == $location) {
         return $location;
     }
     $message = $code;
     $body = json_decode((string) $this->request->getBody(), true);
     if (isset($body['error']['errors'])) {
         $message .= ': ';
         foreach ($body['error']['errors'] as $error) {
             $message .= "{$error[domain]}, {$error[message]};";
         }
         $message = rtrim($message, ';');
     }
     $error = "Failed to start the resumable upload (HTTP {$message})";
     $this->client->getLogger()->error($error);
     throw new GoogleException($error);
 }
 /**
  * @param \Psr\Http\Message\RequestInterface $request
  *
  * @return \Psr\Http\Message\RequestInterface
  */
 public function sign(RequestInterface $request)
 {
     $timestamp = (new \DateTime('now', new \DateTimeZone('UTC')))->getTimestamp();
     $data = implode('|', [$request->getMethod(), rtrim((string) $request->getUri(), '/'), $timestamp]);
     $signature = $this->signer->sign($data);
     return $request->withHeader(self::TIMESTAMP_HEADER, $timestamp)->withHeader(self::SIGNATURE_HEADER, $signature);
 }
Example #3
0
 /**
  * {@inheritdoc}
  */
 public function authenticate(RequestInterface $request)
 {
     // TODO: generate better nonce?
     $nonce = substr(md5(uniqid(uniqid() . '_', true)), 0, 16);
     $created = date('c');
     $digest = base64_encode(sha1(base64_decode($nonce) . $created . $this->password, true));
     $wsse = sprintf('UsernameToken Username="******", PasswordDigest="%s", Nonce="%s", Created="%s"', $this->username, $digest, $nonce, $created);
     return $request->withHeader('Authorization', 'WSSE profile="UsernameToken"')->withHeader('X-WSSE', $wsse);
 }
Example #4
0
 /**
  *
  * @param RequestInterface $request
  */
 public function onRequest(RequestInterface $request)
 {
     reset($this->headers);
     $newRequest = $request->withHeader('Host', $request->getUri()->getHost())->withHeader('User-Agent', self::USER_AGENT);
     $addHeaders = function (&$array, RequestInterface $request, callable $callback) {
         $item = each($array);
         if (!$item) {
             return $request;
         }
         return $callback($array, $request, $callback)->withHeader($item['key'], $item['value']);
     };
     return $addHeaders($this->headers, $newRequest, $addHeaders);
 }
Example #5
0
 /**
  * @param RequestInterface $request
  *
  * @return RequestInterface
  */
 public function __invoke(RequestInterface $request)
 {
     $uri = $request->getUri();
     $path = $uri->getPath();
     $path .= $uri->getQuery() != null ? '?' . $uri->getQuery() : '';
     $payload = ['key' => 'master', 'exp' => time() + $this->exp, 'method' => $request->getMethod(), 'path' => $path];
     if (in_array($request->getMethod(), ['PUT', 'POST'])) {
         $body = $request->getBody();
         $computedHash = \GuzzleHttp\Psr7\hash($body, 'sha256');
         $payload['body'] = ['alg' => 'sha256', 'hash' => $computedHash];
     }
     $jws = new JWS(['typ' => 'JWT', 'alg' => 'HS256']);
     $jws->setPayload($payload)->sign($this->secret);
     $token = $jws->getTokenString();
     return $request->withHeader('Authorization', 'JWT token="' . $token . '"');
 }
 public function __invoke(RequestInterface $request, array $options)
 {
     $fn = $this->nextHandler;
     if (!isset($options['oxygen_site']) || !$options['oxygen_site'] instanceof Site) {
         throw new \RuntimeException(sprintf('The option "oxygen_site" is expected to contain an instance of %s.', Site::class));
     }
     if (!isset($options['oxygen_action']) || !$options['oxygen_action'] instanceof ActionInterface) {
         throw new \RuntimeException(sprintf('The option "oxygen_action" is expected to contain an instance of %s.', ActionInterface::class));
     }
     $transferInfo = $previousCallable = null;
     if (isset($options['on_stats'])) {
         $previousCallable = $options['on_stats'];
     }
     $options['on_stats'] = function (TransferStats $stats) use(&$transferInfo, $previousCallable) {
         $transferInfo = new TransferInfo($stats->getHandlerStats());
         if ($previousCallable) {
             /* @var callable $previousCallable */
             $previousCallable($stats);
         }
     };
     /** @var Site $site */
     $site = $options['oxygen_site'];
     /** @var ActionInterface $action */
     $action = $options['oxygen_action'];
     $options['request_id'] = $requestId = \Undine\Functions\generate_uuid();
     // Kind of like str_rot8, for hexadecimal strings.
     $responseId = strtr($requestId, 'abcdef0123456789', '23456789abcdef01');
     $expiresAt = time() + 86400;
     $userName = '';
     $stateParameters = $this->stateTracker->getParameters($site);
     $requestData = ['oxygenRequestId' => $requestId, 'requestExpiresAt' => $expiresAt, 'publicKey' => $site->getPublicKey(), 'signature' => \Undine\Functions\openssl_sign_data($site->getPrivateKey(), sprintf('%s|%d', $requestId, $expiresAt)), 'handshakeKey' => $this->handshakeKeyName, 'handshakeSignature' => \Undine\Functions\openssl_sign_data($this->handshakeKeyValue, $this->getUrlSlug($site->getUrl())), 'version' => $this->moduleVersion, 'baseUrl' => (string) $site->getUrl(), 'actionName' => $action->getName(), 'actionParameters' => $action->getParameters(), 'userName' => $userName, 'userId' => $site->getUser()->getId(), 'stateParameters' => $stateParameters];
     $oxygenRequest = $request->withHeader('accept', 'text/html,application/json,application/oxygen')->withBody(\GuzzleHttp\Psr7\stream_for(json_encode($requestData)));
     if ($site->hasHttpCredentials()) {
         $oxygenRequest = $oxygenRequest->withHeader('Authorization', 'Basic ' . base64_encode(sprintf('%s:%s', $site->getHttpCredentials()->getUsername(), $site->getHttpCredentials()->getPassword())));
     }
     return $fn($oxygenRequest, $options)->then(function (ResponseInterface $response) use($site, $request, $options, &$transferInfo, $responseId, $action) {
         $responseData = $this->extractData($responseId, $request, $options, $response, $transferInfo);
         try {
             $reaction = $this->createReaction($action, $responseData);
         } catch (ExceptionInterface $e) {
             throw new ResponseException(ResponseException::ACTION_RESULT_MALFORMED, $request, $options, $response, $transferInfo, $e);
         }
         try {
             $this->stateTracker->setResult($site, $responseData['stateResult']);
         } catch (\Exception $e) {
             throw new ResponseException(ResponseException::STATE_MALFORMED, $request, $options, $response, $transferInfo, $e);
         }
         return $reaction;
     }, function (RequestException $e) use($options, &$transferInfo) {
         throw new NetworkException($e->getHandlerContext()['errno'], $e->getRequest(), $options, $e->getResponse(), $transferInfo);
     })->otherwise(function (\Exception $exception) use($site) {
         $this->stateTracker->setException($site, $exception);
         throw $exception;
     });
 }
 /**
  * Add HTTP header to request.
  *
  * {@inheritdoc}
  */
 public function __invoke(Request $request, Response $response, callable $next)
 {
     return call_user_func($next, $request->withHeader(self::HEADER, $this->key), $response);
 }
 /**
  * @inheritDoc
  */
 public function decorate(RequestInterface $request)
 {
     return $request->withHeader('Referer', 'https://www.fshare.vn/');
 }
Example #9
0
 /**
  * Add the API key as Authorization header.
  *
  * @param RequestInterface $request
  *
  * @return \Psr\Http\Message\MessageInterface|\Psr\Http\Message\RequestInterface
  */
 public function onRequest(RequestInterface $request)
 {
     return $request->withHeader('Authorization', 'Bearer ' . $this->getEndpoint()->getApiKey());
 }
 /**
  * Will be called by Guzzle. Adds the desired Authorization header.
  *
  * @param RequestInterface $request
  *
  * @return \Psr\Http\Message\MessageInterface
  */
 private function onBefore(RequestInterface $request)
 {
     return $request->withHeader('Authorization', 'Bearer ' . $this->token);
 }
Example #11
0
 /**
  * Add authentication data to the request and sends it. Calls handleResponse when done.
  *
  * @param RequestInterface $request
  *
  * @return mixed
  */
 protected function handleRequest(RequestInterface $request, array $requestOptions = [])
 {
     $request->withHeader('Authorization', 'Bearer ' . $this->options['token']);
     try {
         $response = $this->httpClient->send($request);
     } catch (ClientException $ex) {
         return $this->handleResponse($ex->getResponse());
     }
     return $this->handleResponse($response, $requestOptions);
 }
 public function modify(RequestInterface $response) : RequestInterface
 {
     return $response->withHeader('User-Agent', $this->userAgent);
 }
 /**
  * Config http request
  *
  * @param RequestInterface $request Http request
  *
  * @return RequestInterface
  */
 protected function request(RequestInterface $request)
 {
     return $request->withHeader('Accept', 'application/json')->withHeader('User-Agent', 'RadPHP-OAuth');
 }
Example #14
0
 public function withCookieHeader(RequestInterface $request)
 {
     $values = [];
     $uri = $request->getUri();
     $scheme = $uri->getScheme();
     $host = $uri->getHost();
     $path = $uri->getPath() ?: '/';
     foreach ($this->cookies as $cookie) {
         if ($cookie->matchesPath($path) && $cookie->matchesDomain($host) && !$cookie->isExpired() && (!$cookie->getSecure() || $scheme == 'https')) {
             $values[] = $cookie->getName() . '=' . self::getCookieValue($cookie->getValue());
         }
     }
     return $values ? $request->withHeader('Cookie', implode('; ', $values)) : $request;
 }
 /**
  * Add HTTP header to request.
  *
  * {@inheritdoc}
  */
 public function __invoke(Request $request, Response $response, callable $next)
 {
     return call_user_func($next, $request->withHeader(self::HEADER, "Bearer {$this->sessionToken}"), $response);
 }
Example #16
0
 function it_authenticates_a_request(RequestInterface $request, RequestInterface $newRequest)
 {
     $request->withHeader('X-Auth-Token', 'token')->willReturn($newRequest);
     $request = $this->authenticateRequest($request);
     $request->shouldBe($newRequest);
 }
Example #17
0
 /**
  * {@inheritdoc}
  */
 public function authenticate(RequestInterface $request)
 {
     $header = sprintf('Bearer %s', $this->token);
     return $request->withHeader('Authorization', $header);
 }
Example #18
0
 /**
  * {@inheritdoc}
  */
 public function authenticateRequest(RequestInterface $request)
 {
     return $request->withHeader('X-Auth-Token', $this->token);
 }
Example #19
0
 public function __invoke(RequestInterface $request)
 {
     return $request->withHeader('Authorization', sprintf('Bearer %s', $this->getToken()));
 }
 /**
  * {@inheritdoc}
  */
 public function authenticate(RequestInterface $request)
 {
     return $request->withHeader(self::COOKIE_CACHET_TOKEN, $this->token);
 }
 /**
  * @param string $header
  * @param string|\string[] $value
  * @return \Psr\Http\Message\MessageInterface
  */
 public function withHeader($header, $value)
 {
     $this->request = $this->request->withHeader($header, $value);
     return $this;
 }
Example #22
0
 /**
  * Add Basic HTTP authentication to request
  *
  * @param RequestInterface $request
  * @param string           $username
  * @param string           $password
  *
  * @return RequestInterface
  *
  * @since 1.01
  */
 public function basic(RequestInterface $request, $username, $password)
 {
     return $request->withHeader('Authorization', 'Basic: ' . base64_encode("{$username}:{$password}"));
 }
Example #23
0
 /**
  * {@inheritdoc}
  */
 public function authenticate(RequestInterface $request)
 {
     $header = sprintf('Basic %s', base64_encode(sprintf('%s:%s', $this->username, $this->password)));
     return $request->withHeader('Authorization', $header);
 }
Example #24
0
 /**
  * @param string $header
  * @param string|string[] $value
  */
 public function addHeaderToRequest($header, $value)
 {
     $this->request = $this->request->withHeader($header, $value);
 }
 /**
  * @param RequestInterface $request
  *
  * @return RequestInterface New instance with Authorization header
  */
 public function authenticate(RequestInterface $request)
 {
     $base64 = base64_encode(sprintf('%s:%s', $this->user, $this->password));
     $value = sprintf('Basic %s', $base64);
     return $request->withHeader('Authorization', $value);
 }
Example #26
0
 public function withHeader($name, $value)
 {
     $new = clone $this;
     $new->request = $this->request->withHeader($name, $value);
     return $new;
 }
Example #27
0
 /**
  * @since 2.0.0
  * @param string $dsn Data Source Name
  */
 public function __construct($dsn)
 {
     $this->driver = new CurlDriver();
     $this->request = new Request($dsn);
     $this->request->withHeader('Content-Type', 'application/json');
 }
 /**
  * {@inheritdoc}
  */
 public function applyRequest(HttpRequestInterface $request, array $options)
 {
     return $request->withHeader('Accept-Encoding', 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3')->withHeader('Content-Type', 'application/json');
 }
 /**
  * Adds request id to request and return new instance
  *
  * @param RequestInterface $request
  *
  * @return RequestInterface
  */
 public function decorate(RequestInterface $request)
 {
     return $request->withHeader($this->headerName, $this->requestIdProvider->getRequestId());
 }
Example #30
-15
 /**
  * {@inheritdoc}
  */
 public function authenticate(RequestInterface $request)
 {
     // TODO: add error handling
     // TODO: support other grant types?
     $accessToken = $this->provider->getAccessToken('client_credentials');
     return $request->withHeader('Authorization', 'Bearer ' . $accessToken);
 }