Esempio n. 1
0
 /**
  * 
  * @param Irto\OAuth2Proxy\ProxyResponse $response
  * @param Closure $next
  * 
  * @return Irto\OAuth2Proxy\ProxyResponse
  */
 public function response($response, Closure $next)
 {
     $original = $response->clientResponse();
     $original->on('data', function ($data) use($response, $original) {
         $response->write($data);
         if ($response->dataLength() === (int) $response->headers()->get('content-length', -1)) {
             $original->close();
         }
     });
     $original->on('end', function () use($response) {
         $response->end();
     });
     return $next($response);
 }
Esempio n. 2
0
 /**
  * Catch $response on get it from api server.
  * 
  * @param Irto\OAuth2Proxy\ProxyResponse $response
  * @param Closure $next
  * 
  * @return React\HttpClient\Response
  */
 public function response($response, Closure $next)
 {
     $request = $response->originRequest();
     $original = $response->clientResponse();
     if ($request->originRequest()->getPath() == $this->server['config']->get('grant_path') && $original->getCode() == 200) {
         $response->setCode(204);
         $original->removeAllListeners('data');
         $original->on('data', function ($data) use($response, $original) {
             $this->bufferData($data);
             if ($response->dataLength() === (int) $response->headers()->get('content-length', -1)) {
                 $original->close();
             }
         });
         $original->on('end', function () use($response) {
             $response->end();
             $this->processResponse($response, json_decode($this->getDataEnd(), true));
         });
     }
     $session = $request->session();
     if ($original->getCode() == 401 && $session->has('oauth_grant.refresh_token')) {
         $original->removeAllListeners('data');
         $this->updateCredentials($response, function ($clientResponse) use($response, $request, $next, $session) {
             $request->removeAllListeners('response');
             $request->on('response', function ($original) use($next, $response) {
                 $response->mergeClientResponse($original);
                 $next($response);
             });
             $credentials = $session->get('oauth_grant');
             $request->headers()->put('authorization', "{$credentials['token_type']} {$credentials['access_token']}");
             $request->retry();
         });
         return false;
     }
     return $next($response);
 }