Guest User

Untitled

a guest
Jun 17th, 2021
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.77 KB | None | 0 0
  1. <?php
  2.  
  3. ini_set('display_errors', 1);
  4. ini_set('display_startup_errors', 1);
  5. error_reporting(E_ALL);
  6. require_once dirname(__FILE__) . '/vendor/autoload.php';
  7.  
  8. // examples/how_to_make_response.php
  9. function buildSAMLResponse()
  10. {
  11.     $destination = 'https://theSPendPoint/';
  12.     $issuer = 'localhost';
  13.  
  14.     $response = new \LightSaml\Model\Protocol\Response();
  15.     $response->addAssertion($assertion = new \LightSaml\Model\Assertion\Assertion());
  16.     $response->setID(\LightSaml\Helper::generateID());
  17.     $response->setIssueInstant(new \DateTime());
  18.     $response->setDestination($destination);
  19.     $response->setIssuer(new \LightSaml\Model\Assertion\Issuer($issuer));
  20.     $response->setStatus(new \LightSaml\Model\Protocol\Status(new \LightSaml\Model\Protocol\StatusCode('urn:oasis:names:tc:SAML:2.0:status:Success')));
  21.  
  22.     $email = 'MYEMAIL';
  23.     $name = 'MYNAME';
  24.  
  25.     $assertion->setId(\LightSaml\Helper::generateID());
  26.     $assertion->setIssueInstant(new \DateTime());
  27.     $assertion->setIssuer(new \LightSaml\Model\Assertion\Issuer($issuer));
  28.     $assertion->setSubject(
  29.         (new \LightSaml\Model\Assertion\Subject())
  30.             ->setNameID(new \LightSaml\Model\Assertion\NameID('email.domain.com', \LightSaml\SamlConstants::NAME_ID_FORMAT_EMAIL))
  31.             ->addSubjectConfirmation(
  32.                 (new \LightSaml\Model\Assertion\SubjectConfirmation())
  33.                     ->setMethod(\LightSaml\SamlConstants::CONFIRMATION_METHOD_BEARER)
  34.                     ->setSubjectConfirmationData(
  35.                         (new \LightSaml\Model\Assertion\SubjectConfirmationData())
  36.                             ->setNotOnOrAfter(new \DateTime('+1 MINUTE'))
  37.                             ->setRecipient($destination)
  38.                     )
  39.             )
  40.     );
  41.     $assertion->setConditions(
  42.         (new \LightSaml\Model\Assertion\Conditions())
  43.             ->setNotBefore(new \DateTime())
  44.             ->setNotOnOrAfter(new \DateTime('+1 MINUTE'))
  45.             ->addItem(
  46.                 new \LightSaml\Model\Assertion\AudienceRestriction([$destination])
  47.             )
  48.     );
  49.     $assertion->addItem(
  50.         (new \LightSaml\Model\Assertion\AttributeStatement())
  51.             ->addAttribute(new \LightSaml\Model\Assertion\Attribute(
  52.                 \LightSaml\ClaimTypes::EMAIL_ADDRESS,
  53.                 $email
  54.             ))
  55.             ->addAttribute(new \LightSaml\Model\Assertion\Attribute(
  56.                 \LightSaml\ClaimTypes::COMMON_NAME,
  57.                 $name
  58.             ))
  59.     );
  60.     $assertion->addItem(
  61.         (new \LightSaml\Model\Assertion\AuthnStatement())
  62.             ->setAuthnInstant(new \DateTime('-10 MINUTE'))
  63.             ->setSessionIndex('_some_session_index')
  64.             ->setAuthnContext(
  65.                 (new \LightSaml\Model\Assertion\AuthnContext())
  66.                     ->setAuthnContextClassRef(\LightSaml\SamlConstants::AUTHN_CONTEXT_PASSWORD_PROTECTED_TRANSPORT)
  67.             )
  68.     );
  69.  
  70.     $certificate = \LightSaml\Credential\X509Certificate::fromFile(__DIR__ . '/saml.crt');
  71.     $privateKey = \LightSaml\Credential\KeyHelper::createPrivateKey(__DIR__ . '/saml.pem', '', true);
  72.  
  73.     $response->setSignature(new \LightSaml\Model\XmlDSig\SignatureWriter($certificate, $privateKey));
  74.  
  75.     $context = new LightSaml\Model\Context\SerializationContext();
  76.     $response->serialize($context->getDocument(), $context);
  77.     $context->getDocument()->formatOutput = true;
  78.     $xml = $context->getDocument()->saveXML();
  79.  
  80.     return $xml;
  81.  
  82. }
  83.  
  84. //DISPLAY IT
  85. echo "<script>var observe;
  86. if (window.attachEvent) {
  87.    observe = function (element, event, handler) {
  88.        element.attachEvent('on'+event, handler);
  89.    };
  90. }
  91. else {
  92.    observe = function (element, event, handler) {
  93.        element.addEventListener(event, handler, false);
  94.    };
  95. }
  96. function init () {
  97.    var text = document.getElementById('text');
  98.    function resize () {
  99.        text.style.height = 'auto';
  100.        text.style.height = text.scrollHeight+'px';
  101.    }
  102.    /* 0-timeout to get the already changed text */
  103.    function delayedResize () {
  104.        window.setTimeout(resize, 0);
  105.    }
  106.    observe(text, 'change',  resize);
  107.    observe(text, 'cut',     delayedResize);
  108.    observe(text, 'paste',   delayedResize);
  109.    observe(text, 'drop',    delayedResize);
  110.    observe(text, 'keydown', delayedResize);
  111.  
  112.    text.focus();
  113.    text.select();
  114.    resize();
  115. }</script>";
  116. echo '<style>textarea {
  117.    border: 0 none white;
  118.    overflow: hidden;
  119.    padding: 0;
  120.    outline: none;
  121.    background-color: #D0D0D0;
  122. }</style>';
  123. echo '<body onload="init();">';
  124. $samlResponse = buildSAMLResponse();
  125. echo '<textarea rows="1" style="height:1em;width: 100%;" id="text">';
  126.  
  127. echo $samlResponse;
  128. echo '</textarea>';
  129.  
  130. echo base64_encode($samlResponse);
  131. echo '<br>&nbsp;';
  132. echo '</body>';
  133.  
Add Comment
Please, Sign In to add comment