Completed
Pull Request — develop (#93)
by Boy
03:48 queued 17s
created

ResponseFactory::createSecondFactorOnlyResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 21
rs 9.3142
cc 1
eloc 17
nc 1
nop 3
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupGateway\SecondFactorOnlyBundle\Saml;
20
21
use SAML2_Assertion;
22
use Surfnet\SamlBundle\Entity\IdentityProvider;
23
use Surfnet\SamlBundle\Entity\ServiceProvider;
24
use Surfnet\StepupGateway\GatewayBundle\Saml\AssertionSigningService;
25
use Surfnet\StepupGateway\GatewayBundle\Saml\Proxy\ProxyStateHandler;
26
27
/**
28
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
29
 */
30
final class ResponseFactory
31
{
32
    /**
33
     * @var \Surfnet\SamlBundle\Entity\IdentityProvider
34
     */
35
    private $hostedIdentityProvider;
36
37
    /**
38
     * @var \Surfnet\StepupGateway\GatewayBundle\Saml\Proxy\ProxyStateHandler
39
     */
40
    private $proxyStateHandler;
41
42
    /**
43
     * @var \DateTime
44
     */
45
    private $currentTime;
46
47
    /**
48
     * @var \Surfnet\StepupGateway\GatewayBundle\Saml\AssertionSigningService
49
     */
50
    private $assertionSigningService;
51
52 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
        IdentityProvider $hostedIdentityProvider,
54
        ProxyStateHandler $proxyStateHandler,
55
        AssertionSigningService $assertionSigningService
56
    ) {
57
        $this->hostedIdentityProvider    = $hostedIdentityProvider;
58
        $this->proxyStateHandler         = $proxyStateHandler;
59
        $this->assertionSigningService   = $assertionSigningService;
60
        $this->currentTime = new \DateTime('now', new \DateTimeZone('UTC'));
61
    }
62
63
    /**
64
     * @param string $nameId
65
     * @param ServiceProvider $targetServiceProvider
66
     * @param string|null $authnContextClassRef
67
     * @return \SAML2_Response
68
     */
69
    public function createSecondFactorOnlyResponse(
70
        $nameId,
71
        ServiceProvider $targetServiceProvider,
72
        $authnContextClassRef
73
    ) {
74
75
        $newAssertion = new SAML2_Assertion();
76
        $newAssertion->setNotBefore($this->currentTime->getTimestamp());
77
        $newAssertion->setNotOnOrAfter($this->getTimestamp('PT5M'));
78
        $newAssertion->setIssuer($this->hostedIdentityProvider->getEntityId());
79
        $newAssertion->setIssueInstant($this->getTimestamp());
80
        $this->assertionSigningService->signAssertion($newAssertion);
81
        $this->addSubjectConfirmationFor($newAssertion, $targetServiceProvider);
82
        $newAssertion->setNameId([
83
            'Format' => \SAML2_Const::NAMEID_UNSPECIFIED,
84
            'Value' => $nameId,
85
        ]);
86
        $newAssertion->setValidAudiences([$this->proxyStateHandler->getRequestServiceProvider()]);
87
        $this->addAuthenticationStatementTo($newAssertion, $authnContextClassRef);
88
        return $this->createNewAuthnResponse($newAssertion, $targetServiceProvider);
89
    }
90
91
    /**
92
     * @param SAML2_Assertion $newAssertion
93
     * @param ServiceProvider $targetServiceProvider
94
     */
95 View Code Duplication
    private function addSubjectConfirmationFor(SAML2_Assertion $newAssertion, ServiceProvider $targetServiceProvider)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
    {
97
        $confirmation         = new \SAML2_XML_saml_SubjectConfirmation();
98
        $confirmation->Method = \SAML2_Const::CM_BEARER;
99
100
        $confirmationData                      = new \SAML2_XML_saml_SubjectConfirmationData();
101
        $confirmationData->InResponseTo        = $this->proxyStateHandler->getRequestId();
102
        $confirmationData->Recipient           = $targetServiceProvider->getAssertionConsumerUrl();
103
        $confirmationData->NotOnOrAfter        = $this->getTimestamp('PT8H');
104
105
        $confirmation->SubjectConfirmationData = $confirmationData;
106
107
        $newAssertion->setSubjectConfirmation([$confirmation]);
108
    }
109
110
    /**
111
     * @param SAML2_Assertion $assertion
112
     * @param SAML2_Assertion $assertion
113
     */
114
    private function addAuthenticationStatementTo(SAML2_Assertion $assertion, $authnContextClassRef)
115
    {
116
        $assertion->setAuthnInstant($this->getTimestamp());
117
        $assertion->setAuthnContextClassRef($authnContextClassRef);
118
        $assertion->setAuthenticatingAuthority([$this->hostedIdentityProvider->getEntityId()]);
119
    }
120
121
    /**
122
     * @param SAML2_Assertion $newAssertion
123
     * @param ServiceProvider $targetServiceProvider
124
     * @return \SAML2_Response
125
     */
126
    private function createNewAuthnResponse(SAML2_Assertion $newAssertion, ServiceProvider $targetServiceProvider)
127
    {
128
        $response = new \SAML2_Response();
129
        $response->setAssertions([$newAssertion]);
130
        $response->setIssuer($this->hostedIdentityProvider->getEntityId());
131
        $response->setIssueInstant($this->getTimestamp());
132
        $response->setDestination($targetServiceProvider->getAssertionConsumerUrl());
133
        $response->setInResponseTo($this->proxyStateHandler->getRequestId());
134
135
        return $response;
136
    }
137
138
    /**
139
     * @param string $interval a \DateInterval compatible interval to skew the time with
0 ignored issues
show
Documentation introduced by
Should the type for parameter $interval not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
140
     * @return int
141
     */
142
    private function getTimestamp($interval = null)
143
    {
144
        $time = clone $this->currentTime;
145
146
        if ($interval) {
147
            $time->add(new \DateInterval($interval));
148
        }
149
150
        return $time->getTimestamp();
151
    }
152
}
153