Example #1
0
 /**
  * @param  string
  * @param  string
  * @return Token
  *
  * @throws LoginException
  */
 public function obtainToken($code, $state)
 {
     if ($state !== $this->storage->get('auth.state')) {
         throw new LoginException('OAuth security state does not match.');
     }
     $params = ['client_id' => $this->conf->clientId, 'client_secret' => $this->conf->clientSecret, 'code' => $code];
     $headers = ['Accept' => 'application/json', 'Content-Type' => 'application/x-www-form-urlencoded'];
     $request = new Http\Request(Http\Request::POST, $this->tokenUrl, $headers, http_build_query($params));
     try {
         $response = $this->client->request($request);
     } catch (Http\BadResponseException $e) {
         throw new LoginException('HTTP request failed.', 0, $e);
     }
     try {
         /** @var $json \stdClass */
         if ($response->isCode(Http\Response::S404_NOT_FOUND)) {
             $json = Github\Helpers::jsonDecode($response->getContent());
             throw new LoginException($json->error, $response->getCode());
         } elseif (!$response->isCode(Http\Response::S200_OK)) {
             throw new LoginException('Unexpected response.', $response->getCode());
         }
         $json = Github\Helpers::jsonDecode($response->getContent());
     } catch (Github\JsonException $e) {
         throw new LoginException('Bad JSON in response.', 0, $e);
     }
     $token = new Token($json->access_token, $json->token_type, strlen($json->scope) ? explode(',', $json->scope) : []);
     $this->storage->set('auth.token', $token->toArray());
     $this->storage->remove('auth.state');
     return $token;
 }
Example #2
0
 /**
  * @return Response
  *
  * @throws BadResponseException
  */
 public function request(Request $request)
 {
     $request = clone $request;
     $cacheKey = implode('.', [$request->getMethod(), $request->getUrl(), $request->getHeader('Accept'), $request->getHeader('Accept-Encoding'), $request->getHeader('Authorization')]);
     if ($cached = $this->cache->load($cacheKey)) {
         if ($this->forbidRecheck) {
             $cached = clone $cached;
             $this->onResponse && call_user_func($this->onResponse, $cached);
             return $cached;
         }
         /** @var $cached Response */
         if ($cached->hasHeader('Last-Modified')) {
             $request->addHeader('If-Modified-Since', $cached->getHeader('Last-Modified'));
         } elseif ($cached->hasHeader('ETag')) {
             $request->addHeader('If-None-Match', $cached->getHeader('ETag'));
         }
     }
     $response = $this->client->request($request);
     if ($this->isCacheable($response)) {
         $this->cache->save($cacheKey, clone $response);
     }
     if (isset($cached) && $response->getCode() === Response::S304_NOT_MODIFIED) {
         $cached = clone $cached;
         /** @todo Should be responses somehow combined into one? */
         $response = $cached->setPrevious($response);
     }
     $this->onResponse && call_user_func($this->onResponse, $response);
     return $response;
 }