*/ session_start(); require_once 'lib/oauth/OAuth.php'; require_once 'lib/lightopenid/openid.php'; // Full URL of the current application is running under. $scheme = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on') ? 'http' : 'https'; $selfUrl = "$scheme://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}"; /** * Wrapper class to make calls to the Chrome Web Store License Server. */ class LicenseServerClient { const LICENSE_SERVER_HOST = 'https://www.googleapis.com'; const CONSUMER_KEY = 'anonymous'; const CONSUMER_SECRET = 'anonymous'; const APP_ID = '1'; // Change to the correct id of your application. const TOKEN = '[REPLACE THIS WITH YOUR OAUTH TOKEN]'; const TOKEN_SECRET = '[REPLACE THIS WITH YOUR OAUTH TOKEN SECRET]'; public $consumer; public $token; public $signatureMethod; public function __construct() { $this->consumer = new OAuthConsumer( self::CONSUMER_KEY, self::CONSUMER_SECRET, NULL); $this->token = new OAuthToken(self::TOKEN, self::TOKEN_SECRET); $this->signatureMethod = new OAuthSignatureMethod_HMAC_SHA1(); } /** * Makes an HTTP GET request to the specified URL. * * @param string $url Full URL of the resource to access * @param string $request OAuthRequest containing the signed request to make. * @param array $extraHeaders (optional) Array of headers. * @param bool $returnResponseHeaders True if resp headers should be returned. * @return string Response body from the server. */ protected function send_signed_get($request, $extraHeaders=NULL, $returnRequestHeaders=false, $returnResponseHeaders=false) { $url = explode('?', $request->to_url()); $curl = curl_init($url[0]); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_FAILONERROR, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // Return request headers in the response. curl_setopt($curl, CURLINFO_HEADER_OUT, $returnRequestHeaders); // Return response headers in the response? if ($returnResponseHeaders) { curl_setopt($curl, CURLOPT_HEADER, true); } $headers = array($request->to_header()); if (is_array($extraHeaders)) { $headers = array_merge($headers, $extraHeaders); } curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); // Execute the request. If an error occurs fill the response body with it. $response = curl_exec($curl); if (!$response) { $response = curl_error($curl); } // Add server's response headers to our response body $response = curl_getinfo($curl, CURLINFO_HEADER_OUT) . $response; curl_close($curl); return $response; } public function checkLicense($userId) { $url = self::LICENSE_SERVER_HOST . '/chromewebstore/v1/licenses/' . self::APP_ID . '/' . urlencode($userId); $request = OAuthRequest::from_consumer_and_token( $this->consumer, $this->token, 'GET', $url, array()); $request->sign_request($this->signatureMethod, $this->consumer, $this->token); return $this->send_signed_get($request); } } try { $openid = new LightOpenID(); $userId = $openid->identity; if (!isset($_GET['openid_mode'])) { // This section performs the OpenID dance with the normal redirect. Use it // if you want an alternative to the popup UI. if (isset($_GET['login'])) { $openid->identity = 'https://www.google.com/accounts/o8/id'; $openid->required = array('namePerson/first', 'namePerson/last', 'contact/email'); header('Location: ' . $openid->authUrl()); } } else if ($_GET['openid_mode'] == 'cancel') { echo 'User has canceled authentication!'; } else { $userId = $openid->validate() ? $openid->identity : ''; $_SESSION['userId'] = $userId; $attributes = $openid->getAttributes(); $_SESSION['attributes'] = $attributes; } } catch(ErrorException $e) { echo $e->getMessage(); exit; } if (isset($_REQUEST['popup']) && !isset($_SESSION['redirect_to'])) { $_SESSION['redirect_to'] = $selfUrl; echo ''; exit; } else if (isset($_SESSION['redirect_to'])) { $redirect = $_SESSION['redirect_to']; unset($_SESSION['redirect_to']); header('Location: ' . $redirect); } else if (isset($_REQUEST['queryLicenseServer'])) { $ls = new LicenseServerClient(); echo $ls->checkLicense($_REQUEST['user_id']); exit; } else if (isset($_GET['logout'])) { unset($_SESSION['attributes']); unset($_SESSION['userId']); header('Location: ' . $selfUrl); } ?>