FailedResponseService   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 105
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createRequesterFailureResponse() 0 8 1
A createResponseFailureResponse() 0 8 1
A sendAuthenticationCancelledByUser() 0 23 1
A sendLoaCannotBeGiven() 0 19 1
A __construct() 0 6 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
class FailedResponseService
28
{
29
    /** @var SamlAuthenticationLogger */
30
    private $samlLogger;
31
32
    /** @var ResponseBuilder */
33
    private $responseBuilder;
34
35
    /**
36
     * GatewayServiceProviderService constructor.
37
     * @param SamlAuthenticationLogger $samlLogger
38
     * @param ResponseBuilder $responseBuilder
39
     */
40
    public function __construct(
41
        SamlAuthenticationLogger $samlLogger,
42
        ResponseBuilder $responseBuilder
43
    ) {
44
        $this->samlLogger = $samlLogger;
45
        $this->responseBuilder = $responseBuilder;
46
    }
47
48
    /**
49
     * Return a SAMLResponse indicating that the given Loa is invalid.
50
     *
51
     * @param ResponseContext $responseContext
52
     * @return SAMLResponse
53
     */
54
    public function sendLoaCannotBeGiven(ResponseContext $responseContext)
55
    {
56
        $originalRequestId = $responseContext->getInResponseTo();
57
58
        $logger = $this->samlLogger->forAuthentication($originalRequestId);
0 ignored issues
show
Bug introduced by
It seems like $originalRequestId can also be of type null; however, parameter $requestId of Surfnet\SamlBundle\Monol...er::forAuthentication() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
        $logger = $this->samlLogger->forAuthentication(/** @scrutinizer ignore-type */ $originalRequestId);
Loading history...
59
        $logger->notice('Loa cannot be given, creating Response with NoAuthnContext status');
60
61
        $response = $this->responseBuilder
62
            ->createNewResponse($responseContext)
63
            ->setResponseStatus(Constants::STATUS_RESPONDER, Constants::STATUS_NO_AUTHN_CONTEXT)
64
            ->get();
65
66
        $logger->notice(sprintf(
67
            'Responding to request "%s" with response based on response from the remote IdP with response "%s"',
68
            $responseContext->getInResponseTo(),
69
            $response->getId()
70
        ));
71
72
        return $response;
73
    }
74
75
    /**
76
     * Return a SAMLResponse indicating that the authentication is cancelled by the user.
77
     *
78
     * @param ResponseContext $responseContext
79
     * @return SAMLResponse
80
     */
81
    public function sendAuthenticationCancelledByUser(ResponseContext $responseContext)
82
    {
83
        $originalRequestId = $responseContext->getInResponseTo();
84
85
        $logger = $this->samlLogger->forAuthentication($originalRequestId);
0 ignored issues
show
Bug introduced by
It seems like $originalRequestId can also be of type null; however, parameter $requestId of Surfnet\SamlBundle\Monol...er::forAuthentication() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
        $logger = $this->samlLogger->forAuthentication(/** @scrutinizer ignore-type */ $originalRequestId);
Loading history...
86
        $logger->notice('Authentication was cancelled by the user, creating Response with AuthnFailed status');
87
88
        $response = $this->responseBuilder
89
            ->createNewResponse($responseContext)
90
            ->setResponseStatus(
91
                Constants::STATUS_RESPONDER,
92
                Constants::STATUS_AUTHN_FAILED,
93
                'Authentication cancelled by user'
94
            )
95
            ->get();
96
97
        $logger->notice(sprintf(
98
            'Responding to request "%s" with response based on response from the remote IdP with response "%s"',
99
            $responseContext->getInResponseTo(),
100
            $response->getId()
101
        ));
102
103
        return $response;
104
    }
105
106
    /**
107
     * @param ResponseContext $responseContext
108
     * @return SAMLResponse
109
     */
110
    public function createRequesterFailureResponse(ResponseContext $responseContext)
111
    {
112
        $response = $this->responseBuilder
113
            ->createNewResponse($responseContext)
114
            ->setResponseStatus(Constants::STATUS_REQUESTER, Constants::STATUS_REQUEST_UNSUPPORTED)
115
            ->get();
116
117
        return $response;
118
    }
119
120
    /**
121
     * @param $context
122
     * @return SAMLResponse
123
     */
124
    public function createResponseFailureResponse($context)
125
    {
126
        $response = $this->responseBuilder
127
            ->createNewResponse($context)
128
            ->setResponseStatus(Constants::STATUS_RESPONDER)
129
            ->get();
130
131
        return $response;
132
    }
133
}
134