Completed
Push — master ( 00f21d...76f138 )
by Boy
19:52 queued 16:05
created

ProxyResponseFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 121
Duplicated Lines 38.02 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 8
dl 46
loc 121
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createProxyResponse() 0 19 1
A addSubjectConfirmationFor() 14 14 1
A addAuthenticationStatementTo() 0 6 1
A createNewAuthnResponse() 11 11 1
A getTimestamp() 10 10 2
A __construct() 11 11 1

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