/**
  * @param string $token
  *
  * @return ProviderMetadata
  */
 public function getUserDetails($token)
 {
     try {
         // Fetch user data
         list($identifier, $secret) = explode('@', $token);
         $tokenObject = new TokenCredentials();
         $tokenObject->setIdentifier($identifier);
         $tokenObject->setSecret($secret);
         $url = 'https://api.bitbucket.org/2.0/user';
         $headers = $this->oauthProvider->getHeaders($tokenObject, 'GET', $url);
         $response = $this->httpClient->get($url, $headers);
         $data = json_decode($response->getContent(), true);
         if (empty($data) || json_last_error() !== JSON_ERROR_NONE) {
             throw new \RuntimeException('Json error');
         }
         // Fetch email
         $url = sprintf('https://api.bitbucket.org/1.0/users/%s/emails', $data['username']);
         $headers = $this->oauthProvider->getHeaders($tokenObject, 'GET', $url);
         $response = $this->httpClient->get($url, $headers);
         $emails = json_decode($response->getContent(), true);
         if (empty($emails) || json_last_error() !== JSON_ERROR_NONE) {
             throw new \RuntimeException('Json error');
         }
         $emails = array_filter($emails, function ($emailData) {
             return true === $emailData['primary'];
         });
         $data['email'] = empty($emails) ? '' : current($emails)['email'];
         return new ProviderMetadata(['uid' => $data['uuid'], 'nickName' => $data['username'], 'realName' => $data['display_name'], 'email' => $data['email'], 'profilePicture' => $data['links']['avatar']['href'], 'homepage' => $data['links']['html']['href'], 'location' => $data['location']]);
     } catch (\Exception $e) {
         throw new \RuntimeException('cannot fetch account details', 0, $e);
     }
 }
Exemple #2
0
 /**
  * {@inheritDoc}
  */
 public function requireScope($scope = null)
 {
     $temporaryCredentials = $this->oauthClient->getTemporaryCredentials();
     $authorizeUrl = $this->oauthClient->getAuthorizationUrl($temporaryCredentials);
     $this->session->set('provider/' . $this->getName() . '/temporary_credentials', $temporaryCredentials);
     return new RedirectResponse($authorizeUrl);
 }