Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- ini_set('display_errors', 1);
- ini_set('display_startup_errors', 1);
- error_reporting(E_ALL);
- require_once dirname(__FILE__) . '/vendor/autoload.php';
- // examples/how_to_make_response.php
- function buildSAMLResponse()
- {
- $destination = 'https://theSPendPoint/';
- $issuer = 'localhost';
- $response = new \LightSaml\Model\Protocol\Response();
- $response->addAssertion($assertion = new \LightSaml\Model\Assertion\Assertion());
- $response->setID(\LightSaml\Helper::generateID());
- $response->setIssueInstant(new \DateTime());
- $response->setDestination($destination);
- $response->setIssuer(new \LightSaml\Model\Assertion\Issuer($issuer));
- $response->setStatus(new \LightSaml\Model\Protocol\Status(new \LightSaml\Model\Protocol\StatusCode('urn:oasis:names:tc:SAML:2.0:status:Success')));
- $email = 'MYEMAIL';
- $name = 'MYNAME';
- $assertion->setId(\LightSaml\Helper::generateID());
- $assertion->setIssueInstant(new \DateTime());
- $assertion->setIssuer(new \LightSaml\Model\Assertion\Issuer($issuer));
- $assertion->setSubject(
- (new \LightSaml\Model\Assertion\Subject())
- ->setNameID(new \LightSaml\Model\Assertion\NameID('email.domain.com', \LightSaml\SamlConstants::NAME_ID_FORMAT_EMAIL))
- ->addSubjectConfirmation(
- (new \LightSaml\Model\Assertion\SubjectConfirmation())
- ->setMethod(\LightSaml\SamlConstants::CONFIRMATION_METHOD_BEARER)
- ->setSubjectConfirmationData(
- (new \LightSaml\Model\Assertion\SubjectConfirmationData())
- ->setNotOnOrAfter(new \DateTime('+1 MINUTE'))
- ->setRecipient($destination)
- )
- )
- );
- $assertion->setConditions(
- (new \LightSaml\Model\Assertion\Conditions())
- ->setNotBefore(new \DateTime())
- ->setNotOnOrAfter(new \DateTime('+1 MINUTE'))
- ->addItem(
- new \LightSaml\Model\Assertion\AudienceRestriction([$destination])
- )
- );
- $assertion->addItem(
- (new \LightSaml\Model\Assertion\AttributeStatement())
- ->addAttribute(new \LightSaml\Model\Assertion\Attribute(
- \LightSaml\ClaimTypes::EMAIL_ADDRESS,
- $email
- ))
- ->addAttribute(new \LightSaml\Model\Assertion\Attribute(
- \LightSaml\ClaimTypes::COMMON_NAME,
- $name
- ))
- );
- $assertion->addItem(
- (new \LightSaml\Model\Assertion\AuthnStatement())
- ->setAuthnInstant(new \DateTime('-10 MINUTE'))
- ->setSessionIndex('_some_session_index')
- ->setAuthnContext(
- (new \LightSaml\Model\Assertion\AuthnContext())
- ->setAuthnContextClassRef(\LightSaml\SamlConstants::AUTHN_CONTEXT_PASSWORD_PROTECTED_TRANSPORT)
- )
- );
- $certificate = \LightSaml\Credential\X509Certificate::fromFile(__DIR__ . '/saml.crt');
- $privateKey = \LightSaml\Credential\KeyHelper::createPrivateKey(__DIR__ . '/saml.pem', '', true);
- $response->setSignature(new \LightSaml\Model\XmlDSig\SignatureWriter($certificate, $privateKey));
- $context = new LightSaml\Model\Context\SerializationContext();
- $response->serialize($context->getDocument(), $context);
- $context->getDocument()->formatOutput = true;
- $xml = $context->getDocument()->saveXML();
- return $xml;
- }
- //DISPLAY IT
- echo "<script>var observe;
- if (window.attachEvent) {
- observe = function (element, event, handler) {
- element.attachEvent('on'+event, handler);
- };
- }
- else {
- observe = function (element, event, handler) {
- element.addEventListener(event, handler, false);
- };
- }
- function init () {
- var text = document.getElementById('text');
- function resize () {
- text.style.height = 'auto';
- text.style.height = text.scrollHeight+'px';
- }
- /* 0-timeout to get the already changed text */
- function delayedResize () {
- window.setTimeout(resize, 0);
- }
- observe(text, 'change', resize);
- observe(text, 'cut', delayedResize);
- observe(text, 'paste', delayedResize);
- observe(text, 'drop', delayedResize);
- observe(text, 'keydown', delayedResize);
- text.focus();
- text.select();
- resize();
- }</script>";
- echo '<style>textarea {
- border: 0 none white;
- overflow: hidden;
- padding: 0;
- outline: none;
- background-color: #D0D0D0;
- }</style>';
- echo '<body onload="init();">';
- $samlResponse = buildSAMLResponse();
- echo '<textarea rows="1" style="height:1em;width: 100%;" id="text">';
- echo $samlResponse;
- echo '</textarea>';
- echo base64_encode($samlResponse);
- echo '<br> ';
- echo '</body>';
Add Comment
Please, Sign In to add comment