/** * Convert the OAuth2 response to a \Zend\Http\Response * * @param $response OAuth2Response * @return \Zend\Http\Response */ private function setHttpResponse(OAuth2Response $response) { $httpResponse = $this->getResponse(); $httpResponse->setStatusCode($response->getStatusCode()); $headers = $httpResponse->getHeaders(); $headers->addHeaders($response->getHttpHeaders()); $headers->addHeaderLine('Content-type', 'application/json'); $httpResponse->setContent($response->getResponseBody()); return $httpResponse; }
/** * Takes an OAuth2 response and converts it to JSON output via Tonis\Http\Response. * * @param Response $oauthResponse * @param ResponseInterface $psrResponse * @return ResponseInterface */ public static function convertResponseToPsr7(Response $oauthResponse, ResponseInterface $psrResponse) { $psrResponse = $psrResponse->withStatus($oauthResponse->getStatusCode()); foreach ($oauthResponse->getHttpHeaders() as $header => $value) { $psrResponse = $psrResponse->withHeader($header, $value); } if (!empty($oauthResponse->getParameters())) { $psrResponse = $psrResponse->withHeader('Content-Type', 'application/json'); $psrResponse->getBody()->write(json_encode($oauthResponse->getParameters())); } return $psrResponse; }
/** * Copies values from the given Oauth2\Response to a PSR-7 Http Response. * * @param OAuth2\Response $oauth2Response The OAuth2 server response. * * @return \Psr\Http\Message\ResponseInterface */ public static final function fromOauth2(OAuth2\Response $oauth2Response) { $headers = []; foreach ($oauth2Response->getHttpHeaders() as $key => $value) { $headers[$key] = explode(', ', $value); } $stream = fopen('php://temp', 'r+'); if (!empty($oauth2Response->getParameters())) { fwrite($stream, $oauth2Response->getResponseBody()); rewind($stream); } return new Response(new Stream($stream), $oauth2Response->getStatusCode(), $headers); }
/** * @param Response $response */ public function __construct(Response $response) { $this->setStatusCode($response->getStatusCode()); $this->getHeaders()->addHeader(new GenericHeader('Content-type', 'application/json'))->addHeaders($response->getHttpHeaders()); $this->setContent($response->getResponseBody('json')); }
/** * Merge the OAuth2\Response instance's status and headers into the current Zend\Http\Response. * * @param int $status * @param Response $response * @param OAuth2Response $oauth2Response * @return Response */ private function mergeOAuth2Response($status, Response $response, OAuth2Response $oauth2Response) { $response->setStatusCode($status); return $this->mergeOAuth2ResponseHeaders($response, $oauth2Response->getHttpHeaders()); }
private function buildResponse($format, HttpResponse $httpResponse, OAuthResponse $oauthResponse) { $httpResponse->setVersion($oauthResponse->version); $httpResponse->setStatusCode($oauthResponse->getStatusCode()); $headers = $httpResponse->getHeaders(); foreach ($oauthResponse->getHttpHeaders() as $name => $value) { $headers->addHeaderLine(sprintf('%s: %s', $name, $value)); } switch ($format) { case 'json': $headers->addHeaderLine('Content-Type: application/json'); $httpResponse->setContent(json_encode($oauthResponse->getParameters())); break; case 'xml': $headers->addHeaderLine('Content-Type: text/xml'); $xml = new SimpleXMLElement('<response/>'); foreach ($oauthResponse->getParameters() as $key => $param) { $xml->addChild($key, $param); } $httpResponse->setContent($xml->asXML()); break; default: throw new RuntimeException('Invalid format provided: ' . $format); } return $httpResponse; }