/** * Makes the request to the server. * * @param string $server * @param string $service The rest service to access e.g. /connections/communities/all * @param string $method GET, POST or PUT * @param string $body * @param string $headers */ public function makeRequest($server, $service, $method, $options, $body = null, $headers = null, $endpointName = "connections") { $store = SBTCredentialStore::getInstance(); $settings = new SBTSettings(); $token = $store->getToken($endpointName); $response = null; $client = new Client($server); $client->setDefaultOption('verify', false); // If global username and password is set, then use it; otherwise use user-specific credentials if ($settings->getBasicAuthMethod($endpointName) == 'global') { $user = $settings->getBasicAuthUsername($endpointName); $password = $settings->getBasicAuthPassword($endpointName); } else { $user = $store->getBasicAuthUsername($endpointName); $password = $store->getBasicAuthPassword($endpointName); } try { $request = $client->createRequest($method, $service, $headers, $body, $options); if ($settings->forceSSLTrust($endpointName)) { $request->getCurlOptions()->set(CURLOPT_SSL_VERIFYHOST, false); $request->getCurlOptions()->set(CURLOPT_SSL_VERIFYPEER, false); } if ($method == 'POST' && isset($_FILES['file']['tmp_name'])) { $request->addPostFile('file', $_FILES['file']['tmp_name']); } $request->setAuth($user, $password); $response = $request->send(); } catch (Guzzle\Http\Exception\BadResponseException $e) { $response = $e->getResponse(); } return $response; }
/** * Callback for creating the plugin header. * * @param unknown $args */ function ibm_sbtk_header($args = array()) { $settings = new SBTSettings(); $store = SBTCredentialStore::getInstance(); $endpoints = $settings->getEndpoints(); if ($endpoints == null || empty($endpoints)) { return; } foreach ($endpoints as $endpoint) { if (($settings->getAuthenticationMethod($endpoint['name']) == 'oauth1' || $settings->getAuthenticationMethod($endpoint['name']) == 'oauth2') && $store->getOAuthAccessToken($endpoint['name']) == null && (!isset($_COOKIE['IBMSBTKOAuthLogin']) || $_COOKIE['IBMSBTKOAuthLogin'] != 'yes')) { $deploy_url = $settings->getSDKDeployURL($endpoint['name']); $authentication_method = $settings->getAuthenticationMethod($endpoint['name']); $js_library = $settings->getJSLibrary($endpoint['name']); $url = $settings->getURL($endpoint['name']); $name = $settings->getName($endpoint['name']); $api_version = $settings->getAPIVersion($endpoint['name']); $type = $settings->getServerType($endpoint['name']); $allow_client_access = $settings->allowClientAccess($endpoint['name']); $endpoints = $settings->getEndpoints(); // Load the header view require BASE_PATH . '/views/includes/header.php'; return; } } $plugin = new SBTPlugin($endpoints[0]['name']); $plugin->createHeader(); }
public function testInsert() { $store = SBTCredentialStore::getInstance($this->uid); $random = rand(); $random = $random . "str"; $store->storeBasicAuthUsername($random, $this->testEndpoint); $ret = $store->getBasicAuthUsername($this->testEndpoint); $this->assertEquals($ret, $random); $store->storeBasicAuthPassword($random, $this->testEndpoint); $ret = $store->getBasicAuthPassword($this->testEndpoint); $this->assertEquals($ret, $random); $store->storeOAuthAccessToken($random, $this->testEndpoint); $ret = $store->getOAuthAccessToken($this->testEndpoint); $this->assertEquals($ret, $random); $store->storeRequestToken($random, $this->testEndpoint); $ret = $store->getRequestToken($this->testEndpoint); $this->assertEquals($ret, $random); $store->storeToken($random, $this->testEndpoint); $ret = $store->getToken($this->testEndpoint); $this->assertEquals($ret, $random); $store->storeRequestToken($random, $this->testEndpoint); $ret = $store->getRequestToken($this->testEndpoint); $this->assertEquals($ret, $random); $store->storeTokenSecret($random, $this->testEndpoint); $ret = $store->getTokenSecret($this->testEndpoint); $this->assertEquals($ret, $random); $store->storeVerifierToken($random, $this->testEndpoint); $ret = $store->getVerifierToken($this->testEndpoint); $this->assertEquals($ret, $random); $store->storeTokenType($random, $this->testEndpoint); $ret = $store->getTokenType($this->testEndpoint); $this->assertEquals($ret, $random); $ret = $store->deleteBasicAuthCredentials($this->testEndpoint); $this->assertTrue($ret); $ret = $store->getBasicAuthUsername($this->testEndpoint); $this->assertEquals($ret, null); $ret = $store->deleteTokens($this->testEndpoint); $this->assertTrue($ret); $ret = $store->getToken($this->testEndpoint); $this->assertEquals($ret, null); $ret = $store->deleteOAuthCredentials($this->testEndpoint); $this->assertTrue($ret); $ret = $store->getOAuthAccessToken($this->testEndpoint); $this->assertEquals($ret, null); }
public function test_insert() { $store = SBTCredentialStore::getInstance($this->uid); $random = rand(); $random = $random . "str"; $store->storeBasicAuthUsername($random, $this->testEndpoint); $ret = $store->getBasicAuthUsername($this->testEndpoint); $this->assertEquals($ret, $random); $store->storeBasicAuthPassword($random, $this->testEndpoint); $ret = $store->getBasicAuthPassword($this->testEndpoint); $this->assertEquals($ret, $random); $store->storeOAuthAccessToken($random, $this->testEndpoint); $ret = $store->getOAuthAccessToken($this->testEndpoint); $this->assertEquals($ret, $random); $store->storeRequestToken($random, $this->testEndpoint); $ret = $store->getRequestToken($this->testEndpoint); $this->assertEquals($ret, $random); $store->storeToken($random, $this->testEndpoint); $ret = $store->getToken($this->testEndpoint); $this->assertEquals($ret, $random); }
public function makeRequest($method, $service, $header = array(), $body = null, $options = array()) { $settings = new SBTSettings(); $store = SBTCredentialStore::getInstance(); $server = $settings->getURL($this->endpointName); if ($settings->getAuthenticationMethod($this->endpointName) == "basic") { $endpoint = new SBTBasicAuthEndpoint(); } else { if ($settings->getAuthenticationMethod($this->endpointName) == "oauth2") { $endpoint = new SBTOAuth2Endpoint(); } else { if ($settings->getAuthenticationMethod($this->endpointName) == "oauth1") { $endpoint = new SBTOAuth1Endpoint(); } } } // Make request $response = $endpoint->makeRequest($settings->getURL($this->endpointName), $service, $method, $options, $body, $header, $this->endpointName); $this->document = $response->getBody(TRUE); $this->_parseXML(); return $this->document; }
/** * Tests the storage of the OAuth tokens. */ function test_oauth_token_storage() { $token = "HasdkHas7373&"; ibm_sbtk_activate_plugin(); if (!class_exists('SBTMemoryCookieAdapter')) { require BASE_PATH . '/core/models/SBTMemoryCookieAdapter.php'; } $mockAdapter = new SBTMemoryCookieAdapter(); $store = SBTCredentialStore::getInstance($mockAdapter); // Store tokens $store->storeOAuthAccessToken($token); $store->storeRequestToken($token); $store->storeToken($token); // Retrieve stored user credentials $retToken1 = $store->getOAuthAccessToken(); $retToken2 = $store->getRequestToken(); $retToken3 = $store->getToken(); // Check that stored tokens are the same than the original // token $this->assertEquals($token, $retToken1); $this->assertEquals($token, $retToken2); $this->assertEquals($token, $retToken3); }
/** * Routes requests. * * @param string server The URL of the server to which to re-direct the request to. Uses SBTSettings if none given. */ public function route($server = null) { $this->loadModel('SBTSettings'); $this->loadModel('SBTCredentialStore'); $proxyHelper = new SBTProxyHelper(); $store = SBTCredentialStore::getInstance(); $settings = new SBTSettings(); $endpointName = $proxyHelper->determineEndpoint(); if (!isset($_REQUEST["_redirectUrl"])) { // Request to check if the user is authenticated if (isset($_REQUEST["isAuthenticated"])) { $_REQUEST["_redirectUrl"] = '/files/basic/api/myuserlibrary/feed'; //used to be /connections/files/basic/api/myuserlibrary/feed $_SERVER['REQUEST_METHOD'] = 'GET'; } else { if (isset($_REQUEST["basicAuthLogout"])) { // Logout request $store->deleteBasicAuthCredentials($endpointName); } } if (isset($_REQUEST["OAuthLogout"])) { $store->deleteOAuthCredentials($endpointName); $timestamp = time(); unset($_COOKIE['IBMSBTKOAuthLogin']); setcookie('IBMSBTKOAuthLogin', "", $timestamp - 604800); return; } else { return; } if (isset($_REQUEST["basicAuthLogout"])) { return; } } // Handle any file operations // If file operations exist, then control flow // will be interrupted and route() will be called // again if ($this->fileOperations()) { return; } $url = $_REQUEST["_redirectUrl"]; $url = str_replace("/connections/", "", $url); if (isset($_REQUEST['basicAuthRequest']) && $_REQUEST['basicAuthRequest'] == 'true') { $store->storeBasicAuthUsername($_POST['username'], $endpointName); $store->storeBasicAuthPassword($_POST['password'], $endpointName); $result = array('status' => 200, 'result' => true); print_r(json_encode($result)); return; } $method = $_SERVER['REQUEST_METHOD']; $options = $proxyHelper->getOptions(); $response = null; $body = file_get_contents('php://input'); $endpoint = null; if ($server == null) { $server = $settings->getURL($endpointName); } $method = $_SERVER['REQUEST_METHOD']; $forwardHeader = $proxyHelper->getHeader($method); if ($settings->getAuthenticationMethod($endpointName) == "basic") { $endpoint = new SBTBasicAuthEndpoint(); } else { if ($settings->getAuthenticationMethod($endpointName) == "oauth2") { $endpoint = new SBTOAuth2Endpoint(); } else { if ($settings->getAuthenticationMethod($endpointName) == "oauth1") { $endpoint = new SBTOAuth1Endpoint(); } } } $url = $proxyHelper->cleanURL($url, $server); // Make request $response = $endpoint->makeRequest($server, $url, $method, $options, $body, $forwardHeader, $endpointName); // Print response $proxyHelper->outputResponse($response, $url); }
/** * Makes the request to the server. * * @param string $server * @param string $service The rest service to access e.g. /connections/communities/all * @param string $method GET, POST or PUT * @param string $body * @param string $headers */ public function makeRequest($server, $service, $method, $options, $body = null, $headers = null, $endpointName = "connections") { $store = SBTCredentialStore::getInstance(); $token = $store->getOAuthAccessToken($endpointName); $store = SBTCredentialStore::getInstance(); $settings = new SBTSettings(); $response = null; if ($options == null) { $options = array(); } try { $client = new Client($server); $request = $client->createRequest($method, $service, $headers, $body, $options); $request->addHeader('authorization', 'Bearer ' . $token); if ($settings->forceSSLTrust($endpointName)) { $request->getCurlOptions()->set(CURLOPT_SSL_VERIFYHOST, false); $request->getCurlOptions()->set(CURLOPT_SSL_VERIFYPEER, false); } if ($method == 'POST' && isset($_FILES['file']['tmp_name'])) { $request->addPostFile('file', $_FILES['file']['tmp_name']); } $response = $request->send(); } catch (Guzzle\Http\Exception\BadResponseException $e) { $response = $e->getResponse(); } return $response; }
<?php // Autoloader require_once '../autoload.php'; $store = SBTCredentialStore::getInstance(); $store->storeRequestToken("TEST 123"); echo "Stored<br/><br/><br/>"; echo $store->getRequestToken();
/** * Constructor. */ function __construct($endpointName = "connections") { $this->endpointName = $endpointName; $this->loadModel('SBTSettings'); $settings = new SBTSettings(); $authMethod = $settings->getAuthenticationMethod($endpointName); global $USER; if (isset($USER->id)) { setcookie('ibm-sbt-uid', $USER->id, time() + 604800); } if ($authMethod == 'oauth1') { // Check if we have an access token. If not, re-direct user to authentication page $this->loadModel('SBTCredentialStore'); $store = SBTCredentialStore::getInstance(); $token = $store->getRequestToken($endpointName); if ($token == null) { // Autoloader if (file_exists('../../../autoload.php')) { include_once '../../../autoload.php'; } else { if (function_exists('plugin_dir_path')) { $dir = plugin_dir_path(__FILE__); include_once $dir . '../../autoload.php'; } } if (file_exists(BASE_PATH . '/core/controllers/endpoint/SBTOAuth1Endpoint.php')) { include BASE_PATH . '/core/controllers/endpoint/SBTOAuth1Endpoint.php'; } // Create endpoint $oauth = new SBTOAuth1Endpoint(); // Send request to authenticate user (auth token is automatically being stored when callback method = authenticationCallback) // find out the domain: $domain = $_SERVER['HTTP_HOST']; // find out the path to the current file: $path = $_SERVER['SCRIPT_NAME']; // find out the QueryString: $queryString = $_SERVER['QUERY_STRING']; // put it all together: $protocol = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443 ? "https://" : "http://"; $url = $protocol . $domain . $path . "?" . $queryString; $body = null; if (strpos(BASE_LOCATION, 'core') !== FALSE) { $body = $oauth->request($url, BASE_LOCATION . '/index.php?plugin=guzzle&class=SBTOAuth1Endpoint&method=authenticationCallback', 'POST', $endpointName); } else { $body = $oauth->request($url, BASE_LOCATION . '/core/index.php?plugin=guzzle&class=SBTOAuth1Endpoint&method=authenticationCallback', 'POST', $endpointName); } var_dump($body); } } else { if ($authMethod == 'oauth2') { // Check if we have an access token. If not, re-direct user to authentication page $this->loadModel('SBTCredentialStore'); $store = SBTCredentialStore::getInstance(); $token = $store->getOAuthAccessToken($endpointName); if ($token == null) { // Autoloader if (file_exists('../../../autoload.php')) { include_once '../../../autoload.php'; } else { if (function_exists('plugin_dir_path')) { $dir = plugin_dir_path(__FILE__); include_once $dir . '../../autoload.php'; } } $parameters = array('response_type' => 'code', 'client_id' => $settings->getClientId($endpointName), 'callback_uri' => $settings->getOAuth2CallbackURL($endpointName)); $authURL = $settings->getAuthorizationURL($endpointName) . '?' . http_build_query($parameters, null, '&'); if (!headers_sent()) { header("Location: " . $authURL); } else { echo '<script type="text/javascript" language="javascript">window.location = "' . $authURL . '";</script>'; } } } } }
/** * Makes the request to the server. * * @param string $server * @param string $service The rest service to access e.g. /connections/communities/all * @param string $method GET, POST or PUT * @param string $body * @param string $headers */ public function makeRequest($server, $service, $method, $options, $body = null, $headers = null, $endpointName = "connections") { $store = SBTCredentialStore::getInstance(); $settings = new SBTSettings(); $random = mt_rand(0, 999999); $nonce = sha1($random); if ($store->getOAuthAccessToken($endpointName) == null) { $this->_getAccessToken($endpointName); } $url = $server . '/' . $service; $client = new Client($url); $client->setDefaultOption('verify', false); $options = array(); $response = null; try { $request = $client->createRequest($method, $url, $headers, $body, $options); $request->addHeader('Authorization', 'OAuth oauth_nonce="' . $nonce . '",oauth_version="1.0", oauth_timestamp="' . time() . '",oauth_signature="' . $settings->getConsumerSecret($endpointName) . '&' . $store->getTokenSecret($endpointName) . '",oauth_signature_method="PLAINTEXT",oauth_consumer_key="' . $settings->getConsumerKey($endpointName) . '",oauth_token="' . $store->getOAuthAccessToken($endpointName) . '"'); if ($settings->forceSSLTrust($endpointName)) { $request->getCurlOptions()->set(CURLOPT_SSL_VERIFYHOST, false); $request->getCurlOptions()->set(CURLOPT_SSL_VERIFYPEER, false); } if ($method == 'POST' && isset($_FILES['file']['tmp_name'])) { $request->addPostFile('file', $_FILES['file']['tmp_name']); } $response = $request->send(); } catch (Guzzle\Http\Exception\BadResponseException $e) { $response = $e->getResponse(); } return $response; }
/** * Prints the response from SmartCloud or IBM Connections on Premise. * * @param unknown $response */ public function outputResponse($response, $url) { $store = SBTCredentialStore::getInstance(); if ($response->getStatusCode() == 200) { if (isset($_REQUEST["isAuthenticated"]) && $settings->getAuthenticationMethod() == "basic") { $result = array('status' => $response->getStatusCode(), 'result' => $response->getStatusCode() == 401 ? false : true); print_r(json_encode($result)); } else { 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()); if (isset($_REQUEST['actionType']) && $_REQUEST['actionType'] == 'download' || strpos($url, '/media/') != false && strpos($url, '/document/') != false) { $headers = $response->getHeaders(); header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: ' . $headers['content-disposition']); header('Content-Transfer-Encoding: binary'); //changed to chunked header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); } print_r($response->getBody(TRUE)); } } else { if ($response->getStatusCode() == 302) { $headers = $response->getHeaders(); $this->route($headers['location']); } else { if ($response->getStatusCode() == 201) { $result = array('status' => 201, 'result' => true); print_r(json_encode($result)); } else { if ($response->getStatusCode() == 400) { echo "400 - Bad Request"; print_r($response->getBody(TRUE)); } else { if ($response->getStatusCode() == 401 || $response->getStatusCode() == '401oauth_token_expired') { if (isset($_GET['endpointName'])) { $store->deleteOAuthCredentials($_GET['endpointName']); $store->deleteBasicAuthCredentials($_GET['endpointName']); } else { $store->deleteOAuthCredentials(); $store->deleteBasicAuthCredentials(); } print_r($response->getStatusCode()); } else { print_r($response->getBody(TRUE)); } } } } } }
/** * Outputs the content of the widget. * * @param array $args * @param array $instance */ public function widget($args, $instance) { $settings = new SBTSettings(); $store = SBTCredentialStore::getInstance(); if (isset($instance['ibm-sbtk-endpoint'])) { $this->endpoint = $instance['ibm-sbtk-endpoint']; } else { $this->endpoint = "connections"; } if (!$this->_isUserLoggedIn()) { // Add && $settings->requireSignOn($this->endpoint) to enable both anon and non-anon access echo '<div class="widget-area" style="width: 100%;"><aside class="widget widget_recent_entries"><h3 class="widget-title">' . $this->widget_name . '</h3>'; echo '' . $GLOBALS[LANG]['must_login'] . '</aside></div>'; return; } // If tokens exist, make sure that they are valid. Otherwise clear the store and force the // user to re-log if (($settings->getAuthenticationMethod($this->endpoint) == 'oauth1' || $settings->getAuthenticationMethod($this->endpoint) == 'oauth2') && $store->getOAuthAccessToken($this->endpoint) != null) { $endpoint = null; if ($settings->getAuthenticationMethod($this->endpoint) == "oauth2") { $endpoint = new SBTOAuth2Endpoint(); } else { if ($settings->getAuthenticationMethod($this->endpoint) == "oauth1") { $endpoint = new SBTOAuth1Endpoint(); } } $service = '/files/basic/api/myuserlibrary/feed'; $response = $endpoint->makeRequest($settings->getURL($this->endpoint), $service, 'GET', array(), null, null, $this->endpoint); if ($response->getStatusCode() == 401) { $store->deleteOAuthCredentials($this->endpoint); setcookie('IBMSBTKOAuthLogin', "", $timestamp - 604800); require BASE_PATH . '/core/views/oauth-login-display.php'; } } else { $endpoint = new SBTBasicAuthEndpoint(); $service = '/files/basic/api/myuserlibrary/feed'; $response = $endpoint->makeRequest($settings->getURL($this->endpoint), $service, 'GET', array(), null, null, $this->endpoint); if ($response->getStatusCode() == 401) { // Delete old credentials. $store->deleteBasicAuthCredentials($this->endpoint); } } echo '<div name="ibm_sbtk_widget" class="widget-area" style="width:100%"><aside class="widget widget_recent_entries">'; echo '<h3 class="widget-title">' . $this->widget_name . '</h3>'; if (($settings->getAuthenticationMethod($this->endpoint) == 'oauth1' || $settings->getAuthenticationMethod($this->endpoint) == 'oauth2') && $store->getOAuthAccessToken($this->endpoint) == null && (!isset($_COOKIE['IBMSBTKOAuthLogin']) || $_COOKIE['IBMSBTKOAuthLogin'] != 'yes') && !$this->_isUserLoggedIn()) { require BASE_PATH . '/core/views/oauth-login-display.php'; echo '</aside></div>'; return; } $plugin = new SBTPlugin($this->endpoint); if ($settings->getAuthenticationMethod($this->endpoint) == 'basic' && $store->getBasicAuthUsername($this->endpoint) != null && $store->getBasicAuthPassword($this->endpoint) != null || $settings->getAuthenticationMethod($this->endpoint) == 'oauth1' && $store->getRequestToken($this->endpoint) != null || $settings->getAuthenticationMethod($this->endpoint) == 'basic' && $settings->getBasicAuthMethod($this->endpoint) == 'global' || $settings->getAuthenticationMethod($this->endpoint) == 'oauth2' && $store->getOAuthAccessToken($this->endpoint) != null) { require $this->widget_location; } if ($settings->getAuthenticationMethod($this->endpoint) == 'basic' && $settings->getBasicAuthMethod($this->endpoint) == 'prompt' && $store->getBasicAuthPassword($this->endpoint) == null) { require_once BASE_PATH . '/views/basic-auth-login-display.php'; } else { if ($settings->getAuthenticationMethod($this->endpoint) == 'oauth1' || $settings->getAuthenticationMethod($this->endpoint) == 'oauth2') { // require_once BASE_PATH . '/views/oauth-logout-display.php'; TODO: Uncomment when OAuth logout has been fixed } } echo '</aside></div>'; }