Example #1
0
 /**
  * @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);
     }
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function buildToken(Connection $connection)
 {
     $token = new TokenCredentials();
     $token->setSecret($connection->getSecret());
     $token->setIdentifier($connection->getIdentifier());
     $this->setToken($token);
     $this->setupProvider();
 }
 public function testStoringOAuth1Token()
 {
     $link = m::mock('Cartalyst\\SentrySocial\\Links\\Eloquent\\Link[save]');
     $tokenCredentials = new OAuth1TokenCredentials();
     $tokenCredentials->setIdentifier('foo');
     $tokenCredentials->setSecret('bar');
     $link->shouldReceive('save')->once();
     $link->storeToken($tokenCredentials);
     $this->assertEquals('foo', $link->oauth1_token_identifier);
     $this->assertEquals('bar', $link->oauth1_token_secret);
 }
Example #4
0
 /** @test */
 public function it_can_store_oauth1_tokens()
 {
     $link = m::mock('Cartalyst\\Sentinel\\Addons\\Social\\Models\\Link[save]');
     $tokenCredentials = new OAuth1TokenCredentials();
     $tokenCredentials->setIdentifier('foo');
     $tokenCredentials->setSecret('bar');
     $link->shouldReceive('save')->once();
     $link->storeToken($tokenCredentials);
     $this->assertEquals('foo', $link->oauth1_token_identifier);
     $this->assertEquals('bar', $link->oauth1_token_secret);
 }
 public function getUserFromToken($token)
 {
     $parts = explode(":", $token);
     if (count($parts) !== 2) {
         throw new InvalidTokenException();
     }
     list($identifier, $secret) = $parts;
     $tokenCredentials = new TokenCredentials();
     $tokenCredentials->setIdentifier($identifier);
     $tokenCredentials->setSecret($secret);
     return $this->createUser($tokenCredentials);
 }
 /**
  * {@inheritdoc}
  */
 protected function findProviderUserFromRequest(ServerRequestInterface $request, ProviderInterface $provider)
 {
     $postParams = $request->getParsedBody();
     $tokenIdentifier = isset($postParams['token_identifier']) ? $postParams['token_identifier'] : null;
     $tokenSecret = isset($postParams['token_secret']) ? $postParams['token_secret'] : null;
     $tokenCredentials = new TokenCredentials();
     $tokenCredentials->setIdentifier($tokenIdentifier);
     $tokenCredentials->setSecret($tokenSecret);
     /** @var \League\OAuth1\Client\Server\Server $providerClient */
     $providerClient = $this->providerClients->get($provider->getName());
     try {
         $user = $providerClient->getUserDetails($tokenCredentials);
     } catch (\Exception $e) {
         throw OAuth2Exception::invalidRequest('Token identifier and token secret are invalid');
     }
     return new Oauth1User($user);
 }
Example #7
0
 /**
  * Creates token credentials from the body response.
  *
  * @param  string  $body
  * @return TokenCredentials
  */
 protected function createTokenCredentials($body)
 {
     parse_str($body, $data);
     if (!$data || !is_array($data)) {
         throw new CredentialsException("Unable to parse token credentials response.");
     }
     if (isset($data['error'])) {
         throw new CredentialsException("Error [{$data['error']}] in retrieving token credentials.");
     }
     $tokenCredentials = new TokenCredentials();
     $tokenCredentials->setIdentifier($data['oauth_token']);
     $tokenCredentials->setSecret($data['oauth_token_secret']);
     return $tokenCredentials;
 }
 /**
  * Since Withings has their own unique implementation of oAuth1 we need to extract the oAuthParameters
  * and append them to the endpoint as a querystring.
  *
  * This is an extraction of $this->protocolHeader()
  *
  * :(
  *
  * @param $url
  * @param TokenCredentials $tokenCredentials
  * @param array $extraParams
  * @return array
  */
 private function getOauthParameters($url, TokenCredentials $tokenCredentials, $extraParams = array())
 {
     $parameters = array_merge($this->baseProtocolParameters(), $this->additionalProtocolParameters(), $extraParams, array('oauth_token' => $tokenCredentials->getIdentifier()));
     $this->signature->setCredentials($tokenCredentials);
     $parameters['oauth_signature'] = $this->signature->sign($url, $parameters, 'GET');
     return $parameters;
 }
Example #9
0
 /**
  * Returns an instance of TwitterOAuthUser.
  *
  * @return TwitterOAuthUser
  */
 public function getUserDetails()
 {
     $accessToken = $this->getAccessToken();
     $tc = new TokenCredentials();
     $tc->setIdentifier($accessToken['oauth_token']);
     $tc->setSecret($accessToken['oauth_token_secret']);
     $user = $this->instance->getUserDetails($tc);
     $twUserObj = new TwitterOAuthUser($user->nickname);
     $twUserObj->setAvatarUrl($user->imageUrl);
     $twUserObj->setName($user->name);
     $twUserObj->setLocation($user->location);
     $twUserObj->setProfileUrl('https://twitter.com/' . $user->screen_name);
     $twUserObj->setWebsite($user->urls['url']);
     $twUserObj->setDescription($user->description);
     $twUserObj->setProfileId($user->uid);
     return $twUserObj;
 }
Example #10
0
 /**
  * Get a Social User instance from a known access token and secret.
  *
  * @param  string  $token
  * @param  string  $secret
  *
  * @return \Arcanedev\Socialite\OAuth\One\User
  */
 public function userFromTokenAndSecret($token, $secret)
 {
     $tokenCredentials = new TokenCredentials();
     $tokenCredentials->setIdentifier($token);
     $tokenCredentials->setSecret($secret);
     $user = $this->server->getUserDetails($tokenCredentials);
     $instance = (new User())->setRaw($user->extra)->setToken($tokenCredentials->getIdentifier(), $tokenCredentials->getSecret());
     return $instance->map(['id' => $user->uid, 'nickname' => $user->nickname, 'name' => $user->name, 'email' => $user->email, 'avatar' => $user->imageUrl]);
 }