Completed
Push — feature/second-factor-only-2 ( 838e43 )
by Boy
03:36
created

ResponseFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 145
Duplicated Lines 31.03 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 8
c 2
b 0
f 1
lcom 1
cbo 8
dl 45
loc 145
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 10 10 1
A createSecondFactorOnlyResponse() 0 15 1
A createNewAssertion() 0 21 1
A addSubjectConfirmationFor() 14 14 1
A addAuthenticationStatementTo() 0 6 1
A createNewAuthnResponse() 11 11 1
A getTimestamp() 10 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
final class ResponseFactory
28
{
29
    /**
30
     * @var \Surfnet\SamlBundle\Entity\IdentityProvider
31
     */
32
    private $hostedIdentityProvider;
33
34
    /**
35
     * @var \Surfnet\StepupGateway\GatewayBundle\Saml\Proxy\ProxyStateHandler
36
     */
37
    private $proxyStateHandler;
38
39
    /**
40
     * @var \DateTime
41
     */
42
    private $currentTime;
43
44
    /**
45
     * @var \Surfnet\StepupGateway\GatewayBundle\Saml\AssertionSigningService
46
     */
47
    private $assertionSigningService;
48
49 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...
50
        IdentityProvider $hostedIdentityProvider,
51
        ProxyStateHandler $proxyStateHandler,
52
        AssertionSigningService $assertionSigningService
53
    ) {
54
        $this->hostedIdentityProvider    = $hostedIdentityProvider;
55
        $this->proxyStateHandler         = $proxyStateHandler;
56
        $this->assertionSigningService   = $assertionSigningService;
57
        $this->currentTime = new \DateTime('now', new \DateTimeZone('UTC'));
58
    }
59
60
    /**
61
     * @param string $nameId
62
     * @param ServiceProvider $targetServiceProvider
63
     * @param string|null $authnContextClassRef
64
     * @return \SAML2_Response
65
     */
66
    public function createSecondFactorOnlyResponse(
67
        $nameId,
68
        ServiceProvider $targetServiceProvider,
69
        $authnContextClassRef
70
    ) {
71
72
        return $this->createNewAuthnResponse(
73
            $this->createNewAssertion(
74
                $nameId,
75
                $targetServiceProvider,
76
                $authnContextClassRef
77
            ),
78
            $targetServiceProvider
79
        );
80
    }
81
82
    /**
83
     * @param SAML2_Assertion $newAssertion
84
     * @param ServiceProvider $targetServiceProvider
85
     * @return \SAML2_Response
86
     */
87 View Code Duplication
    private function createNewAuthnResponse(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...
88
    {
89
        $response = new \SAML2_Response();
90
        $response->setAssertions([$newAssertion]);
91
        $response->setIssuer($this->hostedIdentityProvider->getEntityId());
92
        $response->setIssueInstant($this->getTimestamp());
93
        $response->setDestination($targetServiceProvider->getAssertionConsumerUrl());
94
        $response->setInResponseTo($this->proxyStateHandler->getRequestId());
95
96
        return $response;
97
    }
98
99
    /**
100
     * @param string $nameId
101
     * @param ServiceProvider $targetServiceProvider
102
     * @param string $authnContextClassRef
103
     * @return SAML2_Assertion
104
     */
105
    private function createNewAssertion(
106
        $nameId,
107
        ServiceProvider $targetServiceProvider,
108
        $authnContextClassRef
109
    ) {
110
        $newAssertion = new SAML2_Assertion();
111
        $newAssertion->setNotBefore($this->currentTime->getTimestamp());
112
        $newAssertion->setNotOnOrAfter($this->getTimestamp('PT5M'));
113
        $newAssertion->setIssuer($this->hostedIdentityProvider->getEntityId());
114
        $newAssertion->setIssueInstant($this->getTimestamp());
115
        $this->assertionSigningService->signAssertion($newAssertion);
116
        $this->addSubjectConfirmationFor($newAssertion, $targetServiceProvider);
117
        $newAssertion->setNameId([
118
            'Format' => \SAML2_Const::NAMEID_UNSPECIFIED,
119
            'Value' => $nameId,
120
        ]);
121
        $newAssertion->setValidAudiences([$this->proxyStateHandler->getRequestServiceProvider()]);
122
        $this->addAuthenticationStatementTo($newAssertion, $authnContextClassRef);
123
124
        return $newAssertion;
125
    }
126
127
    /**
128
     * @param SAML2_Assertion $newAssertion
129
     * @param ServiceProvider $targetServiceProvider
130
     */
131 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...
132
    {
133
        $confirmation         = new \SAML2_XML_saml_SubjectConfirmation();
134
        $confirmation->Method = \SAML2_Const::CM_BEARER;
135
136
        $confirmationData                      = new \SAML2_XML_saml_SubjectConfirmationData();
137
        $confirmationData->InResponseTo        = $this->proxyStateHandler->getRequestId();
138
        $confirmationData->Recipient           = $targetServiceProvider->getAssertionConsumerUrl();
139
        $confirmationData->NotOnOrAfter        = $this->getTimestamp('PT8H');
140
141
        $confirmation->SubjectConfirmationData = $confirmationData;
142
143
        $newAssertion->setSubjectConfirmation([$confirmation]);
144
    }
145
146
    /**
147
     * @param SAML2_Assertion $assertion
148
     * @param SAML2_Assertion $assertion
149
     */
150
    private function addAuthenticationStatementTo(SAML2_Assertion $assertion, $authnContextClassRef)
151
    {
152
        $assertion->setAuthnInstant($this->getTimestamp());
153
        $assertion->setAuthnContextClassRef($authnContextClassRef);
154
        $assertion->setAuthenticatingAuthority([$this->hostedIdentityProvider->getEntityId()]);
155
    }
156
157
    /**
158
     * @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...
159
     * @return int
160
     */
161 View Code Duplication
    private function getTimestamp($interval = null)
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...
162
    {
163
        $time = clone $this->currentTime;
164
165
        if ($interval) {
166
            $time->add(new \DateInterval($interval));
167
        }
168
169
        return $time->getTimestamp();
170
    }
171
}
172