/**
  * {@inheritdoc}
  */
 protected function parseAccessTokenResponse($responseBody)
 {
     $data = json_decode($responseBody, true);
     if (null === $data || !is_array($data)) {
         throw new TokenResponseException('Unable to parse response.');
     } elseif (isset($data['error'])) {
         throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
     }
     $token = new StdOAuth2Token();
     $token->setAccessToken($data['access_token']);
     // Salesforce access tokens depend on the session timeout settings.
     // The session timeout for an access token can be configured in Salesforce from Setup by clicking Security Controls | Session Settings.
     $token->setEndOfLife(StdOAuth2Token::EOL_UNKNOWN);
     unset($data['access_token']);
     if (isset($data['refresh_token'])) {
         $token->setRefreshToken($data['refresh_token']);
         // Save Refresh Token persistently until it is cleared manually
         update_option('gf_salesforce_refreshtoken', $data['refresh_token']);
         unset($data['refresh_token']);
     } else {
         $refresh_token = get_option('gf_salesforce_refreshtoken');
         if (!empty($refresh_token)) {
             $token->setRefreshToken($refresh_token);
         }
     }
     $token->setExtraParams($data);
     return $token;
 }
 protected function parseAccessTokenResponse($responseBody)
 {
     $data = json_decode($responseBody, true);
     if (null === $data || !is_array($data)) {
         throw new TokenResponseException('Unable to parse response.');
     } elseif (isset($data['error'])) {
         throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
     }
     $token = new StdOAuth2Token();
     $token->setAccessToken($data['access_token']);
     return $token;
 }
Example #3
0
 /**
  * Set the right base endpoint.
  * 
  * @param StdOAuth2Token $token
  */
 protected function setBaseApiUri(StdOAuth2Token $token)
 {
     // Make request uri.
     $endpoint = 'https://login.mailchimp.com/oauth2/metadata?oauth_token=' . $token->getAccessToken();
     // Grab meta data about the token.
     $response = $this->httpClient->retrieveResponse(new Uri($endpoint), array(), array(), 'GET');
     // Parse JSON.
     $meta = json_decode($response, true);
     // Set base api uri.
     $this->baseApiUri = new Uri('https://' . $meta['dc'] . '.api.mailchimp.com/2.0/');
     // Allow chaining.
     return $this;
 }
Example #4
0
 /**
  * @param array $config
  *
  * param example:
  *
  * private $config = [
  *   'app_key' => 'APP KEY',
  *   'app_secret' => 'APP SECRET',
  *   'access_token' => 'USER TOKEN'
  * ];
  *
  */
 public function __construct($config)
 {
     $serviceFactory = new \OAuth\ServiceFactory();
     $serviceFactory->setHttpClient(new CurlClient());
     $token = new StdOAuth2Token();
     $token->setAccessToken($config['access_token']);
     $storage = new Session();
     $storage->storeAccessToken('linkedin', $token);
     $redirectUrl = '';
     if (isset($_SERVER['HTTP_HOST'])) {
         $redirectUrl = $_SERVER['HTTP_HOST'];
     }
     $credentials = new Credentials($config['app_key'], $config['app_secret'], $redirectUrl);
     $this->service = $serviceFactory->createService('linkedin', $credentials, $storage, ['r_fullprofile', 'r_emailaddress', 'rw_nus']);
 }
Example #5
0
 protected function parseAccessTokenResponse($responseBody)
 {
     $data = json_decode($responseBody, true);
     if ($data === null || !is_array($data)) {
         throw new TokenResponseException('Unable to parse response.');
     } elseif (isset($data['error'])) {
         throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
     }
     $token = new StdOAuth2Token();
     $token->setAccessToken($data['access_token']);
     $token->setEndOfLife(StdOAuth2Token::EOL_NEVER_EXPIRES);
     unset($data['access_token']);
     $token->setExtraParams($data);
     return $token;
 }
