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
 /**
  * @param  callable|NULL function(Response $response)
  * @return self
  */
 public function onResponse($callback)
 {
     $this->client->onResponse(NULL);
     $this->onResponse = $callback;
     return $this;
 }