Using SAML with SmartCloud for Social Business

SAML, Security Access Markup Language, provides an external application (e.g. a partner’s website) the ability to assert the identity of a SmartCloud user.  Prior to any interaction between a partner’s application and SmartCloud, a process is initiated by the IBM customer service team to enable SAML.  This entails sharing an SSL certificate from the partner’s application with SmartCloud.  By doing so, a partner’s application can now securely encrypt data that states the identity of a user.  In short, the partner’s application is saying, “This user is … Trust me.”  So long as SmartCloud can decrypt the sent data using the SSL certificate provided by the partner, it trusts the assertion of the user’s identity.

If this seems like the enterprisey concept of Single Sign-On, it is.  One can be authenticated on a partner’s application and be sent to SmartCloud without a need to re-login.  But the solution begets other advantages.

Consider what’s normally used to login to SmartCloud, your email address and password.  Those credentials may be different from legacy systems.  For example for many years communication services providers (CSPs) used a combination of user ID and PIN.  Having them now use an email address and password could be a problem for those that have been trained to use their ID/PIN combo.  To alleviate this concern, SAML can be used as the intermediary.  Using the existing ID/PIN pair in a partner’s login form, the partner can now associate ID 12345 with and provide the email identity to SmartCloud.  The only change needed is a new technical ability to map an existing ID to the respective SmartCloud email address.  To the user, nothing has changed.

SAML also provides a mechanism to specify where the user should go inside SmartCloud after login.  Again, our CSPs are heavily focused on Meetings. Rather than start your experience on the homepage (dashboard) in SmartCloud, you could be sent directly into hosting a meeting without a need to authenticate.

This now becomes a rather technical conversation.  First, it begins with the exchange of information – namely the SSL certificate – with the SmartCloud team.  See the technote Enabling Federated Identity or Integration Server for use with SmartCloud for Social Business for details.  Next we proceed to the following SAML workflow.

SAML ProcessIt begins with the user requesting authentication on the partner’s login form.  I’ve created the following form.  All it asks me to do is provide my email address – it’s a very trusting login form. The point is that the end result of the partner’s login process should produce the understanding that this user is Here I’m just asking for that understanding more directly (i.e. tell me who you are).

SAM FormNext, we must create the SAML assertion (an XML document) that contains the identity of the user and is signed accordingly.  For this demonstration, I’ve used an open source PHP module called xmlseclibs to complete the encryption.  Since the syntax and format of the XML document is very specific, I’ve created the following helper class.