Example #6
0
 /**
  * Create token object from array.
  *
  * @param array $data
  * @return TokenInterface
  */
 public function arrayToToken(array $data)
 {
     if ($data && array_key_exists('accessToken', $data) && array_key_exists('accessTokenSecret', $data) && array_key_exists('requestToken', $data) && array_key_exists('requestTokenSecret', $data) && array_key_exists('endOfLife', $data) && array_key_exists('extraParams', $data)) {
         $token = new StdOAuth1Token($data['accessToken']);
         $token->setAccessTokenSecret($data['accessTokenSecret']);
         $token->setRequestToken($data['requestToken']);
         $token->setRequestTokenSecret($data['requestTokenSecret']);
         $token->setEndOfLife($data['endOfLife']);
         $token->setExtraParams($data['extraParams']);
     } elseif ($data && array_key_exists('accessToken', $data) && array_key_exists('refreshToken', $data) && array_key_exists('endOfLife', $data) && array_key_exists('extraParams', $data)) {
         $token = new StdOAuth2Token($data['accessToken'], $data['refreshToken'], null, $data['extraParams']);
         $token->setEndOfLife($data['endOfLife']);
     }
     if (!isset($token) || !$token) {
         return null;
     }
     return $token;
 }
Example #7
0
 /**
  * {@inheritdoc}
  */
 protected function parseAccessTokenResponse($responseBody)
 {
     $data = json_decode($responseBody, true);
     if (is_null($data) || !is_array($data)) {
         throw new TokenResponseException('Unable to parse response.');
     } elseif (isset($data['error'])) {
         throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
     }
     $token = new StdOAuth2Token();
     $token->setAccessToken($data['access_token']);
     $token->setLifetime(0);
     if (isset($data['refresh_token'])) {
         $token->setRefreshToken($data['refresh_token']);
         unset($data['refresh_token']);
     }
     unset($data['access_token']);
     $token->setExtraParams($data);
     return $token;
 }
 public function event(Request $request, $id)
 {
     $user = User::find($id);
     $cache_key = 'event' . $user->id;
     $response = Cache::get($cache_key);
     if (!$response) {
         if (!$user->google_access_token) {
             return ['status' => 'no_token'];
         }
         $googleService = \OAuth::consumer('Google');
         $storage = $googleService->getStorage();
         $token = new StdOAuth2Token();
         $token->setAccessToken($user->google_access_token);
         $storage->storeAccessToken('Google', $token);
         $resultRaw = json_decode($googleService->request('https://www.googleapis.com/calendar/v3/calendars/primary/events?orderBy=startTime&singleEvents=true&timeMin=' . urlencode(date("Y-m-d\\TH:i:sP"))), true);
         $response = ['status' => 'ok', 'name' => $resultRaw['items'][0]['summary'], 'location' => $resultRaw['items'][0]['location'], 'start' => date("F j, g:i a", strtotime($resultRaw['items'][0]['start']['dateTime'])), 'end' => date("F j, g:i a", strtotime($resultRaw['items'][0]['end']['dateTime']))];
         Cache::put($cache_key, $response, 90);
     }
     return $response;
 }
Example #9
0
 /**
  * Parses the access token response and returns a TokenInterface.
  *
  *
  * @param string $responseBody
  *
  * @return TokenInterface
  *
  * @throws TokenResponseException
  */
 protected function parseAccessTokenResponse($responseBody)
 {
     $request = \Request::getInstance();
     if ($request->get('error')) {
         $reason = $request->get('error_description');
         throw new TokenResponseException($reason);
     }
     $data = json_decode($responseBody, true);
     if (null === $data || !is_array($data)) {
         throw new TokenResponseException('Unable to parse response.');
     } elseif (isset($data['error'])) {
         throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
     }
     $token = new StdOAuth2Token();
     $token->setAccessToken($data['access_token']);
     $token->setLifetime($data['expires_in']);
     if (isset($data['refresh_token'])) {
         $token->setRefreshToken($data['refresh_token']);
         unset($data['refresh_token']);
     }
     unset($data['access_token']);
     unset($data['expires_in']);
     $data['state'] = $request->get('state');
     $token->setExtraParams($data);
     return $token;
 }
