Home > Technical Support > LiveAgent API > REST API > Examples > Knowledgebase single sign-on generic example

Knowledgebase single sign-on generic example

In this example we'll show you how you can implement simple single sign-on (SSO) for any kind of PHP-based webpage/application. As a result, any user of your web-page/app can access LiveAgent KnowledgeBase directly through a link without going through the generic login process.
 
Goal:
Users of php-based webpage/app can visit KnowledgeBase directly through a link without going through the generic login process.
 
Requirements:
- Users in php-based page/webapp must have the same e-mail address as their username in LiveAgent
- Customers in LiveAgent must be REGISTERED!
 
Case 1
php-based app/webapage is on different domain than LiveAgent KnowledgeBase.
 
Example php-based app domain and path: http://host.lc/www/sso_example/index.php
Example LiveAgent domain and path: http://support.lc/www/git/LiveAgent/LiveAgent/server
 
 
 
 
The code
Here we have the code of our simple php-based webapp. It supports login/logout actions and have one information line about logged-in user and link to LiveAgent knowledgeBase (KB).
This is the content of index.php file:
 
<?php
        //web-app simple login system
session_start();
$action = @strtolower($_GET['action']);
$loggedIn = isset($_SESSION['userEmail']) && $_SESSION['userEmail'] != null; 
        //
$liveAgentUrl = 'http://support.lc/www/git/LiveAgent/LiveAgent/server/'; //put your LiveAgent domain/path here
$liveAgentTrackUrl = $liveAgentUrl . 'scripts/track.js'; //this file is in every LiveAgent installation by default
$liveAgentApiKey = '681e1492ba24cf5ef92aacef31f1befc'; //your LiveAgent API key

function createMagicHash($userEmail, $userAuthToken, $apiKey)
        {
            return md5($userEmail . $userAuthToken . $apiKey);
        }
        
    function getVisitorInfo($liveAgentUrl, $visitorEmail, $apiKey)
        {
            $ch = curl_init($liveAgentUrl . 'api/?handler=' . 'customers/' . $visitorEmail . '&apikey=' . $apiKey);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $rawResult = curl_exec($ch);
            if (!$rawResult) {
                return null;
            }
            $result = json_decode($rawResult);
            if (!isset($result->response)) {
                return null;
            }
            if (isset($result->response) && isset($result->response->statuscode) && $result->response->statuscode != 0) {
                return null;
            }
            return $result->response;
        }
?>
<html>
<head>
</head>
<body>
<h1>Hello</h1>
<?php 
echo '<a href="'.$liveAgentUrl.'" target="_blank">visit knowledgebase</a>';
echo '<script type="text/javascript" id="la_x2s6df8d" src="'.$liveAgentTrackUrl.'"></script>';
if(!$loggedIn && $action=='login' && @$_POST['userEmail'] != null) {
$_SESSION['userEmail'] = $_POST['userEmail'];
$loggedIn = true;
$customerInfo = getVisitorInfo($liveAgentUrl, $_SESSION['userEmail'], $liveAgentApiKey);
$hash = createMagicHash($_SESSION['userEmail'], $customerInfo->authtoken, $liveAgentApiKey);
echo '<script type="text/javascript">LiveAgentTracker.loginUserOnServer(\'' . $hash . '\');</script>';
}
if($loggedIn && $action=='logout' && $_SESSION['userEmail'] != null) {
$customerInfo = getVisitorInfo($liveAgentUrl, $_SESSION['userEmail'], $liveAgentApiKey);
$hash = createMagicHash($_SESSION['userEmail'], $customerInfo->authtoken, $liveAgentApiKey);
echo '<script type="text/javascript">document.onload = (function() { LiveAgentTracker.logoutUserOnServer(\'' . $hash . '\');})();</script>'; 
$_SESSION['userEmail'] = null;
$loggedIn = false;
}
?>
<?php
if(!$loggedIn) {
echo '<p>Not logged in.</p>';
echo '<form action="index.php?action=login" method="post">';
echo '<p>Login as customer&nbsp;&nbsp;<input name="userEmail" placeholder="Customer email"></input></p>';
echo '</form>';
} ?>
<?php 
if($loggedIn) {
echo '<p>Logged in as '.$_SESSION['userEmail'].' - <a href="index.php?action=logout">logout</a></p>';
} ?>
</body>
</html>
 
Workflow
The webapp page should look like this
 
Now if you blink into LiveAgent KB page you'll see that nobody is logged in
 
 
To continue, make sure you have at least one registered customer in your LiveAgent. If not, scroll down this page for instructions how to create one.
 
Now we'll be working with our example customer example@customer.com. Let's try to log him in by typing his email into one textbox of our webapp and hitting enter to submit.
 
  .....and after submit....... 
 
 
Now you can click on KB link and you'll be redirected to LiveAgent domain and automatically logged in.
 
As you can see from the screenshot - we're logged in as "John Doe". Now you can check your tickets by clicking on "My tickets" link.
Note: logout from LiveAgent KB won't affect your online status in php-based web app.
 
Now if you get back into the app and hit "logout" link you'll be logged out from web-app and from LiveAgent KB too.
 
