Completed
Push — master ( 1e88b2...6b6b05 )
by Boy
03:57
created

ResponseBuilder::isValidResponseSubStatus()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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