public function execute() { // TODO: This ONLY will work if API and Social tiers are co-located! $response = array(); // Finish the API session, because we need to start a social session session_regenerate_id(true); $_SESSION = array(); $network_session = new RingsideSocialSession(); $rest = RingsideSocialUtils::getAdminClient(); $session_key = $rest->auth_createAppSession($this->uid, RingsideSocialConfig::$apiKey, false); $network_session->addApiSessionKey(RingsideSocialConfig::$apiKey, $session_key); $network_session->setNetwork($this->user_network_key); //$network_session->addApiSessionKey($apiKey, $session_key); $network_session->setUserId($this->uid); // TODO: Do user identity mapping right now //$network_session->setPrincipalId($pid); //$network_session->setTrust($trust_key); //$network_session->setCallbackUrl($social_callback); $network_session->setLoggedIn(true); $response[self::RESPONSE_SOCIAL_SESSION]['session_id'] = $network_session->getSessionKey(); $response[self::RESPONSE_SOCIAL_SESSION]['initial_expiry'] = $network_session->getExpiry(); session_write_close(); return $response; }
/** * Builds a calling context to invoke the application's AJAX endpoint. * * @param array $request the request array * @param RingsideSocialSession $session the social session * @return RingsideSocialAppContext the context */ private static function buildCallContext($api_key, RingsideSocialSession $session) { $ctx = new RingsideSocialAppContext(); $ctx->setApiKey($api_key); $ctx->setIsAjax(1); $ctx->setFlavor('ajax'); // TODO: $ctx->setNetworkId(); $ctx->setSessionKey($session->getApiSessionKey($api_key)); $ctx->setExpires($session->getExpiry() == null ? 0 : $session->getExpiry()); $ctx->setNetworkId(RingsideSocialConfig::$apiKey); if ($session->isLoggedIn()) { // We only know these if the user is logged in $ctx->setUser($session->getUserId()); // TODO: Is App Added? $ctx->setIsAppAdded(1); } $ctx->setTime(microtime(true)); return $ctx; }
/** * 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; }
<html> <h1>Social Session Dump</h1> <label>User ID:</label><?php echo $network_session->getUserId(); ?> <br/> <label>Principal Id:</label><?php echo $network_session->getPrincipalId(); ?> <br/> <label>Trust:</label><?php echo $network_session->getTrust(); ?> <br/> <label>Expiry:</label><?php echo $network_session->getExpiry(); ?> <br/> <label>Network:</label><?php echo $network_session->getNetwork(); ?> <br/> <label>Session Key:</label><?php echo $network_session->getSessionKey(); ?> <br/> <label>Callback:</label><?php echo $network_session->getCallbackUrl(); ?> <br/> <label>Logged In?:</label><?php