Completed
Push — master ( 524cff...dc4813 )
by
unknown
07:00
created

src/services/messages/AuthnRequest.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: dsmrt
5
 * Date: 1/10/18
6
 * Time: 11:23 AM
7
 */
8
9
namespace flipbox\saml\sp\services\messages;
10
11
12
use craft\base\Component;
13
use flipbox\keychain\records\KeyChainRecord;
14
use flipbox\saml\core\helpers\SecurityHelper;
15
use flipbox\saml\core\records\AbstractProvider;
16
use flipbox\saml\core\records\ProviderInterface;
17
use flipbox\saml\core\services\messages\SamlRequestInterface;
18
use flipbox\saml\sp\models\Settings;
19
use flipbox\saml\sp\records\ProviderRecord;
20
use flipbox\saml\sp\Saml;
21
use flipbox\saml\core\services\traits\Security;
22
use LightSaml\Credential\X509Certificate;
23
use LightSaml\Helper;
24
use LightSaml\Model\Assertion\Issuer;
25
use LightSaml\Model\Protocol\AbstractRequest;
26
use LightSaml\Model\Protocol\SamlMessage;
27
use LightSaml\SamlConstants;
28
use RobRichards\XMLSecLibs\XMLSecurityKey;
29
use yii\base\Event;
30
31
class AuthnRequest extends Component implements SamlRequestInterface
32
{
33
34
    const EVENT_AFTER_MESSAGE_CREATED = 'eventAfterMessageCreated';
35
36
    /**
37
     * @inheritdoc
38
     */
39
    public function create(ProviderInterface $provider, array $config = []): AbstractRequest
40
    {
41
        $location = $provider->getMetadataModel()->getFirstIdpSsoDescriptor()->getFirstSingleSignOnService(
42
        /**
43
         * Just doing post for now
44
         */
45
            SamlConstants::BINDING_SAML2_HTTP_POST
46
        )->getLocation();
47
48
        /**
49
         * @var $samlSettings Settings
50
         */
51
        $samlSettings = Saml::getInstance()->getSettings();
52
        $authnRequest = new \LightSaml\Model\Protocol\AuthnRequest();
53
54
        $authnRequest->setAssertionConsumerServiceURL(
55
            Metadata::getLoginLocation()
56
        )->setProtocolBinding(
57
            $provider->getMetadataModel()->getFirstIdpSsoDescriptor()->getFirstSingleSignOnService()->getBinding()
58
        )->setID($requestId = Helper::generateID())
59
            ->setIssueInstant(new \DateTime())
60
            ->setDestination($location)
61
            ->setRelayState(\Craft::$app->getUser()->getReturnUrl())
62
            ->setIssuer(new Issuer($samlSettings->getEntityId()));
63
64
        /** @var ProviderRecord $thisSp */
65
        $thisSp = Saml::getInstance()->getProvider()->findByEntityId(
66
            Saml::getInstance()->getSettings()->getEntityId()
67
        );
68
        /** @var KeyChainRecord $pair */
69
        $pair = $thisSp->keychain;
0 ignored issues
show
The property keychain does not seem to exist. Did you mean cachedKeychain?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
70
71
        if ($pair && $samlSettings->signAuthnRequest) {
72
            SecurityHelper::signMessage($authnRequest, $pair);
73
        }
74
75
        /**
76
         * Kick off event here so people can manipulate this object if needed
77
         */
78
        $event = new Event();
79
        $event->data = $authnRequest;
80
        $this->trigger(static::EVENT_AFTER_MESSAGE_CREATED, $event);
81
82
        return $authnRequest;
83
    }
84
85
}