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 for Conditions/AudienceRestriction/Audience in AuthnRequest #137

Merged
merged 4 commits into from
Nov 23, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
53 changes: 53 additions & 0 deletions src/SAML2/AuthnRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ class AuthnRequest extends Request
*/
private $requestedAuthnContext;

/**
* Audiences to send in the request.
*
* @var array
*/
private $audiences;

/**
* @var \SAML2\XML\saml\SubjectConfirmation[]
*/
Expand Down Expand Up @@ -404,6 +411,30 @@ public function setIsPassive($isPassive)
$this->isPassive = $isPassive;
}

/**
* Retrieve the audiences to send in the request.
*
* This may be null, in which case no audience will be sent.
*
* @return array|null The audiences.
*/
public function getAudiences()
{
return $this->audiences;
}

/**
* Set the audiences to send in the request.
*
* This may be null, in which case no audience will be sent.
*
* @param array|null $audiences The audiences.
*/
public function setAudiences(array $audiences = null)
{
$this->audiences = $audiences;
}


/**
* This function sets the scoping for the request.
Expand Down Expand Up @@ -732,6 +763,8 @@ public function toUnsignedXML()
$root->appendChild($nameIdPolicy);
}

$this->addConditions($root);

$rac = $this->requestedAuthnContext;
if (!empty($rac) && !empty($rac['AuthnContextClassRef'])) {
$e = $this->document->createElementNS(Constants::NS_SAMLP, 'RequestedAuthnContext');
Expand Down Expand Up @@ -806,4 +839,24 @@ private function addSubject(\DOMElement $root)
$sc->toXML($subject);
}
}

/**
* Add a Conditions-node to the request.
*
* @param \DOMElement $root The request element we should add the conditions to.
*/
private function addConditions(\DOMElement $root)
{
if ($this->audiences !== null) {
$document = $root->ownerDocument;

$conditions = $document->createElementNS(Constants::NS_SAML, 'saml:Conditions');
$root->appendChild($conditions);

$ar = $document->createElementNS(Constants::NS_SAML, 'saml:AudienceRestriction');
$conditions->appendChild($ar);

Utils::addStrings($ar, Constants::NS_SAML, 'saml:Audience', false, $this->audiences);
}
}
}
35 changes: 35 additions & 0 deletions tests/SAML2/AuthnRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -984,4 +984,39 @@ public function testEmptySubjectThrowsException()
$this->setExpectedException('Exception', 'Missing <saml:NameID> or <saml:EncryptedID> in <saml:Subject>');
$authnRequest = new AuthnRequest(DOMDocumentFactory::fromString($xml)->documentElement);
}

/**
* Test setting audiences.
*/
public function testAudiencesAreAddedCorrectly()
{
// basic AuthnRequest
$request = new AuthnRequest();
$request->setIssuer('https://gateway.example.org/saml20/sp/metadata');
$request->setDestination('https://tiqr.example.org/idp/profile/saml2/Redirect/SSO');
$request->setAudiences(array('https://sp1.example.org', 'https://sp2.example.org'));

$expectedStructureDocument = <<<AUTHNREQUEST
<samlp:AuthnRequest
xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
ID=""
Version=""
IssueInstant=""
Destination="https://tiqr.example.org/idp/profile/saml2/Redirect/SSO">
<saml:Issuer>https://gateway.example.org/saml20/sp/metadata</saml:Issuer>
<saml:Conditions>
<saml:AudienceRestriction>
<saml:Audience>https://sp1.example.org</saml:Audience>
<saml:Audience>https://sp2.example.org</saml:Audience>
</saml:AudienceRestriction>
</saml:Conditions>
</samlp:AuthnRequest>
AUTHNREQUEST;

$expectedStructure = DOMDocumentFactory::fromString($expectedStructureDocument)->documentElement;
$requestStructure = $request->toUnsignedXML();

$this->assertEqualXMLStructure($expectedStructure, $requestStructure);
}
}