protected function buildAuthorizationHeaderForAPIRequest($method, UriInterface $uri, TokenInterface $token, $bodyParams = null) { $this->signature->setTokenSecret($token->getAccessTokenSecret()); $parameters = $this->getBasicAuthorizationHeaderInfo(); if (isset($parameters['oauth_callback'])) { unset($parameters['oauth_callback']); } $parameters = array_merge($parameters, ['oauth_token' => $token->getAccessToken()]); $parameters = array_merge($parameters, $bodyParams); $parameters['oauth_signature'] = $this->signature->getSignature($uri, $parameters, $method); $authorizationHeader = 'OAuth '; $delimiter = ''; foreach ($parameters as $key => $value) { $authorizationHeader .= $delimiter . rawurlencode($key) . '="' . rawurlencode($value) . '"'; $delimiter = ', '; } return $authorizationHeader; }
/** * Validates a Test REST api call access using oauth access token * * @param TokenInterface $token The access token. * @param string $method HTTP method. * @return array * @throws TokenResponseException */ public function validateAccessToken($token, $method = 'GET') { //Need to add Accept header else Magento errors out with 503 $extraAuthenticationHeaders = ['Accept' => 'application/json']; $this->signature->setTokenSecret($token->getAccessTokenSecret()); $authorizationHeader = ['Authorization' => $this->buildAuthorizationHeaderForAPIRequest($method, $this->getTestApiEndpoint(), $token, [])]; $headers = array_merge($authorizationHeader, $extraAuthenticationHeaders); $responseBody = $this->httpClient->retrieveResponse($this->getTestApiEndpoint(), [], $headers, $method); return json_decode($responseBody); }
/** * * @param type $method * @param UriInterface $uri * @param \OAuth\OAuth1\Token\TokenInterface $token * @param type $bodyParams * @return string */ protected function buildAuthorizationHeaderForAPIRequest($method, UriInterface $uri, \OAuth\OAuth1\Token\TokenInterface $token, $bodyParams = null) { $this->signature->setTokenSecret($token->getAccessTokenSecret()); $authParameters = $this->getBasicAuthorizationHeaderInfo(); if (isset($authParameters['oauth_callback'])) { unset($authParameters['oauth_callback']); } $authParameters = array_merge($authParameters, array('oauth_token' => $token->getAccessToken())); $authParameters = is_array($bodyParams) ? array_merge($authParameters, $bodyParams) : $authParameters; //In base class here is because $bodyParams array is never merged with $authParameters $authParameters['oauth_signature'] = $this->signature->getSignature($uri, $authParameters, $method); if (isset($bodyParams['oauth_session_handle'])) { $authParameters['oauth_session_handle'] = $bodyParams['oauth_session_handle']; unset($bodyParams['oauth_session_handle']); } $authorizationHeader = 'OAuth '; $delimiter = ''; foreach ($authParameters as $key => $value) { $authorizationHeader .= $delimiter . rawurlencode($key) . '="' . rawurlencode($value) . '"'; $delimiter = ', '; } return $authorizationHeader; }