/** * {@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; }
/** * Parses the access token response and returns a TokenInterface. * * * @param string $responseBody * * @return TokenInterface * * @throws TokenResponseException */ 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']); $token->setEndOfLife(StdOAuth2Token::EOL_NEVER_EXPIRES); unset($data['access_token']); $token->setExtraParams($data); return $token; }
/** * 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; }
/** * 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; }
<?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); }