public function handleResponse(UserResponseInterface $response, UserService $userService)
 {
     $fields = $response->getResponse();
     $gitHubLogin = $fields['login'];
     $accessToken = $response->getAccessToken();
     $user = $userService->findByGitHubLogin($gitHubLogin);
     if (null === $user) {
         throw new UsernameNotFoundException();
     }
     $oAuthUser = new OAuthUser($user);
     $oAuthUser->addRole('ROLE_GITHUB_USER');
     $oAuthUser->setAccessToken($accessToken);
     if (array_key_exists('name', $fields)) {
         $gitHubName = $fields['name'];
         $oAuthUser->setRealName($gitHubName);
     } else {
         $oAuthUser->setRealName($gitHubLogin);
     }
     $client = new Client();
     $client->setOption('api_version', 'v3');
     $client->authenticate($response->getAccessToken(), Client::AUTH_HTTP_TOKEN);
     /* @var \Github\Api\CurrentUser $currentUserApi */
     $currentUserApi = $client->api('current_user');
     $emails = $currentUserApi->emails();
     $allEMails = $emails->all();
     $oAuthUser->setEmail($this->getPrimaryEmailAddress($allEMails));
     return $oAuthUser;
 }
 /**
  * Create a github client wrapper with automated token-based authentication.
  *
  * @param string $token The API token to authenticate with.
  * @param string $owner The owner name of the github repository.
  * @param string $repo The name of the github repository.
  * @param string $apiUrl The base url to the github API if different from the main github site (i.e., GitHub Enterprise).
  * @return self The github client wrapper, authenticated against the API.
  */
 public static function createWithToken($token, $owner, $repo, $apiUrl = null)
 {
     $client = new Client();
     if ($apiUrl !== null) {
         $client->setOption('base_url', $apiUrl);
     }
     $client->authenticate($token, null, Client::AUTH_HTTP_TOKEN);
     return new static($client, $owner, $repo);
 }
Beispiel #3
0
 /**
  * @return Client
  */
 protected function buildGitHubClient()
 {
     $httpClient = new HttpClient(['base_url' => $this->config['base_url']]);
     $client = new Client($httpClient);
     if (false !== getenv('GITHUB_DEBUG')) {
         $logPlugin = LogPlugin::getDebugPlugin();
         $httpClient = $client->getHttpClient();
         $httpClient->addSubscriber($logPlugin);
     }
     $client->setOption('base_url', $this->config['base_url']);
     $this->url = rtrim($this->config['base_url'], '/');
     $this->domain = rtrim($this->config['repo_domain_url'], '/');
     return $client;
 }