PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/fedlab/lib/tests/MultipleAttr.php

http://simplesamlphp-labs.googlecode.com/
PHP | 202 lines | 106 code | 81 blank | 15 comment | 11 complexity | cb65d7f006afec21988d7d6d8853a034 MD5 | raw file
  1. <?php
  2. class sspmod_fedlab_tests_MultipleAttr extends sspmod_fedlab_BasicSPTest {
  3. protected function register() {
  4. $this->testruns = array(
  5. 'multipleattr' => 'SP SHOULD find attributes in a second AttributeStatement, not only in the first.',
  6. );
  7. }
  8. protected function createResponse($testrun, $request, $relayState = NULL) {
  9. $this->log($testrun, 'Creating response with relaystate [' . $relayState. ']');
  10. $idpMetadata = SimpleSAML_Configuration::loadFromArray($this->idpmetadata);
  11. $spMetadata = SimpleSAML_Configuration::loadFromArray($this->metadata);
  12. $requestId = $request->getId();
  13. $consumerURL = $request->getAssertionConsumerServiceURL();
  14. $spentityid = $spMetadata->getString('entityid');
  15. $idpentityid = $idpMetadata->getString('entityid');
  16. $consumerURLf = $spMetadata->getDefaultEndpoint('AssertionConsumerService', array('urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST'), $consumerURL);
  17. $consumerURL = $consumerURLf['Location'];
  18. # print_r($spMetadata); exit;
  19. # print_r($spMetadata->getString('AssertionConsumerServiceURL'))
  20. $protocolBinding = SAML2_Const::BINDING_HTTP_POST;
  21. $config = $this->getConfig($testrun);
  22. $authnInstant = time();
  23. // Build assertion
  24. $a = new sspmod_fedlab_xml_AssertionMultipleAttrStatements();
  25. if ($config['signAssertion']) {
  26. $keyArray = SimpleSAML_Utilities::loadPrivateKey($idpMetadata, TRUE);
  27. $certArray = SimpleSAML_Utilities::loadPublicKey($idpMetadata, FALSE);
  28. $privateKey = new XMLSecurityKey(XMLSecurityKey::RSA_SHA1, array('type' => 'private'));
  29. $privateKey->loadKey($keyArray['PEM'], FALSE);
  30. $a->setSignatureKey($privateKey);
  31. if ($certArray === NULL) throw new Exception('No certificates found. [1]');
  32. if (!array_key_exists('PEM', $certArray)) throw new Exception('No certificates found. [2]');
  33. $a->setCertificates(array($certArray['PEM']));
  34. }
  35. $a->addSubjectConfirmationData = $config['addSubjectConfirmationData'];
  36. $a->iterateSubjectConfirmationData = $config['iterateSubjectConfirmationData'];
  37. $a->subjectAddresses = $this->getAddresses($testrun, array(NULL));
  38. if (isset($config['dateFormat'])) {
  39. $a->dateformat = $config['dateFormat'];
  40. }
  41. $a->setIssueInstant(time() + $config['issueInstantMod']);
  42. $a->extracondition = $config['extracondition'];
  43. $a->setIssuer($this->getIssuerAssertion($testrun, $idpentityid));
  44. $a->setDestination($this->getDestinationAssertion($testrun, array($consumerURL)));
  45. $a->setValidAudiences($this->getValidAudience($testrun, array(array($spentityid))));
  46. $a->setNotBefore(time() + $config['notBeforeSkew']);
  47. $assertionLifetime = $config['assertionLifetime'];
  48. $a->setNotOnOrAfter(time() + $assertionLifetime);
  49. $a->notOnOrAfterSubjectConfirmationData = time() + $config['SubjectConfirmationDataLifetime'];
  50. $a->setAuthnContext($this->getAuthnContext($testrun, SAML2_Const::AC_PASSWORD));
  51. $a->setAuthnInstant( $authnInstant );
  52. $sessionLifetime = $config['sessionLifetime'];
  53. $a->setSessionNotOnOrAfter(time() + $sessionLifetime);
  54. $a->setSessionIndex(SimpleSAML_Utilities::generateID());
  55. /* Add attributes. */
  56. $attributeNameFormat = $config['attributeNameFormat'];
  57. $a->setAttributeNameFormat($attributeNameFormat);
  58. $attributes = array(
  59. 'urn:oid:1.3.6.1.4.1.5923.1.1.1.6' => array('andreas@uninett.no'),
  60. 'urn:mace:dir:attribute-def:eduPersonPrincipalName' => array('andreas@uninett.no'),
  61. );
  62. $a->setAttributes($attributes);
  63. $nameId = array(
  64. 'Format' => $config['nameIdFormat'],
  65. 'SPNameQualifier' => $spentityid,
  66. 'Value' => SimpleSAML_Utilities::generateID(),
  67. );
  68. $a->setNameId($nameId);
  69. // Assertion builded....
  70. # print_r($requestId);
  71. $inresponseto = $this->getInResponseToAssertion($testrun, $requestId);
  72. if (!empty($inresponseto)) {
  73. $a->setInResponseTo($inresponseto);
  74. }
  75. // $assertion->setAuthenticatingAuthority($state['saml:AuthenticatingAuthority']);
  76. /* Maybe encrypt the assertion. */
  77. // $a = sspmod_saml2_Message::encryptAssertion($idpMetadata, $spMetadata, $a);
  78. // Build the response
  79. $signResponse = $config['signResponse'];
  80. $response = new sspmod_fedlab_xml_Response();
  81. $response->setIssuer($this->getIssuerResponse($testrun, $idpentityid));
  82. $response->setDestination($this->getDestinationResponse($testrun, $consumerURL));
  83. if ($signResponse) {
  84. // self::addSign($srcMetadata, $dstMetadata, $r);
  85. $keyArray = SimpleSAML_Utilities::loadPrivateKey($idpMetadata, TRUE);
  86. $certArray = SimpleSAML_Utilities::loadPublicKey($idpMetadata, FALSE);
  87. $privateKey = new XMLSecurityKey(XMLSecurityKey::RSA_SHA1, array('type' => 'private'));
  88. $privateKey->loadKey($keyArray['PEM'], FALSE);
  89. $response->setSignatureKey($privateKey);
  90. if ($certArray === NULL) throw new Exception('No certificates found. [1]');
  91. if (!array_key_exists('PEM', $certArray)) throw new Exception('No certificates found. [2]');
  92. $response->setCertificates(array($certArray['PEM']));
  93. }
  94. $inresponseto = $this->getInResponseToResponse($testrun, $requestId);
  95. if (!empty($inresponseto)) {
  96. $response->setInResponseTo($inresponseto);
  97. }
  98. $response->setRelayState($this->getRelayState($testrun, $relayState));
  99. $response->setAssertions(array($a));
  100. $this->tweakResponse($testrun, $response);
  101. $msgStr = $response->toSignedXML();
  102. $msgStr = $msgStr->ownerDocument->saveXML($msgStr);
  103. # echo '<pre>'; echo(htmlspecialchars($msgStr)); exit;
  104. # $msgStr = base64_encode($msgStr);
  105. # $msgStr = htmlspecialchars($msgStr);
  106. # $this->log($testrun, 'created response: ' . $testrun);
  107. return array('url' => $consumerURL, 'Response' => $msgStr, 'RelayState' => $relayState);
  108. }
  109. protected function expectedResult($testrun, $body, $debugoutput) {
  110. $this->log($testrun, 'checking expected output... ');
  111. switch($testrun) {
  112. case 'multipleattr':
  113. if ($this->containsName($body)) {
  114. $this->setResult(sspmod_fedlab_Tester::STATUS_OK, $testrun, $testrun, $this->testruns[$testrun], $debugoutput);
  115. } else {
  116. $this->setResult(sspmod_fedlab_Tester::STATUS_FATAL, $testrun, $testrun, $this->testruns[$testrun], $debugoutput);
  117. }
  118. break;
  119. }
  120. }
  121. }