Example #10
0
 protected function parseAccessTokenResponse($responseBody)
 {
     if (version_compare($this->apiVersion, 'v2.2', '>') === true) {
         $data = json_decode($responseBody, true);
     } else {
         parse_str($responseBody, $data);
     }
     if (null === $data || !is_array($data)) {
         throw new TokenResponseException('Unable to parse response.');
     } elseif (isset($data['error'])) {
         if (is_array($data['error']) === true) {
             $errorMessage = $data['error']['message'];
         } else {
             $errorMessage = $data['error'];
         }
         throw new TokenResponseException('Error in retrieving token: "' . $errorMessage . '"');
     }
     $token = new StdOAuth2Token();
     $token->setAccessToken($data['access_token']);
     if (isset($data['expires'])) {
         $token->setLifeTime($data['expires']);
     }
     if (isset($data['refresh_token'])) {
         $token->setRefreshToken($data['refresh_token']);
         unset($data['refresh_token']);
     }
     unset($data['access_token']);
     unset($data['expires']);
     $token->setExtraParams($data);
     return $token;
 }
Example #11
0
 public function refreshAccessToken()
 {
     $access_token = $this->access_token;
     $service = Facades\Social::service($this->provider);
     if (2 === Facades\Social::oauthSpec($this->provider)) {
         $token = new StdOAuth2Token();
         $token->setAccessToken(array_get($access_token, 'token'));
         $token->setRefreshToken(array_get($access_token, 'refresh_token'));
     } else {
         $token = new StdOAuth1Token();
         $token->setAccessToken(array_get($access_token, 'token'));
         $token->setAccessTokenSecret(array_get($access_token, 'secret'));
         $token->setRefreshToken(array_get($access_token, 'refresh_token'));
     }
     $service->getStorage()->storeAccessToken(ucfirst($this->provider), $token);
     try {
         $new_token = $service->refreshAccessToken($token);
     } catch (\Exception $e) {
         return false;
     }
     if (!$new_token->getAccessToken()) {
         return false;
     }
     $access_token['token'] = $new_token->getAccessToken();
     if ($new_token->getEndOfLife()) {
         $access_token['end_of_life'] = $new_token->getEndOfLife();
     }
     if ($new_token->getExtraParams()) {
         $access_token['extra_params'] = $new_token->getExtraParams();
     }
     if (2 !== Facades\Social::oauthSpec($this->provider) && $new_token->getAccessTokenSecret()) {
         $access_token['secret'] = $new_token->getAccessTokenSecret();
     }
     $this->access_token = $access_token;
     $this->save();
     return true;
 }
Example #12
0
 /**
  * {@inheritdoc}
  */
 protected function parseAccessTokenResponse($responseBody)
 {
     $data = json_decode($responseBody, true);
     if (null === $data || !is_array($data)) {
         throw new TokenResponseException('Unable to parse response.');
     } elseif (isset($data['error_description']) || isset($data['error'])) {
         throw new TokenResponseException(sprintf('Error in retrieving token: "%s"', isset($data['error_description']) ? $data['error_description'] : $data['error']));
     }
     $token = new StdOAuth2Token();
     $token->setAccessToken($data['access_token']);
     $token->setLifeTime($data['expires_in']);
     if (isset($data['refresh_token'])) {
         $token->setRefreshToken($data['refresh_token']);
         unset($data['refresh_token']);
     }
     unset($data['access_token']);
     unset($data['expires_in']);
     $token->setExtraParams($data);
     return $token;
 }
