Skip to content

Commit

Permalink
Merge branch 'feature/support-encrypted-nameid' of github.com:joonlab…
Browse files Browse the repository at this point in the history
…s/php-saml into joonlabs-feature/support-encrypted-nameid
  • Loading branch information
pitbulk committed Sep 29, 2024
2 parents b1e5c9c + 2b36123 commit f40fbed
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
42 changes: 23 additions & 19 deletions src/Saml2/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ class Response
*/
public $encrypted = false;

/**
* The response contains an encrypted nameId in the assertion.
*
* @var bool
*/
public $encryptedNameId = false;

/**
* After validation, if it fail this var has the cause of the problem
*
Expand Down Expand Up @@ -227,14 +234,12 @@ public function isValid($requestId = null)
);
}

if ($security['wantNameIdEncrypted']) {
$encryptedIdNodes = $this->_queryAssertion('/saml:Subject/saml:EncryptedID/xenc:EncryptedData');
if ($encryptedIdNodes->length != 1) {
throw new ValidationError(
"The NameID of the Response is not encrypted and the SP requires it",
ValidationError::NO_ENCRYPTED_NAMEID
);
}
$this->encryptedNameId = $this->encryptedNameId || $this->_queryAssertion('/saml:Subject/saml:EncryptedID/xenc:EncryptedData')->length > 0;
if (!$this->encryptedNameId && $security['wantNameIdEncrypted']) {
throw new ValidationError(
"The NameID of the Response is not encrypted and the SP requires it",
ValidationError::NO_ENCRYPTED_NAMEID
);
}

// Validate Conditions element exists
Expand Down Expand Up @@ -394,17 +399,6 @@ public function isValid($requestId = null)
}
}

// Detect case not supported
if ($this->encrypted) {
$encryptedIDNodes = Utils::query($this->decryptedDocument, '/samlp:Response/saml:Assertion/saml:Subject/saml:EncryptedID');
if ($encryptedIDNodes->length > 0) {
throw new ValidationError(
'SAML Response that contains an encrypted Assertion with encrypted nameId is not supported.',
ValidationError::NOT_SUPPORTED
);
}
}

if (empty($signedElements) || (!$hasSignedResponse && !$hasSignedAssertion)) {
throw new ValidationError(
'No Signature found. SAML Response rejected',
Expand Down Expand Up @@ -1168,6 +1162,16 @@ protected function decryptAssertion(\DomNode $dom)
if ($check === false) {
throw new Exception('Error: string from decrypted assertion could not be loaded into a XML document');
}

// check if the decrypted assertion contains an encryptedID
$encryptedID = $decrypted->getElementsByTagName('EncryptedID')->item(0);

if ($encryptedID) {
// decrypt the encryptedID
$this->encryptedNameId = true;
$this->decryptAssertion($encryptedID);
}

if ($encData->parentNode instanceof DOMDocument) {
return $decrypted;
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PD94bWwgdmVyc2lvbj0iMS4wIj8+CjxzYW1scDpSZXNwb25zZSB4bWxuczpzYW1scD0idXJuOm9hc2lzOm5hbWVzOnRjOlNBTUw6Mi4wOnByb3RvY29sIiB4bWxuczpzYW1sPSJ1cm46b2FzaXM6bmFtZXM6dGM6U0FNTDoyLjA6YXNzZXJ0aW9uIiBJRD0iX3Jlc3BvbnNlX2lkIiBWZXJzaW9uPSIyLjAiIElzc3VlSW5zdGFudD0iMjAyNC0wOS0wOVQxMjowMDowMFoiIERlc3RpbmF0aW9uPSJodHRwczovL3NwLmV4YW1wbGUuY29tL2FjcyIgSW5SZXNwb25zZVRvPSJfcmVxdWVzdF9pZCI+CiAgICA8c2FtbDpJc3N1ZXI+aHR0cHM6Ly9pZHAuZXhhbXBsZS5jb20vPC9zYW1sOklzc3Vlcj4KICAgIDxzYW1scDpTdGF0dXM+CiAgICAgICAgPHNhbWxwOlN0YXR1c0NvZGUgVmFsdWU9InVybjpvYXNpczpuYW1lczp0YzpTQU1MOjIuMDpzdGF0dXM6U3VjY2VzcyIvPgogICAgPC9zYW1scDpTdGF0dXM+CiAgICA8c2FtbDpFbmNyeXB0ZWRBc3NlcnRpb24geG1sbnM6eGVuYz0iaHR0cDovL3d3dy53My5vcmcvMjAwMS8wNC94bWxlbmMjIj48eGVuYzpFbmNyeXB0ZWREYXRhIHhtbG5zOnhlbmM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDEvMDQveG1sZW5jIyIgVHlwZT0iaHR0cDovL3d3dy53My5vcmcvMjA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bGRzaWcjZW52ZWxvcGVkLXNpZ25hdHVyZSIvPjwvZHM6VHJhbnNmb3Jtcz48ZHM6RGlnZXN0TWV0aG9kIEFsZ29yaXRobT0iaHR0cDovL3d3dy53My5vcmcvMjAwMS8wNC94bWxlbmMjc2hhMjU2Ii8+PGRzOkRpZ2VzdFZhbHVlPi9iVnZlU1JLL1pVdUlza1pFeTdZZ3Nub1lQMjJ1U0FJQWtGRnlpQTk5Y2c9PC9kczpEaWdlc3RWYWx1ZT48L2RzOlJlZmVyZW5jZT48L2RzOlNpZ25lZEluZm8+PGRzOlNpZ25hdHVyZVZhbHVlPmFXSUllOFF1cTI4NHRrOUpBN3d1dXZNaHdnckJNczVhUm1xUUxXRnlzdFV6dWlpcFZUYUE2TElyZ05Gd21ocm1EZmZ3ZVZRWGREK245OGxSNXhSTitsTEs4Vy9IREVGL0NYNmlEekg3UmxQSEhsQ0k0alB2TGtyeTkwZ1J3VHM4ZXZ4Qy9kTlYwYW84WEFJMm5XMkZuYWJjRUhuNXE3ZkNkaXUxWlZkMzQycz08L2RzOlNpZ25hdHVyZVZhbHVlPgo8L2RzOlNpZ25hdHVyZT48L3NhbWxwOlJlc3BvbnNlPgo=
8 changes: 8 additions & 0 deletions tests/src/OneLogin/Saml2/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1823,4 +1823,12 @@ public function testIsValidSignUsingX509certMulti()
$response = new Response($settings, $xml);
$this->assertTrue($response->isValid());
}

public function testCanGetEncryptedNameIdInEncryptedAssertion()
{
$xml = file_get_contents(TEST_ROOT . '/data/responses/response_encrypted_nameid_encrypted_assertion.xml.base64');
$response = new Response($this->_settings, $xml);
$this->assertTrue($response->isValid());
$this->assertSame('user@example.com', $response->getNameId());
}
}

0 comments on commit f40fbed

Please sign in to comment.