Completed
Pull Request — develop (#166)
by
unknown
05:33 queued 02:32
created

createRequesterFailureResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * Copyright 2018 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\GatewayBundle\Service\Gateway;
20
21
use SAML2\Constants;
22
use SAML2\Response as SAMLResponse;
23
use Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger;
24
use Surfnet\StepupGateway\GatewayBundle\Saml\ResponseBuilder;
25
use Surfnet\StepupGateway\GatewayBundle\Saml\ResponseContext;
26
27
/**
28
 * Entry point for the Stepup login flow.
29
 *
30
 * See docs/GatewayState.md for a high-level diagram on how this controller
31
 * interacts with outside actors and other parts of Stepup.
32
 */
33
class FailedResponseService
34
{
35
    /** @var SamlAuthenticationLogger */
36
    private $samlLogger;
37
38
    /** @var ResponseBuilder */
39
    private $responseBuilder;
40
41
    /**
42
     * GatewayServiceProviderService constructor.
43
     * @param SamlAuthenticationLogger $samlLogger
44
     * @param ResponseBuilder $responseBuilder
45
     */
46
    public function __construct(
47
        SamlAuthenticationLogger $samlLogger,
48
        ResponseBuilder $responseBuilder
49
    ) {
50
        $this->samlLogger = $samlLogger;
51
        $this->responseBuilder = $responseBuilder;
52
    }
53
54
    /**
55
     * @param ResponseContext $responseContext
56
     * @return SAMLResponse
57
     */
58 View Code Duplication
    public function sendLoaCannotBeGiven(ResponseContext $responseContext)
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
    {
60
        $originalRequestId = $responseContext->getInResponseTo();
61
62
        $logger = $this->samlLogger->forAuthentication($originalRequestId);
63
        $logger->notice('Loa cannot be given, creating Response with NoAuthnContext status');
64
65
        $response = $this->responseBuilder
66
            ->createNewResponse($responseContext)
67
            ->setResponseStatus(Constants::STATUS_RESPONDER, Constants::STATUS_NO_AUTHN_CONTEXT)
68
            ->get();
69
70
        $logger->notice(sprintf(
71
            'Responding to request "%s" with response based on response from the remote IdP with response "%s"',
72
            $responseContext->getInResponseTo(),
73
            $response->getId()
74
        ));
75
76
        return $response;
77
    }
78
79
    /**
80
     * @param ResponseContext $responseContext
81
     * @return SAMLResponse
82
     */
83 View Code Duplication
    public function sendAuthenticationCancelledByUser(ResponseContext $responseContext)
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...
84
    {
85
        $originalRequestId = $responseContext->getInResponseTo();
86
87
        $logger = $this->samlLogger->forAuthentication($originalRequestId);
88
        $logger->notice('Authentication was cancelled by the user, creating Response with AuthnFailed status');
89
90
        $response = $this->responseBuilder
91
            ->createNewResponse($responseContext)
92
            ->setResponseStatus(
93
                Constants::STATUS_RESPONDER,
94
                Constants::STATUS_AUTHN_FAILED,
95
                'Authentication cancelled by user'
96
            )
97
            ->get();
98
99
        $logger->notice(sprintf(
100
            'Responding to request "%s" with response based on response from the remote IdP with response "%s"',
101
            $responseContext->getInResponseTo(),
102
            $response->getId()
103
        ));
104
105
        return $response;
106
    }
107
108
    /**
109
     * @param ResponseContext $responseContext
110
     * @return SAMLResponse
111
     */
112
    public function createRequesterFailureResponse(ResponseContext $responseContext)
113
    {
114
        $response = $this->responseBuilder
115
            ->createNewResponse($responseContext)
116
            ->setResponseStatus(Constants::STATUS_REQUESTER, Constants::STATUS_REQUEST_UNSUPPORTED)
117
            ->get();
118
119
        return $response;
120
    }
121
122
    /**
123
     * @param $context
124
     * @return SAMLResponse
125
     */
126
    public function createResponseFailureResponse($context)
127
    {
128
        $response = $this->responseBuilder
129
            ->createNewResponse($context)
130
            ->setResponseStatus(Constants::STATUS_RESPONDER)
131
            ->get();
132
133
        return $response;
134
    }
135
}
136