/** * @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; }
public function __construct(Storages\ICache $cache, IClient $client = NULL) { $this->cache = $cache; $this->client = $client ?: Github\Helpers::createDefaultClient(); }
/** * @param Storages\ICache * @param IClient * @param bool forbid checking Github for new data; more or less development purpose only */ public function __construct(Storages\ICache $cache, IClient $client = NULL, $forbidRecheck = FALSE) { $this->cache = $cache; $this->client = $client ?: Github\Helpers::createDefaultClient(); $this->forbidRecheck = (bool) $forbidRecheck; }