Ejemplo n.º 1
0
 /**
  * The callback function for authenticating the user and then storing the token in the CredentialStore (no content
  * is being requested).
  */
 public function authenticationCallback()
 {
     if (!isset($_GET['code'])) {
         return;
     }
     $store = SBTCredentialStore::getInstance();
     $settings = new SBTSettings();
     $endpointName = "connections";
     if (isset($_GET['endpointName'])) {
         $endpointName = $_GET['endpointName'];
     }
     $parameters = array('callback_uri' => $settings->getOAuth2CallbackURL($endpointName), 'code' => $_GET['code'], 'grant_type' => 'authorization_code', 'client_id' => $settings->getClientId($endpointName), 'client_secret' => $settings->getClientSecret($endpointName));
     $tokenURL = $settings->getAccessTokenURL($endpointName) . '?' . http_build_query($parameters, null, '&');
     $client = new Client($tokenURL);
     $client->setDefaultOption('verify', false);
     $headers = null;
     $body = null;
     $options = array();
     $response = null;
     try {
         $request = $client->createRequest('GET', $tokenURL, $headers, $body, $options);
         if ($settings->forceSSLTrust($endpointName)) {
             $request->getCurlOptions()->set(CURLOPT_SSL_VERIFYHOST, false);
             $request->getCurlOptions()->set(CURLOPT_SSL_VERIFYPEER, false);
         }
         $response = $request->send();
         foreach ($response->getHeaderLines() as $h) {
             if (strpos($h, "Content-Type") === 0) {
                 header($h, TRUE);
             }
         }
         header(':', true, $response->getStatusCode());
         header('X-PHP-Response-Code: ' . $response->getStatusCode(), true, $response->getStatusCode());
         parse_str($response->getBody(TRUE), $info);
         if (!isset($info['access_token'])) {
             die('Missing access token. Something went wrong - make sure that your client ID and client secret are correct and try again.');
         }
         $accessToken = $store->getOAuthAccessToken($endpointName);
         if ($accessToken == null || $accessToken == "") {
             $store->storeOAuthAccessToken($info['access_token'], $endpointName);
         }
         header("Location: " . $settings->getOAuthOrigin($endpointName));
     } catch (Guzzle\Http\Exception\BadResponseException $e) {
         $response = $e->getResponse();
         print_r($response->getBody(TRUE));
     }
 }