NetSuite Development Notes
Friday, February 15, 2019
PHP sample code to invoke ssoLogin and redirect the user to NetSuite Home Page
Create a combination of PHP and HTML code that contains text boxes. It will accept the token string, Netsuite Account Id and Partner ID and a SSO Login button
<html><head><title>SSO LOGIN</title></head><body><?phprequire_once '../PHPtoolkit.php';require_once 'login_info.php';global $myNSclient; if ($_POST["EUID"]!="" && $_POST["ECID"]!="") { $partnerId = $_POST["PID"]; $partnerAccount = $_POST["ECID"]; //Generate a timestamp of the Epoch time in milliseconds. SSO allows 15 min difference between the timestamp in the token and the time the request is processed $timestamp = round(vsprintf("%d%d",gettimeofday()),-3)/1000; //Generate a string token in the format <External Company ID> <space> <External User ID> <space> <Timestamp> $stringtoken = $_POST["ECID"]." ".$_POST["EUID"]." ".$timestamp; // Encrypt the string token $enctoken = encrypttoken ($stringtoken); // Hex encode the encrypted token so it can pass as a URL paramater $authToken = strtoupper(bin2hex($enctoken)); //Construct the SSO login URL with all the parameters $SSOURL = "https://system.netsuite.com/pages/partners/singlesignon.jsp?returnurl=".$_POST["ReturnURL"]."&pid=".$partnerId."&pacct=".$partnerAccount."&a=".$authToken; //Invoke ssoLogin and if successful redirect to the user NetSuite Home Page $ssoLoginResponse = $myNSclient->ssoLogin($partnerId,$authToken); if (!$ssoLoginResponse->isSuccess) { echo "<font color='red'><b>" . $ssoLoginResponse->statusDetail[0]->message . "</b></font>"; } else { -- echo("<script type="text/javascript">location.href='$SSOURL'</script>"); } } // This function will load the private key from a predefined path, encrypt a string passed as parameter and return the encrypted string function encrypttoken($token) { // Load the private key from a predefined path $priv_key_der = file_get_contents('C:\Documents and Settings\hguerrero\My Documents\NetSuite\SingleSignOn\SSO Working Sample\privkey.der'); $priv_key_pem = chunk_split(base64_encode($priv_key_der), 64, "\n"); $priv_key_pem = "-----BEGIN RSA PRIVATE KEY-----\n".$priv_key_pem."-----END RSA PRIVATE KEY-----\n"; //create a resourse from the key string $priv_res = openssl_pkey_get_private($priv_key_pem); $timestamp = round(vsprintf("%d%d",gettimeofday()),-3)/1000; $ssotoken = $_POST["ECID"]." ".$_POST["EUID"]." ".$timestamp; //Does not change anything if userId is numeric openssl_private_encrypt($ssotoken, $ssotokenEnc, $priv_res); return ($ssotokenEnc); }?><form name="GenerateToken" method="POST" target"_self"><table cellspacing='0' cellpadding='5' border='0' width='600'> <tr><td colspan="2"><u>Token components used by the external authentication mechanism:</u></td></tr><tr><td height='9'></td></tr> <tr><td align='left' valign='top' width='200'>External User ID:</td><td align='left' valign='top' width='400'><input type="text" name="EUID" id="EUID" value="<?php echo $_POST["EUID"]?>"/><?php if ($_POST["EUID"]!="") {} else {print (" Please enter a value!");}?></td></tr><tr><td height='9'></td></tr> <td align='left' valign='top' width='200'>External Company ID: </td><td align='left' valign='top' width='400'><input type="text" name="ECID" id="ECID" value="<?php echo $_POST["ECID"]?>" /><?php if ($_POST["ECID"]!="") {} else {print (" Please enter a value!");}?></td></tr><tr><td height='9'></td></tr> <td align='left' valign='top' width='200'>Partner ID: </td><td align='left' valign='top' width='400'><input type="text" name="PID" id="PID" value="<?php echo $_POST["PID"]?>" /></td></tr><tr><td height='9'></td></tr> <td align='left' valign='top' width='200'>Return URL: </td><td align='left' valign='top' width='400'><input type="text" name="ReturnURL" id="ReturnURL" value="http://www.google.com/" /></td></tr><tr><td height='9'></td></tr><tr><td height='9'></td></tr><tr><td height='5'> <input type="Button" name="GenToken" id="GenToken" value="SSO Login" onClick="document.GenerateToken.submit()"/></td></tr></table></form></body></html>
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment