protected function getUserIdFromApi() { // Create a LinkedIn object $linkedInApiConfig = array('appKey' => LI_API_KEY, 'appSecret' => LI_SECRET, 'callbackUrl' => APP_URL . '/' . Content::l() . '/login/linkedincallback/' . (!empty($_GET['nextPage']) ? $_GET['nextPage'] : '')); $linkedIn = new LinkedIn($linkedInApiConfig); try { $response = $linkedIn->retrieveTokenAccess($_GET['oauth_token'], $_SESSION['oauth']['linkedin']['request']['oauth_token_secret'], $_GET['oauth_verifier']); } catch (Error $e) { Debug::l('Error. Could not retrieve LinkedIn access token. ' . $e); header('Location: ' . APP_URL . '/' . Content::l() . '/login/linkedin/'); exit; } if ($response['success'] === TRUE) { // The request went through without an error, gather user's access tokens $_SESSION['oauth']['linkedin']['access'] = $response['linkedin']; // Set the user as authorized for future quick reference $_SESSION['oauth']['linkedin']['authorized'] = true; } else { $this->exitWithMessage('Error. The OAuth access token was not retrieved. ' . print_r($response, 1)); } $this->accessToken = serialize($response['linkedin']); /* Retrieve the user ID The XML response will look like one of these: <person> <id>8GhzNjjaOi</id> </person> <error> <status>401</status> <timestamp>1288518358054</timestamp> <error-code>0</error-code> <message>[unauthorized]. The token used in the OAuth request is not valid.</message> </error> */ try { $response = $linkedIn->profile('~:(id,first-name,last-name)'); if ($response['success'] === TRUE) { $response['linkedin'] = new SimpleXMLElement($response['linkedin']); if ($response['linkedin']->getName() != 'person') { Debug::l('Error. Could not retrieve person data from LinkedIn. ' . print_r($response, 1)); header('Location: ' . APP_URL . '/' . Content::l() . '/login/linkedin/'); exit; } } else { Debug::l('Error. Could not retrieve person data from LinkedIn. ' . print_r($response, 1)); header('Location: ' . APP_URL . '/' . Content::l() . '/login/linkedin/'); exit; } $this->linkedInId = (string) $response['linkedin']->id; $this->name = $response['linkedin']->{'first-name'} . ' ' . $response['linkedin']->{'last-name'}; } catch (Error $e) { Debug::l('Error. Could not retrieve person ID from LinkedIn. ' . $e); header('Location: ' . APP_URL . '/' . Content::l() . '/login/linkedin/'); exit; } }
function try_li_connect() { global $_SESSION; // start the session if (!session_id()) { bb_die('This script requires session support, which appears to be disabled according to session_start().'); } // check for cURL if (extension_loaded('curl')) { $curl_version = curl_version(); $curl_version = $curl_version['version']; } else { bb_die('You must load the cURL extension to use this library.'); } $API_CONFIG = array('appKey' => bb_get_option('li_app_id'), 'appSecret' => bb_get_option('li_secret'), 'callbackUrl' => NULL); $API_CONFIG['callbackUrl'] = 'http://' . $_SERVER['SERVER_NAME'] . ($_SERVER['SERVER_PORT'] != PORT_HTTP || $_SERVER['SERVER_PORT'] != PORT_HTTP_SSL ? ':' . $_SERVER['SERVER_PORT'] : '') . $_SERVER['PHP_SELF'] . '?' . LINKEDIN::_GET_TYPE . '=initiate&' . LINKEDIN::_GET_RESPONSE . '=1'; $OBJ_linkedin = new LinkedIn($API_CONFIG); try { // perform linkedin rest authorization $_GET[LINKEDIN::_GET_RESPONSE] = isset($_GET[LINKEDIN::_GET_RESPONSE]) ? $_GET[LINKEDIN::_GET_RESPONSE] : ''; if (!$_GET[LINKEDIN::_GET_RESPONSE]) { // LinkedIn hasn't sent us a response, the user is initiating the connection // send a request for a LinkedIn access token $response = $OBJ_linkedin->retrieveTokenRequest(); if ($response['success'] === TRUE) { // store the request token $_SESSION['oauth']['linkedin']['request'] = $response['linkedin']; // redirect the user to the LinkedIn authentication/authorisation page to initiate validation. header('Location: ' . LINKEDIN::_URL_AUTH . $response['linkedin']['oauth_token']); exit; } else { // bad token request bb_die("unable to connect to LinkedIn"); } } else { // LinkedIn has sent a response, user has granted permission, // take the temp access token, the user's secret and the verifier to request the user's real secret key $response = $OBJ_linkedin->retrieveTokenAccess($_SESSION['oauth']['linkedin']['request']['oauth_token'], $_SESSION['oauth']['linkedin']['request']['oauth_token_secret'], $_GET['oauth_verifier']); if ($response['success'] === TRUE) { // the request went through without an error, gather user's 'access' tokens $_SESSION['oauth']['linkedin']['access'] = $response['linkedin']; // set the user as authorized for future quick reference $_SESSION['oauth']['linkedin']['authorized'] = TRUE; //bb_die($_SESSION['oauth']['linkedin']['request']['oauth_token'] //.' '.$_SESSION['oauth']['linkedin']['request']['oauth_token_secret'] //.' '.$_GET['oauth_verifier']); } else { // bad token access bb_die("authorization failed"); } } } catch (LinkedInException $e) { error_log($e); } return; }
/** * {@inheritdoc} */ function loginFinish() { $oauth_token = $_REQUEST['oauth_token']; $oauth_verifier = $_REQUEST['oauth_verifier']; if (!$oauth_verifier) { throw new Exception("Authentication failed! {$this->providerId} returned an invalid Token.", 5); } $response = $this->api->retrieveTokenAccess($oauth_token, $this->token("oauth_token_secret"), $oauth_verifier); if (isset($response['success']) && $response['success'] === true) { $this->deleteToken("oauth_token"); $this->deleteToken("oauth_token_secret"); $this->token("access_token_linkedin", $response['linkedin']); $this->token("access_token", $response['linkedin']['oauth_token']); $this->token("access_token_secret", $response['linkedin']['oauth_token_secret']); // set user as logged in $this->setUserConnected(); } else { throw new Exception("Authentication failed! {$this->providerId} returned an invalid Token.", 5); } }
public function getAccess($token, $secret, $verifier) { $access = parent::retrieveTokenAccess($token, $secret, $verifier); if (isset($access['linkedin']['oauth_problem'])) { return false; } $obj = new stdClass(); $obj->token = $access['linkedin']['oauth_token']; $obj->secret = $access['linkedin']['oauth_token_secret']; $obj->params = ''; //@todo: expiry return $obj; }
/** * {@inheritdoc} */ public function loginFinish() { // in case we get oauth_problem=user_refused if (isset($_REQUEST['oauth_problem']) && $_REQUEST['oauth_problem'] == "user_refused") { throw new Exception("Authentication failed! The user denied your request.", 5); } $oauth_token = isset($_REQUEST['oauth_token']) ? $_REQUEST['oauth_token'] : null; $oauth_verifier = isset($_REQUEST['oauth_verifier']) ? $_REQUEST['oauth_verifier'] : null; if (!$oauth_token || !$oauth_verifier) { throw new Exception("Authentication failed! {$this->providerId} returned an invalid Token.", 5); } $response = $this->api->retrieveTokenAccess($oauth_token, $this->token("oauth_token_secret"), $oauth_verifier); if (isset($response['success']) && $response['success'] === true) { $this->deleteToken("oauth_token"); $this->deleteToken("oauth_token_secret"); $this->token("access_token_linkedin", $response['linkedin']); $this->token("access_token", $response['linkedin']['oauth_token']); $this->token("access_token_secret", $response['linkedin']['oauth_token_secret']); // set user as logged in $this->setUserConnected(); } else { throw new Exception("Authentication failed! {$this->providerId} returned an invalid Token in response: " . Hybrid_Logger::dumpData($response), 5); } }
/** * Exchanges the request token with the access token * * @since 4.0 * @access public * @param string * @return */ public function getAccess() { // Get the verifier codes $verifier = $this->getVerifier(); $access = parent::retrieveTokenAccess($this->request_token, $this->request_secret, $verifier); if (isset($access['linkedin']['oauth_problem'])) { return false; } $obj = new stdClass(); $obj->token = $access['linkedin']['oauth_token']; $obj->secret = $access['linkedin']['oauth_token_secret']; $obj->params = ''; $obj->expires = EB::date(); // If the expiry date is given if (isset($access['linkedin']['oauth_expires_in'])) { $expires = $access['linkedin']['oauth_expires_in']; // Set the expiry date with proper date data $obj->expires = EB::date(strtotime('now') + $expires)->toSql(); } return $obj; }
/** * this function used to get user access token after authentication * @author Ahmed <*****@*****.**> */ public function linkedInCallBackAction() { //get the request object $request = $this->getRequest(); //get the session object $session = $request->getSession(); //get the translator object $translator = $this->get('translator'); //linkedIn config parameters $config = array('appKey' => $this->container->getParameter('linkedin_api_key'), 'appSecret' => $this->container->getParameter('linkedin_secret_key'), 'callbackUrl' => ''); //create new linkedIn oauth object $oauth = new \LinkedIn($config); //get user access token $access_token = $oauth->retrieveTokenAccess($request->get('oauth_token'), $session->get('oauth_token_secret'), $request->get('oauth_verifier')); /* If HTTP response is 200 continue otherwise send to connect page to retry */ if (200 == $access_token['info']['http_code']) { /* The user has been verified store the data in the session */ $session->set('oauth_linkedin', $access_token['linkedin']); $session->set('oauth_token', $access_token['linkedin']['oauth_token']); $session->set('oauth_token_secret', $access_token['linkedin']['oauth_token_secret']); //check if this is a popup if ($session->get('linkedInPopup', FALSE)) { //remove the flag $session->remove('linkedInPopup'); //redirect the parent window and then close the popup return new Response(' <script> window.opener.top.location.href = "' . $this->generateUrl($session->get('callbackUrl'), array(), TRUE) . '"; self.close(); </script> '); } //redirect the user to linkedInUserDataAction te get user data return $this->redirect($this->generateUrl($session->get('callbackUrl'), array(), TRUE)); } else { //something went wrong go to connect page again $session->clear(); return new Response($translator->trans('linkedIn connection error') . ' <a href="' . $this->generateUrl('linkedInButton', array('callbackUrl' => 'linkedInCallBack'), TRUE) . '">' . $translator->trans('try again') . '</a>'); } }
/** * * * @return array */ function linkedin() { App::import("Vendor", "Users.linkedin/linkedin_3.2.0.class"); $response = array(); $user_profile = array(); $ldnToken = ''; $ldnSecret = ''; $API_CONFIG = array('appKey' => LINKEDIN_API_KEY, 'appSecret' => LINKEDIN_SECRET_KEY, 'callbackUrl' => NULL); if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') { $protocol = 'https'; } else { $protocol = 'http'; } $API_CONFIG['callbackUrl'] = Router::url(array('plugin' => 'users', 'controller' => 'users', 'action' => 'login', 'linkedin'), true) . '?' . LINKEDIN::_GET_TYPE . '=initiate&' . LINKEDIN::_GET_RESPONSE . '=1'; $OBJ_linkedin = new LinkedIn($API_CONFIG); // check for response from LinkedIn $_GET[LINKEDIN::_GET_RESPONSE] = isset($_GET[LINKEDIN::_GET_RESPONSE]) ? $_GET[LINKEDIN::_GET_RESPONSE] : ''; if (!$_GET[LINKEDIN::_GET_RESPONSE]) { // LinkedIn hasn't sent us a response, the user is initiating the connection // send a request for a LinkedIn access token $response = $OBJ_linkedin->retrieveTokenRequest(); if ($response['success'] === TRUE) { // store the request token $_SESSION['oauth']['linkedin']['request'] = $response['linkedin']; // redirect the user to the LinkedIn authentication/authorisation page to initiate validation. $response['url'] = LINKEDIN::_URL_AUTH . $response['linkedin']['oauth_token']; } else { // bad token request $response['Request_Token_Failed_Response'] = $response; $response['Request_Token_Failed_Linkedin'] = $OBJ_linkedin; } } else { // LinkedIn has sent a response, user has granted permission, take the temp access token, the user's secret and the verifier to request the user's real secret key $response = $OBJ_linkedin->retrieveTokenAccess($_SESSION['oauth']['linkedin']['request']['oauth_token'], $_SESSION['oauth']['linkedin']['request']['oauth_token_secret'], $_GET['oauth_verifier']); if ($response['success'] === TRUE) { // the request went through without an error, gather user's 'access' tokens $_SESSION['oauth']['linkedin']['access'] = $response['linkedin']; // set the user as authorized for future quick reference $_SESSION['oauth']['linkedin']['authorized'] = TRUE; // redirect the user back to the demo page //header('Location: ' . $_SERVER['PHP_SELF']); $response = $OBJ_linkedin->profile('~:(id,first-name,last-name,picture-url)'); if ($response['success'] === TRUE) { $user_profile = new SimpleXMLElement($response['linkedin']); $ldnSecret = $_SESSION['oauth']['linkedin']['request']['oauth_token_secret']; $ldnToken = $_SESSION['oauth']['linkedin']['request']['oauth_token']; } else { // request failed $user_profile = ''; $ldnSecret = $_SESSION['oauth']['linkedin']['request']['oauth_token_secret']; $ldnToken = $_SESSION['oauth']['linkedin']['request']['oauth_token']; } } else { // bad token access $response['Request_Token_Failed_Response'] = $response; $response['Request_Token_Failed_Linkedin'] = $OBJ_linkedin; } } $response['user_profile'] = $user_profile; return $response; }
/** * Similar to onAuthenticate, except we already have a logged in user, we're just linking accounts * * @param array $options * @return void */ public function link($options = array()) { $jsession = App::get('session'); // Set up linkedin configuration $linkedin_config['appKey'] = $this->params->get('api_key'); $linkedin_config['appSecret'] = $this->params->get('app_secret'); // Create Object $linkedin_client = new LinkedIn($linkedin_config); if (!Request::getVar('oauth_verifier', NULL)) { // User didn't authorize our app, or, clicked cancel App::redirect(Route::url('index.php?option=com_members&id=' . User::get('id') . '&active=account'), Lang::txt('PLG_AUTHENTICATION_LINKEDIN_MUST_AUTHORIZE_TO_LOGIN', App::get('sitename')), 'error'); } // LinkedIn has sent a response, user has granted permission, take the temp access token, // the user's secret and the verifier to request the user's real secret key $request = $jsession->get('linkedin.oauth.request'); $reply = $linkedin_client->retrieveTokenAccess($request['oauth_token'], $request['oauth_token_secret'], Request::getVar('oauth_verifier')); if ($reply['success'] === TRUE) { // The request went through without an error, gather user's 'access' tokens $jsession->set('linkedin.oauth.access', $reply['linkedin']); // Set the user as authorized for future quick reference $jsession->set('linkedin.oauth.authorized', TRUE); } else { return new Exception(Lang::txt('Access token retrieval failed'), 500); } if ($jsession->get('linkedin.oauth.authorized') == TRUE) { $linkedin_client->setTokenAccess($jsession->get('linkedin.oauth.access')); // Get the linked in profile $profile = $linkedin_client->profile('~:(id,first-name,last-name,email-address)'); $profile = $profile['linkedin']; // Parse the profile XML $profile = new SimpleXMLElement($profile); // Get the profile values $li_id = $profile->{'id'}; $username = (string) $li_id; // (make sure this is unique) $hzad = \Hubzero\Auth\Domain::getInstance('authentication', 'linkedin', ''); // Create the link if (\Hubzero\Auth\Link::getInstance($hzad->id, $username)) { // This linkedin account is already linked to another hub account App::redirect(Route::url('index.php?option=com_members&id=' . User::get('id') . '&active=account'), Lang::txt('PLG_AUTHENTICATION_LINKEDIN_ACCOUNT_ALREADY_LINKED'), 'error'); } else { $hzal = \Hubzero\Auth\Link::find_or_create('authentication', 'linkedin', null, $username); $hzal->user_id = User::get('id'); $hzal->email = (string) $profile->{'email-address'}; $hzal->update(); } } else { // User didn't authorize our app, or, clicked cancel App::redirect(Route::url('index.php?option=com_members&id=' . User::get('id') . '&active=account'), Lang::txt('PLG_AUTHENTICATION_LINKEDIN_MUST_AUTHORIZE_TO_LINK', Config::get('sitename')), 'error'); } }
function getLinkedinProfile($req_type) { $session = LinkedinHelper::oauth_session_exists(); $app = JFactory::getApplication(); // include the LinkedIn class require_once 'linkedin_3.1.1.class.php'; // display constants $API_CONFIG = array('appKey' => 'r7xckk3gxdfg', 'appSecret' => 'fJQh57Ex2Qg7YyPi', 'callbackUrl' => NULL); define('CONNECTION_COUNT', 20); define('PORT_HTTP', '80'); define('PORT_HTTP_SSL', '443'); define('UPDATE_COUNT', 10); // set index //$_REQUEST[LINKEDIN::_GET_TYPE] = (isset($_REQUEST[LINKEDIN::_GET_TYPE])) ? $_REQUEST[LINKEDIN::_GET_TYPE] : ''; switch ($req_type) { case 'initiate': /** * Handle user initiated LinkedIn connection, create the LinkedIn object. */ // check for the correct http protocol (i.e. is this script being served via http or https) if (isset($_SERVER['HTTPS'])) { if ($_SERVER['HTTPS'] == 'on') { $protocol = 'https'; } else { $protocol = 'http'; } } else { $protocol = 'http'; } // set the callback url $API_CONFIG['callbackUrl'] = $protocol . '://' . $_SERVER['SERVER_NAME'] . ($_SERVER['SERVER_PORT'] != PORT_HTTP || $_SERVER['SERVER_PORT'] != PORT_HTTP_SSL ? ':' . $_SERVER['SERVER_PORT'] : '') . $_SERVER['PHP_SELF'] . '?' . $req_type . '=initiate&' . LINKEDIN::_GET_RESPONSE . '=1'; // $API_CONFIG['callbackUrl'] = JRoute::_('index.php?option=com_jobboard&view=user') . '&' . $req_type . '=initiate&' . LINKEDIN::_GET_RESPONSE . '=1'; // echo $API_CONFIG['callbackUrl'];die; $OBJ_linkedin = new LinkedIn($API_CONFIG); // check for response from LinkedIn $_GET[LINKEDIN::_GET_RESPONSE] = isset($_GET[LINKEDIN::_GET_RESPONSE]) ? $_GET[LINKEDIN::_GET_RESPONSE] : ''; if (!$_GET[LINKEDIN::_GET_RESPONSE]) { // LinkedIn hasn't sent us a response, the user is initiating the connection // send a request for a LinkedIn access token $response = $OBJ_linkedin->retrieveTokenRequest(); //echo 'response: '.'<pre>'.print_r($response, true).'</pre>'; if ($response['success'] === TRUE) { // store the request token $session_oauth = $session->get('oauth'); $session_oauth['oauth']['linkedin']['request'] = $response['linkedin']; $session->set('oauth', $session_oauth); //redirect the user to the LinkedIn authentication/authorisation page to initiate validation. $app->redirect(LINKEDIN::_URL_AUTH . $response['linkedin']['oauth_token']); } else { $profile_assoc = array(); $profile_assoc['response'] = false; $profile_assoc['msg'] = "Request token retrieval failed:<br /><br />RESPONSE:<br /><br /><pre>"; return $profile_assoc; // bad token request } } else { // LinkedIn has sent a response // user has cancelled LinkedIn authentication if (JRequest::getString('oauth_problem') == 'user_refused') { $app->redirect('index.php?option=com_jobboard&view=user&task=addcv', JText::_('COM_JOBBOARD_IMPORTLINKEDINCANCELLED')); } // user has granted permission, take the temp access token, the user's secret and the verifier to request the user's real secret key $session_oauth = $session->get('oauth'); $response = $OBJ_linkedin->retrieveTokenAccess($session_oauth['oauth']['linkedin']['request']['oauth_token'], $session_oauth['oauth']['linkedin']['request']['oauth_token_secret'], $_GET['oauth_verifier']); if ($response['success'] === TRUE) { // the request went through without an error, gather user's 'access' tokens $session_oauth['oauth']['linkedin']['access'] = $response['linkedin']; // set the user as authorized for future quick reference $session_oauth['oauth']['linkedin']['authorized'] = TRUE; $session->set('oauth', $session_oauth); } else { // bad token access echo "Access token retrieval failed:<br /><br />RESPONSE:<br /><br /><pre>" . print_r($response, TRUE) . "</pre><br /><br />LINKEDIN OBJ:<br /><br /><pre>" . print_r($OBJ_linkedin, TRUE) . "</pre>"; } } $response = $OBJ_linkedin->profile('~:(id,first-name,last-name,summary,educations,positions,skills)'); if ($response['success'] === TRUE) { $profile = new SimpleXMLElement($response['linkedin']); $profile_json = json_encode($profile); $profile_assoc = array_unique(json_decode($profile_json, TRUE), SORT_REGULAR); $profile_assoc['response'] = $response['success']; } else { // profile retrieval failed $profile_assoc = array(); $profile_assoc['response'] = false; } return $profile_assoc; break; case 'revoke': /** * Handle authorization revocation. */ $session_oauth = $session->get('oauth'); $OBJ_linkedin = new LinkedIn($API_CONFIG); $OBJ_linkedin->setTokenAccess($session_oauth['oauth']['linkedin']['access']); $response = $OBJ_linkedin->revoke(); if ($response['success'] === TRUE) { // revocation successful, clear session if ($session->clear('oauth')) { // session destroyed //header('Location: ' . $_SERVER['PHP_SELF']); } else { // session not destroyed echo "Error clearing user's session"; } } else { // revocation failed echo "Error revoking user's token:<br /><br />RESPONSE:<br /><br /><pre>" . print_r($response, TRUE) . "</pre><br /><br />LINKEDIN OBJ:<br /><br /><pre>" . print_r($OBJ_linkedin, TRUE) . "</pre>"; } break; default: // nothing being passed back, display demo page // check PHP version if (version_compare(PHP_VERSION, '5.0.0', '<')) { throw new LinkedInException('You must be running version 5.x or greater of PHP to use this library.'); } // check for cURL if (extension_loaded('curl')) { $curl_version = curl_version(); $curl_version = $curl_version['version']; } else { throw new LinkedInException('You must load the cURL extension to use this library.'); } $session_oauth = $session->get('oauth'); if (!isset($session_oauth['oauth']['linkedin']['authorized'])) { $session_oauth['oauth']['linkedin']['authorized'] = FALSE; $session->set('oauth', $session_oauth); } if ($session_oauth['oauth']['linkedin']['authorized'] === TRUE) { // user is already connected $OBJ_linkedin = new LinkedIn($API_CONFIG); $OBJ_linkedin->setTokenAccess($session_oauth['oauth']['linkedin']['access']); ?> <?php //$response = $OBJ_linkedin->profile('~:(id,first-name,last-name,picture-url)'); $response = $OBJ_linkedin->profile('~:(id,first-name,last-name,summary,educations,positions,skills)'); if ($response['success'] === TRUE) { //$response['linkedin'] = new SimpleXMLElement($response['linkedin']); // echo "<pre>" . print_r($response['linkedin'], TRUE) . "</pre>"; $profile = new SimpleXMLElement($response['linkedin']); $profile_json = json_encode($profile); return json_decode($profile_json, TRUE); } else { // profile retrieval failed echo "Error retrieving profile information:<br /><br />RESPONSE:<br /><br /><pre>" . print_r($response) . "</pre>"; } } else { // user isn't connected ?> <?php } ?> <?php break; } }