/** * Get the session key between an application and the API server. * * @param unknown_type $api_key * @param unknown_type $secret_key * @param RingsideSocialSession $socialSession * @return string session key for the API container */ public static function getApiSessionKey($api_key, $secret_key, RingsideSocialSession $socialSession) { $uid = $socialSession->getUserId(); $sessionKey = $socialSession->getApiSessionKey($api_key); if ($sessionKey != null) { // Validate Session Key is still valid. $apiClient = new RingsideApiClientsRest($api_key, $secret_key, $sessionKey); $apiClient->setNetworkKey($socialSession->getNetwork()); try { $apiClient->users_getLoggedInUser(); } catch (Exception $e) { // error_log( "Session expired? " . $e->getMessage() ) ; // error_log($e->getTraceAsString()); $sessionKey = null; $socialSession->unsetApiSessionKey($api_key); } } if ($sessionKey == null && $uid != null) { // Need to simulate being app and auth, approve, get... which of course // TODO we need to re-think once we are working. // TODO catch some exceptions. try { // Configure where we get the URL for the REST SERVER from. $apiClient = new RingsideApiClientsRest($api_key, $secret_key, null, null, RingsideSocialConfig::$apiKey); // Once the client is authenticated with a session, the network key will be associated via the session $apiClient->setNetworkKey($socialSession->getNetwork()); $auth_token = $apiClient->auth_createToken($socialSession->getExpiry() == null ? true : false); $result = $apiClient->auth_approveToken($uid); $result = $apiClient->auth_getSession($auth_token); if (!empty($apiClient->session_key)) { $sessionKey = trim($apiClient->session_key); $socialSession->addApiSessionKey($api_key, $sessionKey); } } catch (Exception $exception) { error_log("Error creating session key " . $exception); } } return $sessionKey; }
/** * Enter description here... * * @param unknown_type $apiKey * @param unknown_type $secretKey * @param unknown_type $session * @param unknown_type $url * @param unknown_type $uid * @return unknown */ public function getRingsideRestClient($apiKey, $secretKey, $session, $url, $uid) { $apiClientSocial = new RingsideApiClientsRest($apiKey, $secretKey, null, null); $authToken = $apiClientSocial->auth_createToken(); $res = $apiClientSocial->auth_approveToken($uid); $result = $apiClientSocial->auth_getSession($authToken); if ($res["result"] == '1') { return $apiClientSocial; } return null; }
/** * Manufacure a Mock token for use with the test. * * @return unknown */ public function getToken() { $uid_ = '100000'; $vid_ = '100001'; $api_key_ = '4333592132647f39255bb066151a2099'; $api_secret_ = 'b37428ff3f4320a7af98b4eb84a4aa99'; $serverUrl = 'http://localhost:8080/restserver.php'; $app_client = new RingsideApiClientsRest($api_key_, $api_secret_, null, $serverUrl); $authToken = $app_client->auth_createToken(); $res = $app_client->auth_approveToken($uid_); $this->assertEquals("1", $res["result"]); $session_ = $app_client->auth_getSession($authToken); $methods = array(); $arguments = array(); $token = $this->getMock('RingsideGadgetToken'); //,$methods,$arguments $token->expects($this->any())->method('getAppClient')->will($this->returnValue($app_client)); $token->expects($this->any())->method('getAppId')->will($this->returnValue($api_key_)); $token->expects($this->any())->method('getDomain')->will($this->returnValue('ringside')); $token->expects($this->any())->method('getOwnerId')->will($this->returnValue($uid_)); $token->expects($this->any())->method('getViewerId')->will($this->returnValue($vid_)); $token->expects($this->any())->method('getAppUrl')->will($this->returnValue('http://localhost:8080/canvas.php/footprints')); $token->expects($this->any())->method('getModuleId')->will($this->returnValue('footprints')); return $token; }
/** * Handle the trust situation for a web application, this * will force redirects to canvas or callback url as specified. * * @param Application $application * @param int $uid * @param boolean $infinite * @param boolean $canvas * @param string next * @return boolean success failure */ function handleWebAppTrust($apikey, $application, $uid, $infinite, $canvas, $trust, $next) { $this->trustUser($application, $uid, $infinite); try { // Get a client which is represents this SOCIAL engine to API relationship $apiSessionKey = RingsideSocialUtils::getApiSessionKey(RingsideSocialConfig::$apiKey, RingsideSocialConfig::$secretKey, $application->getSocialSession()); $apiClientSocial = new RingsideApiClientsRest(RingsideSocialConfig::$apiKey, RingsideSocialConfig::$secretKey, $apiSessionKey, null, RingsideSocialConfig::$apiKey); // Get information about a given application. $result = $apiClientSocial->admin_getAppProperties("application_id,secret_key,canvas_url,callback_url", null, null, $apikey); $canvas_url = isset($result['canvas_url']) ? $result['canvas_url'] : ""; $callback_url = isset($result['callback_url']) ? $result['callback_url'] : ""; $secret = isset($result['secret_key']) ? $result['secret_key'] : ""; if ($canvas === true) { $trust_info = $apiClientSocial->admin_getTrustInfo(array(RingsideSocialConfig::$apiKey)); $canvas_root = $trust_info[0]['trust_canvas_url']; $this->redirect = $canvas_root . '/' . $canvas_url . $next; } else { // get the clients applications and create $appClient = new RingsideApiClientsRest($apikey, $secret, null, null, RingsideSocialConfig::$apiKey); $token = $appClient->auth_createToken($infinite); $appClient->auth_approveToken($uid); if (strpos($next, "?") === false) { $next = $next . "?"; } else { if (strpos($next, "&") !== false) { $next = $next . "&"; } } if ($trust === true) { $redir = $next . "auth_token=" . $token; } else { if (strpos($next, $callback_url) === 0) { $redir = $next . "auth_token=" . $token; } else { $redir = $callback_url . $next . "auth_token=" . $token; } } $this->redirect = $redir; } return true; } catch (Exception $e) { error_log($e->getMessage()); $this->removeTrust($application, $uid); return false; } }