Completed
Push — master ( d0c770...89538d )
by Damien
04:12
created

AuthnRequest::firstIdpSsoService()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 4
cts 6
cp 0.6667
rs 9.8666
c 0
b 0
f 0
cc 3
nc 4
nop 1
crap 3.3332
1
<?php
2
3
namespace flipbox\saml\sp\services\messages;
4
5
use craft\base\Component;
6
use flipbox\keychain\records\KeyChainRecord;
7
use flipbox\saml\core\exceptions\InvalidMetadata;
8
use flipbox\saml\core\helpers\MessageHelper;
9
use flipbox\saml\core\records\AbstractProvider;
10
use flipbox\saml\sp\models\Settings;
11
use flipbox\saml\sp\Saml;
12
use SAML2\AuthnRequest as SamlAuthnRequest;
13
use SAML2\Constants;
14
use SAML2\XML\md\EndpointType;
15
use yii\base\Event;
16
17
class AuthnRequest extends Component
18
{
19
20
    const EVENT_AFTER_MESSAGE_CREATED = 'eventAfterMessageCreated';
21
22
    /**
23
     * @param AbstractProvider $identityProvider
24
     * @return \SAML2\XML\md\IndexedEndpointType|null
25
     * @throws InvalidMetadata
26
     */
27 6
    private function firstIdpSsoService(AbstractProvider $identityProvider): EndpointType
28
    {
29 6
        if (!($service = $identityProvider->firstIdpSsoService(Constants::BINDING_HTTP_POST))) {
30
            $service = $identityProvider->firstIdpSsoService();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $service is correct as $identityProvider->firstIdpSsoService() (which targets flipbox\saml\core\record...r::firstIdpSsoService()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
31
        }
32
33 6
        if (!$service) {
34
            throw new InvalidMetadata("IdP Metadata is missing SSO Service");
35
        }
36
37 6
        return $service;
38
    }
39
40
    /**
41
     * @param AbstractProvider $myServiceProvider
42
     * @param AbstractProvider $identityProvider
43
     * @return \SAML2\AuthnRequest
44
     * @throws \craft\errors\SiteNotFoundException
45
     */
46 6
    public function create(
47
        AbstractProvider $myServiceProvider,
48
        AbstractProvider $identityProvider
49
    ): SamlAuthnRequest {
50
51 6
        $idpSsoService = $this->firstIdpSsoService($identityProvider);
52
53 6
        $location = $idpSsoService->getLocation();
54
55
        /**
56
         * @var $samlSettings Settings
57
         */
58 6
        $samlSettings = Saml::getInstance()->getSettings();
59
60 6
        $authnRequest = new \SAML2\AuthnRequest();
61
62 6
        $authnRequest->setAssertionConsumerServiceURL(
63 6
            $myServiceProvider->firstSpAcsService(Constants::BINDING_HTTP_POST)->getLocation()
64
        );
65
66 6
        $authnRequest->setAssertionConsumerServiceIndex(
67 6
            "0"
68
        );
69
70 6
        $authnRequest->setAssertionConsumerServiceURL(
71 6
            $samlSettings->getDefaultLoginEndpoint()
72
        );
73
74 6
        $authnRequest->setProtocolBinding(
75 6
            $idpSsoService->getBinding()
76
        );
77
78 6
        $authnRequest->setId($requestId = MessageHelper::generateId());
79
80 6
        $authnRequest->setIssueInstant(
81 6
            (new \DateTime())->getTimestamp()
82
        );
83
84 6
        $authnRequest->setDestination(
85 6
            $location
86
        );
87
88 6
        $authnRequest->setRelayState(
89 6
            \Craft::$app->getUser()->getReturnUrl()
90
        );
91
92 6
        $authnRequest->setIssuer(
93 6
            Saml::getInstance()->getSettings()->getEntityId()
94
        );
95
96
        /**
97
         * @var KeyChainRecord $pair
98
         */
99 6
        $pair = $myServiceProvider->keychain;
100
101 6
        if ($pair && $samlSettings->signAuthnRequest) {
102 3
            $authnRequest->setSignatureKey(
103 3
                $myServiceProvider->keychainPrivateXmlSecurityKey()
104
            );
105
        }
106
107
        /**
108
         * Kick off event here so people can manipulate this object if needed
109
         */
110 6
        $event = new Event();
111 6
        $event->data = $authnRequest;
112 6
        $this->trigger(static::EVENT_AFTER_MESSAGE_CREATED, $event);
113
114 6
        return $authnRequest;
115
    }
116
}
117