|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Active Collab Authentication project. |
| 5 | + * |
| 6 | + * (c) A51 doo <[email protected]>. All rights reserved. |
| 7 | + */ |
| 8 | + |
| 9 | +namespace ActiveCollab\Authentication\Saml; |
| 10 | + |
| 11 | +use DateTime; |
| 12 | +use LightSaml\Binding\BindingFactory; |
| 13 | +use LightSaml\ClaimTypes; |
| 14 | +use LightSaml\Context\Profile\MessageContext; |
| 15 | +use LightSaml\Credential\KeyHelper; |
| 16 | +use LightSaml\Credential\X509Certificate; |
| 17 | +use LightSaml\Helper; |
| 18 | +use LightSaml\Model\Assertion\Assertion; |
| 19 | +use LightSaml\Model\Assertion\Attribute; |
| 20 | +use LightSaml\Model\Assertion\AttributeStatement; |
| 21 | +use LightSaml\Model\Assertion\AudienceRestriction; |
| 22 | +use LightSaml\Model\Assertion\AuthnContext; |
| 23 | +use LightSaml\Model\Assertion\AuthnStatement; |
| 24 | +use LightSaml\Model\Assertion\Conditions; |
| 25 | +use LightSaml\Model\Assertion\Issuer; |
| 26 | +use LightSaml\Model\Assertion\NameID; |
| 27 | +use LightSaml\Model\Assertion\Subject; |
| 28 | +use LightSaml\Model\Assertion\SubjectConfirmation; |
| 29 | +use LightSaml\Model\Assertion\SubjectConfirmationData; |
| 30 | +use LightSaml\Model\Protocol\Response; |
| 31 | +use LightSaml\Model\XmlDSig\SignatureWriter; |
| 32 | +use LightSaml\SamlConstants; |
| 33 | +use Psr\Log\LoggerInterface; |
| 34 | +use RuntimeException; |
| 35 | +use Symfony\Component\HttpFoundation\Response as SymfonyResponse; |
| 36 | + |
| 37 | +class SsoResponse |
| 38 | +{ |
| 39 | + /** |
| 40 | + * @var SamlDataManagerInterface |
| 41 | + */ |
| 42 | + private $saml_data_manager; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var LoggerInterface |
| 46 | + */ |
| 47 | + private $logger; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var string |
| 51 | + */ |
| 52 | + private $saml_crt; |
| 53 | + |
| 54 | + /** |
| 55 | + * @var string |
| 56 | + */ |
| 57 | + private $saml_key; |
| 58 | + |
| 59 | + /** |
| 60 | + * @param SamlDataManagerInterface $saml_data_manager |
| 61 | + * @param string $saml_crt |
| 62 | + * @param string $saml_key |
| 63 | + * @param LoggerInterface $logger |
| 64 | + */ |
| 65 | + public function __construct(SamlDataManagerInterface $saml_data_manager, $saml_crt, $saml_key, LoggerInterface $logger = null) |
| 66 | + { |
| 67 | + $this->saml_data_manager = $saml_data_manager; |
| 68 | + $this->logger = $logger; |
| 69 | + $this->saml_crt = $saml_crt; |
| 70 | + $this->saml_key = $saml_key; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @param string $email |
| 75 | + * @param string $message_id |
| 76 | + * @return string |
| 77 | + */ |
| 78 | + public function send($email, $message_id) |
| 79 | + { |
| 80 | + $message = $this->saml_data_manager->get($message_id); |
| 81 | + |
| 82 | + if (!$message) { |
| 83 | + if ($this->logger) { |
| 84 | + $this->logger->error("Saml message with id $message_id not found or expired"); |
| 85 | + } |
| 86 | + |
| 87 | + throw new RuntimeException('Authentication message does not exist'); |
| 88 | + } |
| 89 | + |
| 90 | + $this->saml_data_manager->delete($message_id); |
| 91 | + |
| 92 | + $response = new Response(); |
| 93 | + $assertion = new Assertion(); |
| 94 | + $response |
| 95 | + ->addAssertion($assertion) |
| 96 | + ->setID(Helper::generateID()) |
| 97 | + ->setIssueInstant(new DateTime()) |
| 98 | + ->setDestination($message->getAssertionConsumerServiceURL()) |
| 99 | + ->setIssuer(new Issuer($message->getIssuer()->getValue())); |
| 100 | + |
| 101 | + $assertion |
| 102 | + ->setId(Helper::generateID()) |
| 103 | + ->setIssueInstant(new DateTime()) |
| 104 | + ->setIssuer(new Issuer($message->getIssuer()->getValue())) |
| 105 | + ->setSubject( |
| 106 | + (new Subject()) |
| 107 | + ->setNameID(new NameID($email, SamlConstants::NAME_ID_FORMAT_EMAIL)) |
| 108 | + ->addSubjectConfirmation( |
| 109 | + (new SubjectConfirmation()) |
| 110 | + ->setMethod(SamlConstants::CONFIRMATION_METHOD_BEARER) |
| 111 | + ->setSubjectConfirmationData( |
| 112 | + (new SubjectConfirmationData()) |
| 113 | + ->setInResponseTo($message->getID()) |
| 114 | + ->setNotOnOrAfter(new DateTime('+1 MINUTE')) |
| 115 | + ->setRecipient($message->getAssertionConsumerServiceURL()) |
| 116 | + ) |
| 117 | + ) |
| 118 | + ) |
| 119 | + ->setConditions( |
| 120 | + (new Conditions()) |
| 121 | + ->setNotBefore(new DateTime()) |
| 122 | + ->setNotOnOrAfter(new DateTime('+1 MINUTE')) |
| 123 | + ->addItem( |
| 124 | + new AudienceRestriction([$message->getAssertionConsumerServiceURL()]) |
| 125 | + ) |
| 126 | + ) |
| 127 | + ->addItem( |
| 128 | + (new AttributeStatement()) |
| 129 | + ->addAttribute(new Attribute(ClaimTypes::EMAIL_ADDRESS, $email)) |
| 130 | + ) |
| 131 | + ->addItem( |
| 132 | + (new AuthnStatement()) |
| 133 | + ->setAuthnInstant(new DateTime('-10 MINUTE')) |
| 134 | + ->setSessionIndex($message_id) |
| 135 | + ->setAuthnContext( |
| 136 | + (new AuthnContext())->setAuthnContextClassRef(SamlConstants::AUTHN_CONTEXT_PASSWORD_PROTECTED_TRANSPORT) |
| 137 | + ) |
| 138 | + ); |
| 139 | + |
| 140 | + $certificate = X509Certificate::fromFile($this->saml_crt); |
| 141 | + $private_key = KeyHelper::createPrivateKey($this->saml_key, '', true); |
| 142 | + |
| 143 | + $response->setSignature(new SignatureWriter($certificate, $private_key)); |
| 144 | + |
| 145 | + $binding_factory = new BindingFactory(); |
| 146 | + $post_binding = $binding_factory->create(SamlConstants::BINDING_SAML2_HTTP_POST); |
| 147 | + $message_context = new MessageContext(); |
| 148 | + $message_context->setMessage($response); |
| 149 | + /** @var SymfonyResponse $http_response */ |
| 150 | + $http_response = $post_binding->send($message_context); |
| 151 | + |
| 152 | + return $http_response->getContent(); |
| 153 | + } |
| 154 | +} |
0 commit comments