emulation-sample.php

--- run ---
<?php
 /*
   Swekey Emulated Authentication Sample
   (c) Musbe, Inc. 2012
   
   This is a very simple emulated authentication sample.
 */
 
 // The page is called by the swekey.com web page
 // In that case it should just return the value passed in the query string
 // (see Swekey_EnableMobileEmulation below)
 if (isset($_GET['xsite_scripting_result']))
 {
   if (get_magic_quotes_gpc())
     $_GET['xsite_scripting_result'] = stripslashes(@$_GET['xsite_scripting_result']);
   echo htmlentities (@$_GET['xsite_scripting_result']);
   exit;
 }
 
 
 
 // include the file enclosed in the swekey SDK
 include "swekey.php";
 
 ?>
 
 <HTML>
 <HEAD>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <TITLE>Swekey Php Authentication Sample</TITLE>
 
   <!-- include the file enclosed in the swekey SDK -->
   <script type="text/javascript" src="swekey.js"></script>  
 
   
 </HEAD>
 
 <body onload="OnPageLoaded()">
 
   <center>
   <h1>Swekey Php Emulated Authentication Sample<br><br><br></h1>
 
   
 <?php
    if (! isset($_GET['swekey_id']))
   {
     ?>
        No Swekey detected.
       <script type="text/javascript">
     
         var swekey_id = "";
 
         function OnPageLoaded() {
           // Enable mobile emulation
           Swekey_EnableMobileEmulation("http://developer.swekey.com/samples/emulation-sample.php?xsite_scripting_result=%RESULT%", true);
           CheckIfSwekeyWasPlugged();   
         }
     
         function CheckIfSwekeyWasPlugged() {
           var swekey_ids = Swekey_ListKeyIds();
           // in case of we have multiple swekey plugged we take only
           // the first one (32 = length of a swekey id)
           var swekey_id = swekey_ids.substr(0, 32);
           if (swekey_id.length == 32)  {
             window.location.search = "?swekey_id=" + swekey_id;
           } else {
             setTimeout("CheckIfSwekeyWasPlugged()", 2000);
           };
         }
       </script>  
     <?php
   }
   else if (strlen($_GET['otp']) != 64 || strlen($_GET['rt']) != 64)
   {
     echo 'Authenticating step 1...'.$_GET['swekey_id'];
     
     // we get the random token from the server
     $rt = Swekey_GetFastRndToken();
     ?>
       <script>
       
         var swekey_id = "";
 
         function OnPageLoaded() {
           // Enable mobile emulation (no need to refresh here)
           Swekey_EnableMobileEmulation("http://developer.swekey.com/samples/emulation-sample.php?xsite_scripting_result=%RESULT%", false);
       
           var swekey_ids = "<?php echo $_GET['swekey_id'] ?>";
 
           // we get the otp from the swekey
           // we MUST use the async version to support emulation
           Swekey_GetOtps(swekey_ids, "<?php echo $rt ?>", function(ids, rt, otps) {
             if (otps.length != 64) {
               window.location.search = ""; // swekey was unplugged we restart the demo
             } else {
               window.location.search = "?swekey_id=<?php echo $_GET['swekey_id'] ?>&rt=<?php echo $rt ?>&otp=" + otps;
             }
           });          
         }
 
   
       </script>
     <?php
   }
   else
   {        
     echo "<script> function OnPageLoaded() {} </script>";
 
     if (Swekey_CheckOtp($_GET['swekey_id'], $_GET['rt'], $_GET['otp']))
     {
       echo 'Swekey '.$_GET['swekey_id'].' is connected and validated';
     }
     else
     {
       echo 'Swekey '.$_GET['swekey_id'].' is connected but can not be validated because of error '.Swekey_GetLastError();
     }
   }
 ?>
 
  <br><br><br>
  <a href="./emulation-sample.php">retry</a>
   </body>
 </html>
 
--- run ---