Passed
Push — master ( aedbf8...4cb110 )
by Tim
03:44
created

NegotiateController::enable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 15
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SimpleSAML\Module\negotiate\Controller;
4
5
use Exception;
6
use SimpleSAML\Auth;
7
use SimpleSAML\Configuration;
8
use SimpleSAML\Error;
9
use SimpleSAML\HTTP\RunnableResponse;
10
use SimpleSAML\Logger;
11
use SimpleSAML\Metadata\MetaDataStorageHandler;
12
use SimpleSAML\Module;
13
use SimpleSAML\Module\negotiate\Auth\Source\Negotiate;
14
use SimpleSAML\Session;
15
use SimpleSAML\Utils;
16
use SimpleSAML\XHTML\Template;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
20
/**
21
 * Controller class for the negotiate module.
22
 *
23
 * This class serves the different views available in the module.
24
 *
25
 * @package SimpleSAML\Module\negotiate
26
 */
27
class NegotiateController
28
{
29
    /** @var \SimpleSAML\Configuration */
30
    protected $config;
31
32
    /** @var \SimpleSAML\Session */
33
    protected $session;
34
35
36
    /**
37
     * Controller constructor.
38
     *
39
     * It initializes the global configuration and session for the controllers implemented here.
40
     *
41
     * @param \SimpleSAML\Configuration              $config The configuration to use by the controllers.
42
     * @param \SimpleSAML\Session                    $session The session to use by the controllers.
43
     *
44
     * @throws \Exception
45
     */
46
    public function __construct(
47
        Configuration $config,
48
        Session $session
49
    ) {
50
        $this->config = $config;
51
        $this->session = $session;
52
    }
53
54
55
    /**
56
     * Show enable.
57
     *
58
     * @return \SimpleSAML\XHTML\Template
59
     */
60
    public function enable(): Template
61
    {
62
        $params = [
63
            'secure' => false,
64
            'httponly' => true,
65
        ];
66
67
        Utils\HTTP::setCookie('NEGOTIATE_AUTOLOGIN_DISABLE_PERMANENT', null, $params, false);
68
69
        $this->session->setData('negotiate:disable', 'session', false, 86400); // 24*60*60=86400
70
71
        $t = new Template($this->config, 'negotiate:enable.twig');
72
        $t->data['url'] = Module::getModuleURL('negotiate/disable');
73
74
        return $t;
75
    }
76
77
78
    /**
79
     * Show disable.
80
     *
81
     * @return \SimpleSAML\XHTML\Template
82
     */
83
    public function disable(): Template
84
    {
85
        $params = [
86
            'expire' => mktime(0, 0, 0, 1, 1, 2038),
87
            'secure' => false,
88
            'httponly' => true,
89
        ];
90
91
        Utils\HTTP::setCookie('NEGOTIATE_AUTOLOGIN_DISABLE_PERMANENT', 'True', $params, false);
92
93
        $this->session->setData('negotiate:disable', 'session', false, 86400); //24*60*60=86400
94
95
        $t = new Template($this->config, 'negotiate:disable.twig');
96
        $t->data['url'] = Module::getModuleURL('negotiate/enable');
97
98
        return $t;
99
    }
100
101
102
    /**
103
     * Show retry
104
     *
105
     * @param Request $request The request that lead to this retry operation.
106
     * @return \SimpleSAML\HTTP\RunnableResponse
107
     */
108
    public function retry(Request $request): RunnableResponse
109
    {
110
        $authState = $request->get('AuthState', null);
111
        if ($authState === null) {
112
            throw new Error\BadRequest('Missing required AuthState query parameter.');
113
        }
114
115
        /** @psalm-var array $state */
116
        $state = Auth\State::loadState($authState, Negotiate::STAGEID);
117
118
        $metadata = MetaDataStorageHandler::getMetadataHandler();
119
        $idpid = $metadata->getMetaDataCurrentEntityID('saml20-idp-hosted', 'metaindex');
120
        $idpmeta = $metadata->getMetaData($idpid, 'saml20-idp-hosted');
121
122
        if (isset($idpmeta['auth'])) {
123
            $source = Auth\Source::getById($idpmeta['auth']);
124
            if ($source === null) {
125
                throw new Error\BadRequest('Invalid AuthId "' . $idpmeta['auth'] . '" - not found.');
126
            }
127
128
            $this->session->setData('negotiate:disable', 'session', false, 86400); //24*60*60=86400
129
            Logger::debug('Negotiate(retry) - session enabled, retrying.');
130
131
            return new RunnableResponse([$source, 'authenticate'], [$state]);
132
        }
133
        throw new Exception('Negotiate - retry - no "auth" parameter found in IdP metadata.');
134
    }
135
136
137
    /**
138
     * Show fallback
139
     *
140
     * @param Request $request The request that lead to this retry operation.
141
     * @return \SimpleSAML\HTTP\RunnableResponse
142
     */
143
    public function fallback(Request $request): RunnableResponse
144
    {
145
        $authState = $request->get('AuthState', null);
146
        if ($authState === null) {
147
            throw new Error\BadRequest('Missing required AuthState query parameter.');
148
        }
149
150
        /** @psalm-var array $state */
151
        $state = Auth\State::loadState($authState, Negotiate::STAGEID);
152
153
        Logger::debug('backend - fallback: ' . $state['LogoutState']['negotiate:backend']);
154
155
        return new RunnableResponse([Negotiate::class, 'fallback'], [$state]);
156
    }
157
}
158