/** * * @param string $consumerKey * @param string $consumerSecret * @param osapiStorage $storage * @param string $cookieKey * @param osapiProvider $provider * @param string $localUserId optional */ public function __construct($consumerKey, $consumerSecret, osapiStorage $storage, $cookieKey, osapiProvider $provider, $localUserId = null) { $this->storage = $storage; $this->storageKey = 'OAuth2u:' . $consumerKey . ':' . $localUserId; if (!($this->accessToken = $storage->get($this->storageKey))) { if (!isset($_COOKIE[$cookieKey])) { return; } $cookie = array(); parse_str($_COOKIE[$cookieKey], $cookie); setcookie($cookieKey, '', 0, '/'); if (isset($cookie['error'])) { throw new osapiException($cookie['error']); } if (!isset($cookie['access_token'])) { throw new osapiException('missing access token in cookie'); } if (isset($cookie['signature']) && isset($cookie['issued_at'])) { $this->verifySignature($cookie['signature'], $consumerSecret, $cookie['access_token'], $cookie['issued_at'], isset($cookie['user_id']) ? $cookie['user_id'] : '', ''); if (isset($cookie['userId'])) { $this->userId = $cookie['userId']; } } $this->accessToken = new OAuth2_Token($cookie['access_token'], null, null); unset($cookie['access_token']); unset($cookie['signature']); unset($cookie['issued_at']); foreach ($cookie as $key => $value) { $this->accessToken->{'set' . $key}($value); } $storage->set($this->storageKey, $this->accessToken); } }
/** * parse the response of an access token request and store it in dataStore * * @param OAuth2_HttpClient $http */ private function _parseAccessTokenResponse(OAuth2_HttpClient $http) { $headers = $http->getHeaders(); $type = 'text'; if (isset($headers['Content-Type']) && strpos($headers['Content-Type'], 'application/json') !== false) { $type = 'json'; } switch ($type) { case 'json': $response = json_decode($http->getResponse(), true); break; case 'text': default: $response = OAuth2_HttpClient::parseStringToArray($http->getResponse(), '&', '='); break; } if (isset($response['error'])) { throw new OAuth2_Exception('got error while requesting access token: ' . $response['error']); } if (!isset($response['access_token'])) { throw new OAuth2_Exception('no access_token found'); } $token = new OAuth2_Token($response['access_token'], isset($response['refresh_token']) ? $response['refresh_token'] : null, isset($response['expires_in']) ? $response['expires_in'] : null); unset($response['access_token']); unset($response['refresh_token']); unset($response['expires_in']); // add additional parameters which may be returned depending on service and scope foreach ($response as $key => $value) { $token->{'set' . $key}($value); } if (isset($_GET['platform'])) { $token->setplatform($_GET['platform']); } $this->_dataStore->set($this->_storageKey, $token); }
public function __construct($providerUrl, osapiStorage $storage, osapiHttpProvider $httpProvider = null) { $this->providerUrl = $providerUrl; $this->providerName = $this->providerUrl; if ($httpProvider) { $this->httpProvider = $httpProvider; } else { $this->httpProvider = new osapiCurlProvider(); } // See if we have any cached XRDS info so we can skip the http request. Cache time is currently hard-coded to 1 day if (($xrds = $storage->get($this->providerUrl.":xrds", 24 * 60 * 60)) !== false) { list($requestTokenUrl, $authorizeUrl, $accessTokenUrl, $restEndpoint, $rpcEndpoint, $this->providerName, $isOpenSocial) = $xrds; } else { // Start XRDS discovery $xrds = XrdsSimpleParser::doOAuthDiscovery($this->providerUrl, true, $this->httpProvider); // OAuth end-points $requestTokenUrl = $xrds['requestUrl']; $authorizeUrl = $xrds['authorizeUrl']; $accessTokenUrl = $xrds['accessUrl']; if (empty($requestTokenUrl) || empty($authorizeUrl) || empty($accessTokenUrl)) { throw new osapiException("Could not discover the required OAuth end-points"); } $rddXml = $xrds['rdd']; // PortableContacts end-point, optional $pocoUrl = XrdsSimpleParser::getServiceByType($rddXml, 'http://portablecontacts.net/spec/1.0'); if (empty($pocoUrl)) $pocoUrl = null; // These are not official end-point names, only partuza supports them currently, a proposal has been send to the spec list $restEndpoint = XrdsSimpleParser::getServiceByType($rddXml, 'http://ns.opensocial.org/rest/0.8'); $rpcEndpoint = XrdsSimpleParser::getServiceByType($rddXml, 'http://ns.opensocial.org/rpc/0.8'); if (empty($restEndpoint) && empty($rpcEndpoint)) { // no experimental simple end points found, try to find the rest base based on the people end-point $peopleEndpoint = XrdsSimpleParser::getServiceByType($rddXml, 'http://ns.opensocial.org/people/0.8'); $restEndpoint = str_replace('/people', '', $peopleEndpoint); } $isOpenSocial = true; if (empty($restEndpoint) && empty($rpcEndpoint) && empty($pocoUrl)) { throw new osapiException("No supported social end-points found"); } elseif (empty($restEndpoint) && empty($rpcEndpoint) && !empty($pocoUrl)) { $isOpenSocial = false; $restEndpoint = $pocoUrl; $rpcEndpoint = null; } // Store the results in cache so we can skip it next time $storage->set($this->providerUrl.":xrds", array((string)$requestTokenUrl, (string)$authorizeUrl, (string)$accessTokenUrl, (string)$restEndpoint, (string)$rpcEndpoint, (string)$this->providerName, (int)$isOpenSocial)); } // Construct our selves based on the XRDS discovered end-points parent::__construct($requestTokenUrl, $authorizeUrl, $accessTokenUrl, $restEndpoint, $rpcEndpoint, $this->providerName, $isOpenSocial); }
/** * */ public function storeCallbackUrl() { $this->storage->set($this->storageKey . ":originalUrl", 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); }