class SmartCloudToken {
	private $key = NULL;
	private $cert = NULL;
	private $isFile = FALSE;
	public function __construct($key, $cert, $isFile = FALSE) {
		$this->key = $key;
		$this->cert = $cert;
		$this->isFile = $isFile;
	private function getTokenTimestamps() {
		$time = gmmktime();
		$instant = gmstrftime("%Y-%m-%dT%H:%M:%S.000Z", $time);
		// compute the time conditions under which the SAML token is valid
		$notBefore = gmstrftime("%Y-%m-%dT%H:%M:%S.000Z", ($time - (60 * SmartCloudToken::TOKEN_EXPIRATION_MINUTES)));
		$notAfter = gmstrftime("%Y-%m-%dT%H:%M:%S.000Z", ($time + (60 * SmartCloudToken::TOKEN_EXPIRATION_MINUTES)));
		return array("epoch"=>$time,
				"instant" => $instant,
				"notBefore" => $notBefore,
				"notAfter" => $notAfter);
	public function getToken($recipient, $issuer, $email) {
		$xml = $this->getTokenXML($recipient, $issuer, $email);
		return $xml->saveXML();
	private function getTokenXML($recipient, $issuer, $email) {
		$xml = <<<XML
<samlp:Response xmlns:samlp="urn:oasis:names:tc:SAML:1.0:protocol"
    ResponseID="Response-%RESPONSE_ID%" >
        <samlp:StatusCode Value="samlp:Success" />
        MinorVersion="1" >
            NotOnOrAfter="%NOT_AFTER%" >
            AuthenticationMethod="urn:oasis:names:tc:SAML:1.0:am:password" >
                <saml:NameIdentifier Format="urn:oasis:names:tc:SAML:1.0:assertion#emailAddress">%USER_NAME%</saml:NameIdentifier>
                <saml:NameIdentifier Format="urn:oasis:names:tc:SAML:1.0:assertion#emailAddress">%USER_NAME%</saml:NameIdentifier>
		$timestamps = $this->getTokenTimestamps();
		// replace the variable XML values with correct SAML values
		$computed = array($timestamps['instant'], $timestamps['epoch'], $recipient, $recipient, $issuer, $timestamps['epoch'], $timestamps['notBefore'], $timestamps['notAfter'], $timestamps['instant'], $email);
		$xml = str_replace($template, $computed, $xml);
		$doc = new DOMDocument();
		// the URI in the signed reference node must match the ResponseID value, which is set as the epoch above
		$this->sign($doc->documentElement, "ResponseID", "Response-" . $timestamps['epoch']);
		// unless configured by IBM, no need to sign the assertion
		//$this->sign($doc->getElementsByTagName("Assertion")->item(0), "AssertionID", "Assertion-" . $timestamps['epoch'], FALSE);
		return $doc;
	private function sign($node, $refId, $uri, $top = TRUE){
		$objDSig = new XMLSecurityDSig();
		$objDSig->addReference2($node, XMLSecurityDSig::SHA1, array(''), $refId, $uri);
		$objKey = new XMLSecurityKey(XMLSecurityKey::RSA_SHA1, array('type'=>'private'));
			$objKey->loadKey($this->key, TRUE);
		} else {
		} else {
		$objDSig->appendSignature($node, $top);

You may notice that I am invoking a forked function called addReference2. I found that I needed to make changes to the existing like-named functions within the XMLSecurityDSig class.

private function addRefInternal2($sinfoNode, $node, $algorithm, $arTransforms=NULL, $refId, $refURI) {
    	$refNode = $this->createNewSignNode('Reference');
    	if (! $node instanceof DOMDocument) {
    		$refNode->setAttribute("URI", '#'.$refURI);
    		$refNode->setAttribute("Id", $refId);
    	$transNodes = $this->createNewSignNode('Transforms');
    	if (is_array($arTransforms)) {
    		foreach ($arTransforms AS $transform) {
    			$transNode = $this->createNewSignNode('Transform');
    			if (is_array($transform) &&
    					(! empty($transform[''])) &&
    					(! empty($transform['']['query']))) {
    				$transNode->setAttribute('Algorithm', '');
    				$XPathNode = $this->createNewSignNode('XPath', $transform['']['query']);
    				if (! empty($transform['']['namespaces'])) {
    					foreach ($transform['']['namespaces'] AS $prefix => $namespace) {
    						$XPathNode->setAttributeNS("", "xmlns:$prefix", $namespace);
    			} else {
    				$transNode->setAttribute('Algorithm', $transform);
    	} elseif (! empty($this->canonicalMethod)) {
    		$transNode = $this->createNewSignNode('Transform');
    		$transNode->setAttribute('Algorithm', $this->canonicalMethod);
    	$canonicalData = $this->processTransforms($refNode, $node);
    	$digValue = $this->calculateDigest($algorithm, $canonicalData);
    	$digestMethod = $this->createNewSignNode('DigestMethod');
    	$digestMethod->setAttribute('Algorithm', $algorithm);
    	$digestValue = $this->createNewSignNode('DigestValue', $digValue);
    public function addReference2($node, $algorithm, $arTransforms=NULL, $refId, $refURI) {
    	if ($xpath = $this->getXPathObj()) {
    		$query = "./secdsig:SignedInfo";
    		$nodeset = $xpath->query($query, $this->sigNode);
    		if ($sInfo = $nodeset->item(0)) {
    			$this->addRefInternal2($sInfo, $node, $algorithm, $arTransforms, $refId, $refURI);

And now the helper class is called.

// get keys from plain-text
// require("./sample/SmartCloudKeys.php");
//$saml = new SmartCloudToken(SmartCloudKeys::PRIVATE_KEY, SmartCloudKeys::PUBLIC_KEY);
// get keys from PEM files
$saml = new SmartCloudToken("key_private.pem", "key_public.pem", TRUE);
// create a SAML token for the authenticated user
$token = $saml->getToken("", "", $user);

If you are wondering about the format of the PEM file, it’s the following (with some portion omitted).


This is a self-signed certificate for development purposes. It was created using openssl with the following command.

REM The following creates a self-signed certificate using openssl.
REM The resulting server key contains both the public and private
REM keys.

set openssl_exe=C:\Dev\eclipse-php\zend\Apache2\bin\openssl.exe
set openssl_conf=C:\Dev\eclipse-php\zend\Apache2\conf\openssl.cnf
set output_dir=C:\Dev\eclipse-php\zend\Apache2\htdocs\FIM\sps\SAML\saml11

%openssl_exe% req -config "%openssl_conf%" -new -x509 -days 1852 -nodes -sha1 -out "%output_dir%\server.pem" -keyout "%output_dir%\server.pem"

The above creates the PEM file with the contents of the public and private key. The IBM customer team recommends to send a password protected keystore that contains the certificate to be imported into SmartCloud. To do this, I created a keystore with ikeyman and then converted the PEM file into a p12 file. The resulting p12 file can be imported into a keystore using ikeyman.

REM The following exports a self-signed certificate using openssl into PKCS#8.
REM The resulting private key may be imported into a JKS keystore.

set openssl_exe=C:\Dev\eclipse-php\zend\Apache2\bin\openssl.exe
set output_dir=C:\Dev\eclipse-php\zend\Apache2\htdocs\FIM\sps\SAML\saml11

%openssl_exe% pkcs12 -export -inkey "%output_dir%\key_private.pem" -in "%output_dir%\key_public.pem" -out "%output_dir%\key_private.p12"

We must then POST the XML document to the SmartCloud server to begin the authorization process. To initiate the POST, I have added the XML document into another HTML form.

SAML FormBelow you’ll see the HTML for this login form.  Of importance is the TARGET, which is where we want the user to go after the login suceeds and the SAMLResponse, which is the base64 encoded XML document.

<script>document.cookie = "IV_JCT=%2FFIM; path=/";
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>SAML POST response</title>
	<h1>SmartCloud SAML Test Form</h1>
	<h2 style="display :<?php echo $tokenStyle ?>">Impersonating <?php echo $user; ?></h2>
	<form method="post"
		action="SAMLForm.php" style="display :<?php echo $userStyle ?>">
				<td><input name="User" size="80"
					type="text" />
		<button type="submit">POST</button>
	<form method="post"
		action="" style="display :<?php echo $tokenStyle ?>">
				<td>Target URL</td>
				<td><input name="TARGET" size="80"
					type="text" />
				<td>Raw Token</td>
				<td><textarea rows=20 cols=80>
						<?php echo htmlentities($token) ?>
				<td>Encoded Token</td>
				<td><textarea rows=20 cols=80 name="SAMLResponse">
						<?php echo base64_encode($token) ?>
		<button type="submit">POST</button>

Upon posting the SAML assertion, SmartCloud decrypts the XML document.  If the decryption succeeds, SmartCloud sets the user’s identity based on the email address in the XML and directs the user’s browser to the TARGET location.

The SAML process described requires in-depth review to understand fully. In addition to this post, I recommend the following document, which also describes the mechanics of SAML SmartCloud Federated ID.

Happy coding.

One thought on “Using SAML with SmartCloud for Social Business”

Leave a Reply

Your email address will not be published.