Example #13
0
 /**
  * {@inheritdoc}
  */
 protected function parseAccessTokenResponse($responseBody)
 {
     // Facebook gives us a query string ... Oh wait. JSON is too simple, understand ?
     parse_str($responseBody, $data);
     if (null === $data || !is_array($data)) {
         throw new TokenResponseException('Unable to parse response.');
     } elseif (isset($data['error'])) {
         throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
     }
     $token = new StdOAuth2Token();
     $token->setAccessToken($data['access_token']);
     $token->setLifeTime($data['expires']);
     if (isset($data['refresh_token'])) {
         $token->setRefreshToken($data['refresh_token']);
         unset($data['refresh_token']);
     }
     unset($data['access_token']);
     unset($data['expires']);
     $token->setExtraParams($data);
     return $token;
 }
Example #14
0
 /**
  * {@inheritdoc}
  */
 protected function parseAccessTokenResponse($responseBody)
 {
     $JSON = new \JSON(JSON_LOOSE_TYPE);
     $data = $JSON->decode($responseBody);
     if (null === $data || !is_array($data)) {
         throw new TokenResponseException('Unable to parse response.');
     } elseif (isset($data['error'])) {
         throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
     }
     $token = new StdOAuth2Token();
     $token->setAccessToken($data['access_token']);
     if (isset($data['expires'])) {
         $token->setLifeTime($data['expires']);
     }
     if (isset($data['refresh_token'])) {
         $token->setRefreshToken($data['refresh_token']);
         unset($data['refresh_token']);
     }
     unset($data['access_token']);
     unset($data['expires']);
     $token->setExtraParams($data);
     return $token;
 }
Example #15
0
<?php

use OAuth\Common\Consumer\Credentials;
use OAuth\OAuth2\Token\StdOAuth2Token;
require_once __DIR__ . '/../bootstrap.php';
$credentials = new Credentials($githubCredentials['key'], $githubCredentials['secret'], 'http://github.com');
$github = $serviceFactory->createService('GitHub', $credentials, $storage, array());
$token = new StdOAuth2Token();
$token->setAccessToken($githubCredentials['cli_token']);
$token->setEndOfLife(StdOAuth2Token::EOL_NEVER_EXPIRES);
$storage->storeAccessToken('GitHub', $token);
$stmt = $dbConnection->query('SELECT id, feed_id, repository FROM feeds_repositories ORDER BY id ASC');
$repositories = [];
$feeds = $stmt->fetchAll();
foreach ($feeds as $repository) {
    $repositories[] = $repository['repository'];
}
$repositories = array_unique($repositories);
$result = [];
foreach ($repositories as $repository) {
    $result[$repository] = json_decode($github->request('repos/' . $repository . '/releases'), true);
}
foreach ($feeds as $feed) {
    $timestamp = new \DateTime();
    foreach ($result[$feed['repository']] as $release) {
        if (postExists($dbConnection, $feed['feed_id'], $release['id'])) {
            continue;
        }
        $postId = addPost($dbConnection, $feed['id'], $release);
        logAddition($dbConnection, $feed['feed_id'], $postId, $timestamp);
    }
 /**
  * Get token from storage
  *
  * @param  string $provider
  * @param  int $key
  * @return Token
  */
 public function getToken($provider, $key)
 {
     $provider = ucfirst(strtolower($provider));
     $data = $this['option']->get('oauth:token:' . $provider . ':' . $key);
     if ($data && array_key_exists('accessToken', $data) && array_key_exists('accessTokenSecret', $data) && array_key_exists('requestToken', $data) && array_key_exists('requestTokenSecret', $data) && array_key_exists('endOfLife', $data) && array_key_exists('extraParams', $data)) {
         $token = new StdOAuth1Token($data['accessToken']);
         $token->setAccessTokenSecret($data['accessTokenSecret']);
         $token->setRequestToken($data['requestToken']);
         $token->setRequestTokenSecret($data['requestTokenSecret']);
         $token->setEndOfLife($data['endOfLife']);
         $token->setExtraParams($data['extraParams']);
     } elseif ($data && array_key_exists('accessToken', $data) && array_key_exists('refreshToken', $data) && array_key_exists('endOfLife', $data) && array_key_exists('extraParams', $data)) {
         $token = new StdOAuth2Token($data['accessToken'], $data['refreshToken'], null, $data['extraParams']);
         $token->setEndOfLife($data['endOfLife']);
     }
     if (!isset($token) || !$token) {
         return null;
     }
     return $token;
 }
Example #17
0
 /**
  * @param string $responseBody
  * @return StdOAuth2Token
  * @throws TokenResponseException
  */
 protected function parseAccessTokenResponse($responseBody)
 {
     $data = $this->service->parseAccessTokenResponse($responseBody);
     if (!isset($data) || !is_array($data)) {
         throw new TokenResponseException('Unable to parse response.');
     }
     $error = $this->service->getAccessTokenResponseError($data);
     if (isset($error)) {
         throw new TokenResponseException('Error in retrieving token: "' . $error . '"');
     }
     $token = new StdOAuth2Token();
     $names = $this->service->getAccessTokenArgumentNames();
     $token->setAccessToken($data[$names['access_token']]);
     unset($data[$names['access_token']]);
     if (isset($data[$names['expires_in']])) {
         $token->setLifeTime($data[$names['expires_in']]);
         unset($data[$names['expires_in']]);
     } else {
         $token->setLifetime($this->service->getTokenDefaultLifetime());
     }
     if (isset($data[$names['refresh_token']])) {
         $token->setRefreshToken($data[$names['refresh_token']]);
         unset($data[$names['refresh_token']]);
     }
     $token->setExtraParams($data);
     return $token;
 }
Example #18
0
 /**
  * {@inheritdoc}
  */
 protected function parseAccessTokenResponse($responseBody)
 {
     parse_str($responseBody, $data);
     if (null === $data || !is_array($data) || empty($data)) {
         throw new TokenResponseException('Unable to parse response.');
     } elseif (isset($data['error'])) {
         throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
     } elseif (isset($data['error_reason'])) {
         throw new TokenResponseException('Error in retrieving token: "' . $data['error_reason'] . '"');
     }
     $token = new StdOAuth2Token();
     $token->setAccessToken($data['access_token']);
     $token->setLifeTime($data['expires']);
     // I hope one day Deezer add a refresh token :)
     if (isset($data['refresh_token'])) {
         $token->setRefreshToken($data['refresh_token']);
         unset($data['refresh_token']);
     }
     unset($data['access_token']);
     unset($data['expires']);
     $token->setExtraParams($data);
     return $token;
 }
Example #19
0
 /**
  * {@inheritdoc}
  */
 protected function parseAccessTokenResponse($responseBody)
 {
     $data = @json_decode($responseBody, true);
     // Facebook gives us a query string on old api (v2.0)
     if (!$data) {
         parse_str($responseBody, $data);
     }
     if (null === $data || !is_array($data)) {
         throw new TokenResponseException('Unable to parse response.');
     } elseif (isset($data['error'])) {
         throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
     }
     $token = new StdOAuth2Token();
     $token->setAccessToken($data['access_token']);
     if (isset($data['expires'])) {
         $token->setLifeTime($data['expires']);
     }
     if (isset($data['refresh_token'])) {
         $token->setRefreshToken($data['refresh_token']);
         unset($data['refresh_token']);
     }
     unset($data['access_token']);
     unset($data['expires']);
     $token->setExtraParams($data);
     return $token;
 }
Example #20
0
 /** -----------------------------------------------------------------------
  * {@inheritdoc}
  */
 protected function parseAccessTokenResponse($responseBody)
 {
     $data = json_decode($responseBody, true);
     if ($data === null || !is_array($data)) {
         throw new TokenResponseException('Unable to parse response.');
     } elseif (isset($data['error'])) {
         $err = $data['error'];
         throw new TokenResponseException("Error in retrieving token: \"{$err}\"");
     }
     $token = new StdOAuth2Token($data['access_token'], null, $data['expires_in']);
     unset($data['access_token']);
     unset($data['expires_in']);
     $token->setExtraParams($data);
     return $token;
 }