Skip to content

Commit

Permalink
Merge pull request #95 from JohnMaguire/test-soap-binding
Browse files Browse the repository at this point in the history
Add tests for SOAP SAML binding
  • Loading branch information
thijskh authored May 26, 2017
2 parents 8651bdf + a14962f commit 3c5f58c
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/SAML2/SOAP.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,8 @@
*/
class SOAP extends Binding
{
/**
* Send a SAML 2 message using the SOAP binding.
*
* Note: This function never returns.
*
* @param \SAML2\Message $message The message we should send.
*/
public function send(Message $message)
public function getOutputToSend(Message $message)
{
header('Content-Type: text/xml', true);
$outputFromIdp = '<?xml version="1.0" encoding="UTF-8"?>';
$outputFromIdp .= '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">';
$outputFromIdp .= '<SOAP-ENV:Body>';
Expand All @@ -28,7 +20,21 @@ public function send(Message $message)
$outputFromIdp .= $tempOutputFromIdp;
$outputFromIdp .= '</SOAP-ENV:Body>';
$outputFromIdp .= '</SOAP-ENV:Envelope>';
print($outputFromIdp);

return $outputFromIdp;
}

/**
* Send a SAML 2 message using the SOAP binding.
*
* Note: This function never returns.
*
* @param \SAML2\Message $message The message we should send.
*/
public function send(Message $message)
{
header('Content-Type: text/xml', true);
print($this->getOutputToSend($message));
exit(0);
}

Expand All @@ -42,7 +48,7 @@ public function send(Message $message)
*/
public function receive()
{
$postText = file_get_contents('php://input');
$postText = $this->getInputStream();

if (empty($postText)) {
throw new \Exception('Invalid message received to AssertionConsumerService endpoint.');
Expand All @@ -55,4 +61,9 @@ public function receive()

return Message::fromXML($results[0]);
}

protected function getInputStream()
{
return file_get_contents('php://input');
}
}
105 changes: 105 additions & 0 deletions tests/SAML2/SOAPTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace SAML2;

use Exception;
use DOMDocument;

use PHPUnit_Framework_TestCase;

class SOAPTest extends PHPUnit_Framework_TestCase
{
public function testRequestParsingEmptyMessage()
{
$this->setExpectedException('Exception', 'Invalid message received');

$stub = $this->getStubWithInput('');
$stub->receive();
}

public function testRequestParsing()
{
$id = '_6c3a4f8b9c2d';
$artifact = 'AAQAADWNEw5VT47wcO4zX/iEzMmFQvGknDfws2ZtqSGdkNSbsW1cmVR0bzU=';
$issuer = 'https://ServiceProvider.com/SAML';

$input = <<<SOAP
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<samlp:ArtifactResolve
xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
xmlns="urn:oasis:names:tc:SAML:2.0:assertion"
ID="{$id}" Version="2.0"
IssueInstant="2004-01-21T19:00:49Z">
<Issuer>{$issuer}</Issuer>
<samlp:Artifact>{$artifact}</samlp:Artifact>
</samlp:ArtifactResolve>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
SOAP;
$stub = $this->getStubWithInput($input);
$message = $stub->receive();

$this->assertInstanceOf('SAML2\\ArtifactResolve', $message);
$this->assertEquals($artifact, $message->getArtifact());
$this->assertEquals($id, $message->getId());
$this->assertEquals($issuer, $message->getIssuer());

// TODO Validate XML signature is received?
}

public function testResponse()
{
$xml = <<<SOAP
<samlp:ArtifactResponse
xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
xmlns="urn:oasis:names:tc:SAML:2.0:assertion"
ID="_FQvGknDfws2Z" Version="2.0"
InResponseTo="_6c3a4f8b9c2d"
IssueInstant="2004-01-21T19:00:49Z">
<Issuer>https://IdentityProvider.com/SAML</Issuer>
<samlp:Status>
<samlp:StatusCode
Value="urn:oasis:names:tc:SAML:2.0:status:Success"/>
</samlp:Status>
<samlp:LogoutResponse ID="_b0730d21b628110d8b7e004005b13a2b"
InResponseTo="_d2b7c388cec36fa7c39c28fd298644a8"
IssueInstant="2004-01-21T19:05:49Z"
Version="2.0">
<Issuer>https://ServiceProvider.com/SAML</Issuer>
<samlp:Status>
<samlp:StatusCode
Value="urn:oasis:names:tc:SAML:2.0:status:Success"/>
</samlp:Status>
</samlp:LogoutResponse>
</samlp:ArtifactResponse>
SOAP;
$doc = new DOMDocument();
$doc->loadXML($xml);

$message = Message::fromXML($doc->getElementsByTagName('ArtifactResponse')->item(0));

$soap = new SOAP();
$output = $soap->getOutputToSend($message);

$expected = <<<SOAP
<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><samlp:ArtifactResponse xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" ID="_FQvGknDfws2Z" Version="2.0" IssueInstant="2004-01-21T19:00:49Z" InResponseTo="_6c3a4f8b9c2d"><saml:Issuer>https://IdentityProvider.com/SAML</saml:Issuer><samlp:Status><samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/></samlp:Status><samlp:LogoutResponse ID="_b0730d21b628110d8b7e004005b13a2b" InResponseTo="_d2b7c388cec36fa7c39c28fd298644a8" IssueInstant="2004-01-21T19:05:49Z" Version="2.0">
<saml:Issuer>https://ServiceProvider.com/SAML</saml:Issuer>
<samlp:Status>
<samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/>
</samlp:Status>
</samlp:LogoutResponse></samlp:ArtifactResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
SOAP;
$this->assertEquals($output, $expected);
}

private function getStubWithInput($input)
{
$stub = $this->getMock('SAML2\\SOAP', array('getInputStream'));
$stub->expects($this->once())
->method('getInputStream')
->will($this->returnValue($input));

return $stub;
}
}

0 comments on commit 3c5f58c

Please sign in to comment.