Completed
Push — master ( 0f0d0d...791e0e )
by Tim
15s queued 14s
created

Authorize::forbidden()   B

Complexity

Conditions 11
Paths 73

Size

Total Lines 53
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 34
c 0
b 0
f 0
dl 0
loc 53
rs 7.3166
cc 11
nc 73
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\authorize\Controller;
6
7
use SimpleSAML\Auth;
8
use SimpleSAML\Configuration;
9
use SimpleSAML\Error;
10
use SimpleSAML\Module;
11
use SimpleSAML\Session;
12
use SimpleSAML\XHTML\Template;
13
use Symfony\Component\HttpFoundation\Request;
14
15
/**
16
 * Controller class for the authorize module.
17
 *
18
 * This class serves the different views available in the module.
19
 *
20
 * @package SimpleSAML\Module\authorize
21
 */
22
class Authorize
23
{
24
    /**
25
     * Controller constructor.
26
     *
27
     * It initializes the global configuration and auth source configuration for the controllers implemented here.
28
     *
29
     * @param \SimpleSAML\Configuration              $config The configuration to use by the controllers.
30
     * @param \SimpleSAML\Session                    $session The session to use by the controllers.
31
     *
32
     * @throws \Exception
33
     */
34
    public function __construct(
35
        protected Configuration $config,
36
        protected Session $session,
37
    ) {
38
    }
39
40
41
    /**
42
     * Show a 403 Forbidden page about not authorized to access an application.
43
     *
44
     * @param \Symfony\Component\HttpFoundation\Request $request
45
     * @return \SimpleSAML\XHTML\Template
46
     */
47
    public function forbidden(Request $request): Template
48
    {
49
        $stateId = $request->query->get('StateId', false);
50
        if (!is_string($stateId)) {
51
            throw new Error\BadRequest('Missing required StateId query parameter.');
52
        }
53
54
        /** @var array $state */
55
        $state = Auth\State::loadState($stateId, 'authorize:Authorize');
56
57
        $t = new Template($this->config, 'authorize:authorize_403.twig');
58
        if (isset($state['Source']['auth'])) {
59
            $t->data['source'] = $state['Source']['auth'];
60
        }
61
        if (isset($state['authprocAuthorize_reject_msg'])) {
62
            $t->data['reject_msg'] = $state['authprocAuthorize_reject_msg'];
63
        }
64
65
        if (isset($state['Source']['auth'])) {
66
            $t->data['LogoutURL'] = Module::getModuleURL(
67
                'core/logout/' . urlencode($state['Source']['auth']),
68
            );
69
        }
70
71
        $t->data['allow_reauthentication'] = $state['authprocAuthorize_allow_re_authenticate_on_unauthorized'] ?? false;
72
        $stateId = Auth\State::saveState($state, 'authorize:Authorize');
73
        $t->data['url_reauthentication'] =
74
            Module::getModuleURL('authorize/error/reauthenticate', ['StateId' => $stateId]);
75
76
        if (
77
            isset($state['authprocAuthorize_errorURL'])
78
            && $state['authprocAuthorize_errorURL'] === true
79
            && isset($state['Source']['errorURL'])
80
        ) {
81
            $errorURL = $state['Source']['errorURL'];
82
            $errorURL = str_replace('ERRORURL_CODE', 'AUTHORIZATION_FAILURE', $errorURL);
83
            if (isset($state['saml:sp:State']['core:SP'])) {
84
                $errorURL = str_replace('ERRORURL_RP', urlencode($state['saml:sp:State']['core:SP']), $errorURL);
85
            }
86
            if (isset($state['saml:AuthnInstant'])) {
87
                $errorURL = str_replace('ERRORURL_TS', $state['saml:AuthnInstant'], $errorURL);
88
            } else {
89
                $errorURL = str_replace('ERRORURL_TS', strval(time()), $errorURL);
90
            }
91
            $errorURL = str_replace('ERRORURL_TID', urlencode($this->session->getTrackID()), $errorURL);
92
            if (isset($state['authprocAuthorize_ctx'])) {
93
                $errorURL = str_replace('ERRORURL_CTX', urlencode($state['authprocAuthorize_ctx']), $errorURL);
94
            }
95
            $t->data['errorURL'] = $errorURL;
96
        }
97
98
        $t->setStatusCode(403);
99
        return $t;
100
    }
101
102
    public function reauthenticate(Request $request): void
103
    {
104
        $stateId = $request->query->get('StateId', false);
105
        if (!is_string($stateId)) {
106
            throw new Error\BadRequest('Missing required StateId query parameter.');
107
        }
108
        /** @var array $state */
109
        $state = Auth\State::loadState($stateId, 'authorize:Authorize');
110
111
        $authSource = $state['Source']['auth'];
112
        if (empty($authSource)) {
113
            throw new Error\BadRequest('Missing required auth source.');
114
        }
115
        $parameters = ['ForceAuthn' => true];
116
117
        if (isset($state['\\SimpleSAML\\Auth\\State.restartURL'])) {
118
            $returnToUrl = $state['\\SimpleSAML\\Auth\\State.restartURL'] ;
119
            $parameters['ReturnTo'] = $returnToUrl;
120
        }
121
122
        $auth = new Auth\Simple($authSource);
123
        $auth->login($parameters);
124
    }
125
}
126