Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support to add SAML token in message and attach pointer to the signature #57

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 23 additions & 24 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
{
"name" : "robrichards/wse-php",
"description" : "Libraries for adding WS-* support to ext/soap in PHP.",
"authors" : [{
"name" : "Rob Richards",
"homepage" : "http://www.cdatazone.org/",
"role" : "Main developer"
}
],
"license" : "BSD-3-Clause",
"keywords" : [
"SOAP",
"WS-Addressing",
"WS-Security"
],
"homepage" : "https://github.com/robrichards/wse-php",
"autoload" : {
"psr-4" : {
"RobRichards\\WsePhp\\" : "src"
}
},
"require" : {
"php" : ">= 5.3",
"robrichards/xmlseclibs" : ">=3.0.4"
}
"name": "robrichards/wse-php",
"description": "Libraries for adding WS-* support to ext/soap in PHP.",
"authors": [{
"name": "Rob Richards",
"homepage": "http://www.cdatazone.org/",
"role": "Main developer"
}],
"license": "BSD-3-Clause",
"keywords": [
"SOAP",
"WS-Addressing",
"WS-Security"
],
"homepage": "https://github.com/robrichards/wse-php",
"autoload": {
"psr-4": {
"RobRichards\\WsePhp\\": "src"
}
},
"require": {
"php": ">= 5.3",
"robrichards/xmlseclibs": ">=3.0.4"
}
}
66 changes: 66 additions & 0 deletions examples/soap-wsse-saml-example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
require __DIR__ . '/../vendor/autoload.php';

use RobRichards\WsePhp\WSSESoap;
use RobRichards\XMLSecLibs\XMLSecurityKey;

define('PRIVATE_KEY', 'priv_key.pem');
define('CERT_FILE', 'pub_key.pem');
define('SERVICE_CERT', 'sitekey_pub.cer');

class MySoap extends SoapClient
{
/**
* @var string
*/
public $samlToken;


public function __doRequest($request, $location, $saction, $version)
{
$doc = new DOMDocument('1.0');
$doc->loadXML($request);

$objWSSE = new WSSESoap($doc);

/* add Timestamp with no expiration timestamp */
$objWSSE->signAllHeaders = true;
$objWSSE->signBody = true;
$objWSSE->addTimestamp();

/* create new XMLSec Key using AES256_CBC and type is private key */
$objKey = new XMLSecurityKey(XMLSecurityKey::RSA_SHA1, array('type' => 'private'));

/* load the private key from file - last arg is bool if key in file (true) or is string (false) */
$objKey->loadKey(PRIVATE_KEY, true);

/* Sign the message - also signs appropiate WS-Security items */
$options = array("insertBefore" => false);
$objWSSE->signSoapDoc($objKey, $options);

/* Add SAML Token to the message */
$token = $objWSSE->addSamlToken($this->samlToken);

/* Attach pointer to Signature */
$objWSSE->attachTokentoSig($token, true);

$request = $objWSSE->saveXML();

$retVal = parent::__doRequest($request, $location, $saction, $version);

return $retVal;
}
}

$wsdl = '<wsdl location>';

$sc = new MySoap($wsdl);
$sc->samlToken = '<SAML_TOKEN>';

try {
$out = $sc->callmethod(1);
var_dump($out);
} catch (SoapFault $fault) {
var_dump($fault);
}

23 changes: 19 additions & 4 deletions src/WSSESoap.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,16 @@ public function addBinaryToken($cert, $isPEMFormat = true, $isDSig = true)
return $token;
}

public function attachTokentoSig($token)
public function addSamlToken($samlToken)
{
$token = dom_import_simplexml(simplexml_load_string($samlToken));
$token = $this->soapDoc->importNode($token, true);
$security = $this->locateSecurityHeader();
$token = $security->insertBefore($token, $security->firstChild);
return $token;
}

public function attachTokentoSig($token, $isSamlToken = false)
{
if (!($token instanceof DOMElement)) {
throw new Exception('Invalid parameter: BinarySecurityToken element expected');
Expand All @@ -209,9 +218,15 @@ public function attachTokentoSig($token)

$tokenRef = $this->soapDoc->createElementNS(self::WSSENS, self::WSSEPFX.':SecurityTokenReference');
$keyInfo->appendChild($tokenRef);
$reference = $this->soapDoc->createElementNS(self::WSSENS, self::WSSEPFX.':Reference');
$reference->setAttribute('ValueType', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3');
$reference->setAttribute('URI', $tokenURI);

if ($isSamlToken) {
$reference = $this->soapDoc->createElementNS(self::WSSENS, self::WSSEPFX.':KeyIdentifier', $token->getAttribute('AssertionID'));
$reference->setAttribute('ValueType', 'http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.0#SAMLAssertionID');
} else {
$reference = $this->soapDoc->createElementNS(self::WSSENS, self::WSSEPFX.':Reference');
$reference->setAttribute('ValueType', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3');
$reference->setAttribute('URI', $tokenURI);
}
$tokenRef->appendChild($reference);
} else {
throw new Exception('Unable to locate digital signature');
Expand Down