ResponseBuilder   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 53
dl 0
loc 93
rs 10
c 0
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isValidResponseSubStatus() 0 22 1
A createNewResponse() 0 17 2
A isValidResponseStatus() 0 7 1
A get() 0 8 1
A setResponseStatus() 0 21 6
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\GatewayBundle\Saml;
20
21
use SAML2\Constants;
22
use SAML2\Response;
23
use Surfnet\SamlBundle\Exception\LogicException;
24
25
class ResponseBuilder
26
{
27
    /**
28
     * @var Response
29
     */
30
    private $response;
31
32
    private ?ResponseContext $responseContext = null;
33
34
    public function createNewResponse(ResponseContext $context): static
35
    {
36
        if ($this->response) {
37
            throw new LogicException('Cannot create a new Response when still building a response.');
38
        }
39
40
        $this->responseContext = $context;
41
42
        $response = new Response();
43
        $response->setDestination($context->getDestination());
44
        $response->setIssuer($context->getIssuer());
45
        $response->setIssueInstant($context->getIssueInstant());
46
        $response->setInResponseTo($context->getInResponseTo());
47
48
        $this->response = $response;
49
50
        return $this;
51
    }
52
53
    public function setResponseStatus(string $status, ?string $subStatus = null, ?string $message = null): static
54
    {
55
        if (!$this->isValidResponseStatus($status)) {
56
            throw new LogicException('Trying to set invalid Response Status');
57
        }
58
59
        if ($subStatus && !$this->isValidResponseSubStatus($subStatus)) {
60
            throw new LogicException('Trying to set invalid Response SubStatus');
61
        }
62
63
        $status = ['Code' => $status];
64
        if ($subStatus) {
65
            $status['SubCode'] = $subStatus;
66
        }
67
        if ($message) {
68
            $status['Message'] = $message;
69
        }
70
71
        $this->response->setStatus($status);
72
73
        return $this;
74
    }
75
76
    public function get()
77
    {
78
        $response = $this->response;
79
80
        $this->response = null;
81
        $this->responseContext = null;
82
83
        return $response;
84
    }
85
86
    private function isValidResponseStatus(string $status): bool
87
    {
88
        return in_array($status, [
89
            Constants::STATUS_SUCCESS,            // weeee!
90
            Constants::STATUS_REQUESTER,          // Something is wrong with the AuthnRequest
91
            Constants::STATUS_RESPONDER,          // Something went wrong with the Response
92
            Constants::STATUS_VERSION_MISMATCH,   // The version of the request message was incorrect
93
        ]);
94
    }
95
96
    private function isValidResponseSubStatus($subStatus): bool
97
    {
98
        return in_array($subStatus, [
99
            Constants::STATUS_AUTHN_FAILED,               // failed authentication
100
            Constants::STATUS_INVALID_ATTR,
101
            Constants::STATUS_INVALID_NAMEID_POLICY,
102
            Constants::STATUS_NO_AUTHN_CONTEXT,           // insufficient Loa or Loa cannot be met
103
            Constants::STATUS_NO_AVAILABLE_IDP,
104
            Constants::STATUS_NO_PASSIVE,
105
            Constants::STATUS_NO_SUPPORTED_IDP,
106
            Constants::STATUS_PARTIAL_LOGOUT,
107
            Constants::STATUS_PROXY_COUNT_EXCEEDED,
108
            Constants::STATUS_REQUEST_DENIED,
109
            Constants::STATUS_REQUEST_UNSUPPORTED,
110
            Constants::STATUS_REQUEST_VERSION_DEPRECATED,
111
            Constants::STATUS_REQUEST_VERSION_TOO_HIGH,
112
            Constants::STATUS_REQUEST_VERSION_TOO_LOW,
113
            Constants::STATUS_RESOURCE_NOT_RECOGNIZED,
114
            Constants::STATUS_TOO_MANY_RESPONSES,
115
            Constants::STATUS_UNKNOWN_ATTR_PROFILE,
116
            Constants::STATUS_UNKNOWN_PRINCIPAL,
117
            Constants::STATUS_UNSUPPORTED_BINDING,
118
        ]);
119
    }
120
}
121