/** * Signs the specified request with an SellerCenter API signing protocol by using the * provided SellerCenter API credentials and adding the required headers to the request * * @param RequestInterface $request Request to add a signature to * @param CredentialsInterface $credentials Signing credentials */ public function signRequest(RequestInterface $request, CredentialsInterface $credentials) { $parameters = $request->getQuery()->toArray(); $parameters['UserID'] = $credentials->getId(); $parameters['Version'] = '1.0'; $parameters['Action'] = $request->getConfig()->get('command')->getName(); $parameters['Timestamp'] = gmdate(DateTime::ISO8601); // the keys MUST be in alphabetical order to correct signature calculation ksort($parameters); $parameters['Signature'] = rawurlencode(hash_hmac('sha256', http_build_query($parameters, '', '&', PHP_QUERY_RFC3986), $credentials->getKey(), false)); $request->setQuery($parameters); }
protected function clientCall($path, $method = 'GET', $data = array()) { if ($this->request instanceof RequestInterface) { $this->request = $this->client->createRequest($method); } $this->request->setPath($path); $this->request->setMethod($method); $this->request->setQuery($data); if (isset($this->accessToken)) { $this->request->getQuery()->set('access_token', $this->accessToken); } $response = $this->client->send($this->request); return $response->json(); }
/** * Calculate signature for request * * @param RequestInterface $request Request to generate a signature for * * @return string * * @throws \RuntimeException */ public function getSignature(RequestInterface $request) { // For POST|PUT set the JSON body string as the params if ($request->getMethod() == 'POST' || $request->getMethod() == 'PUT') { $params = $request->getBody()->__toString(); // Make sure to remove any other query params $request->setQuery([]); } else { $params = Query::fromString($request->getQuery(), Query::RFC1738)->toArray(); $params = $this->prepareParameters($params); // Re-Set the query to the properly ordered query string $request->setQuery($params); $request->getQuery()->setEncodingType(Query::RFC1738); } $baseString = $this->createBaseString($request, $params); return base64_encode($this->sign_HMAC_SHA256($baseString)); }
private function add_query(RequestInterface $request, $value) { if ($value instanceof Query) { $original = $request->getQuery(); // Do not overwrite existing query string variables by overwriting // the object with the query string data passed in the URL $request->setQuery($value->overwriteWith($original->toArray())); } elseif (is_array($value)) { // Do not overwrite existing query string variables $query = $request->getQuery(); foreach ($value as $k => $v) { if (!isset($query[$k])) { $query[$k] = $v; } } } else { throw new \InvalidArgumentException('query value must be an array ' . 'or Query object'); } }
/** * Set HTTP request to activeCollab API * @param RequestInterface $request * @param bool $authenticate * @return object * @throws Exception\ApiException * @throws Exception\AuthenticationFailedException */ public function send(RequestInterface $request, $authenticate = true) { $request->addHeader('Accept', 'application/json'); if ($authenticate) { $request->setQuery($request->getQuery()->set('auth_api_token', $this->api_token)); } /** @var ResponseInterface $response */ $response = $this->client->send($request); if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) { switch ($response->getStatusCode()) { case 403: throw new AuthenticationFailedException($response); default: throw new ApiException($response); } } return $this->parseResponse($request, $response); }
/** * Calculate signature for request * * @param RequestInterface $request Request to generate a signature for * * @return string */ public function getSignature(RequestInterface $request) { // For POST|PUT set the JSON body string as the params if ($request->getMethod() == 'POST' || $request->getMethod() == 'PUT') { $params = $request->getBody()->__toString(); /** * If you don't seek() back to the beginning then attempting to * send a JSON body > 1MB will probably fail. * * @link http://stackoverflow.com/q/32359664/99071 * @link https://groups.google.com/forum/#!topic/guzzle/vkF5druf6AY */ $request->getBody()->seek(0); // Make sure to remove any other query params $request->setQuery([]); } else { $params = Query::fromString($request->getQuery(), Query::RFC1738)->toArray(); $params = $this->prepareParameters($params); // Re-Set the query to the properly ordered query string $request->setQuery($params); $request->getQuery()->setEncodingType(Query::RFC1738); } $baseString = $this->createBaseString($request, $params); return base64_encode($this->sign_HMAC_SHA256($baseString)); }
public function withQuery(Query $query) { $this->expectedRequest->setQuery($query); return $this; }