The magic
So how does it work? It is pretty simple. LiveAgent uses cookies on client side to store customer authentication info. Let's have a look how cookies look like when nobody is logged-in in the KB.
 
 
We have 4 default cookies the LiveAgent Knowledgebase uses for storing various application and customer-based info. Now login somebody into the Knowledgebase and check the cookies again:
 
 
New cookie is there: V_auth. And also content of visitor_la_sid is now different and holds the  auth token of logged in user. To logout, you need to delete these two cookies or set their validity date to the past. LiveAgent uses a second option to handle user logout.
To make this work from different a domain we use a little help from our LiveAgent javascript tracking library. In this lib we have implemented methods for cross-domain login/logout. From the code above you can recognize the proper usage.
 
 
Case 2
php-based app/webapge is on the same domain as the Liveagent KnowledgeBase.
Note: this case applies also when web-app is on a different sub-domain than LiveAgent KnowledgeBase. Example webapp.example.com and suport.example.com.
 
Example php-based app domain and path: http://support.lc/www/sso_example/index.php
Example LiveAgent domain and path: http://support.lc/www/git/LiveAgent/LiveAgent/server
 
 
The code
Here we have the code of our simple php-based webapp. It supports login/logout actions and have one information line about logged-in user and link to LiveAgent KnowledgeBase (KB).
This is content of index.php file:
<?php
session_start();
$action = @strtolower($_GET['action']);
$loggedIn = isset($_SESSION['userEmail']) && $_SESSION['userEmail'] != null;
$liveAgentUrl = 'http://support.lc/www/git/LiveAgent/LiveAgent/server/';
$liveAgentApiKey = '681e1492ba24cf5ef92aacef31f1befc';

    function getVisitorInfo($liveAgentUrl, $visitorEmail, $apiKey)
        {
            $ch = curl_init($liveAgentUrl . 'api/?handler=' . 'customers/' . $visitorEmail . '&apikey=' . $apiKey);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $rawResult = curl_exec($ch);
            if (!$rawResult) {
                return null;
            }
            $result = json_decode($rawResult);
            if (!isset($result->response)) {
                return null;
            }
            if (isset($result->response) && isset($result->response->statuscode) && $result->response->statuscode != 0) {
                return null;
            }
            return $result->response;
        }
?>
<html>
<head>
</head>
<body>
<h1>Hello</h1>
<?php 
echo '<a href="'.$liveAgentUrl.'" target="_blank">visit knowledgebase</a>';
if(!$loggedIn && $action=='login' && @$_POST['userEmail'] != null) {
$_SESSION['userEmail'] = $_POST['userEmail'];
$loggedIn = true;
$customerInfo = getVisitorInfo($liveAgentUrl, $_SESSION['userEmail'], $liveAgentApiKey);
setcookie($customerInfo->browsercookiename, $customerInfo->authtoken, time() + 60 * 60 * 24 * 356, '/');

}
if($loggedIn && $action=='logout' && $_SESSION['userEmail'] != null) {
$customerInfo = getVisitorInfo($liveAgentUrl, $_SESSION['userEmail'], $liveAgentApiKey);
setcookie($customerInfo->browsercookiename, '', time() - 3600, '/');
                setcookie('visitor_la_sid', '', time() - 3600, '/');
$_SESSION['userEmail'] = null;
$loggedIn = false;
}
?>
<?php
if(!$loggedIn) {
echo '<p>Not logged in.</p>';
echo '<form action="index.php?action=login" method="post">';
echo '<p>Login as customer&nbsp;&nbsp;<input name="userEmail" placeholder="Customer email"></input></p>';
echo '</form>';
} ?>
<?php 
if($loggedIn) {
echo '<p>Logged in as '.$_SESSION['userEmail'].' - <a href="index.php?action=logout">logout</a></p>';
} ?>
</body>
</html>
 
Differences from case 1 are:
  • we don't need to include track.js script into html body
  • we don't need to call JS code, just save cookies
  • this method can be safely used also in pure JavaScript (application can be in different code than PHP)
In general the principle is the same - using cookies. But here we can create them directly because we're on the same domain. 
 
How to create a registered customer example
Open your LiveAgent KB page:
 
Now click on "Login" link and then on "Register" link in the popup dialog
 
Now fill in a customer email address and name and hit "Register" button
Note: you need to enter valid email address that you have chance to check for new emails later. Otherwise you wont be able to confirm the registration by the link delivered to the email address you entered
 ..........after clicking on "Register"......... 
 
Now check the inbox of the email address you entered as customer email addres. New registration email should appear there. Now click the registration link which brings you back into LiveAgent KB. You'll be asked for a new password.
Enter a new password and press the "Login" button. Now you have a new registered customer for testing purposes.
 
 
 
 

Ready to try LiveAgent?

It's free for the first 14 days! No credit card required.

Get Started
We work well with others...
Magento Joomla Wordpress Mailchimp
Contact us

support@ladesk.com

+1-800-811-6590 (Toll Free in USA & Canada)

+421 2 33 456 826 (European Union & Worldwide)

Quality Unit, LLC 616 Corporate Way, Suite 2-3278 Valley Cottage, NY 10989

Stay in touch
Blog Google+
© 2004-2015 QualityUnit.com, All rights reserved