Passed
Pull Request — master (#69)
by
unknown
04:58
created

Saml2Controller   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 195
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 68
c 4
b 1
f 0
dl 0
loc 195
rs 10
wmc 20

7 Methods

Rating   Name   Duplication   Size   Complexity  
A metadata() 0 5 1
A acs() 0 28 4
A sls() 0 19 2
A login() 0 17 2
A logout() 0 15 1
C setRequest() 0 16 9
A unsetRequest() 0 12 1
1
<?php
2
3
namespace Slides\Saml2\Http\Controllers;
4
5
use Slides\Saml2\Events\SignedIn;
6
use Slides\Saml2\Auth;
7
use Illuminate\Routing\Controller;
0 ignored issues
show
Bug introduced by
The type Illuminate\Routing\Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Illuminate\Http\Request;
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use OneLogin\Saml2\Error as OneLoginError;
10
11
/**
12
 * Class Saml2Controller
13
 *
14
 * @package Slides\Saml2\Http\Controllers
15
 */
16
class Saml2Controller extends Controller
17
{
18
    /**
19
     * Render the metadata.
20
     *
21
     * @param Auth $auth
22
     *
23
     * @return \Illuminate\Support\Facades\Response
24
     *
25
     * @throws OneLoginError
26
     */
27
    public function metadata(Auth $auth)
28
    {
29
        $metadata = $auth->getMetadata();
30
31
        return response($metadata, 200, ['Content-Type' => 'text/xml']);
0 ignored issues
show
Bug introduced by
The function response was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

31
        return /** @scrutinizer ignore-call */ response($metadata, 200, ['Content-Type' => 'text/xml']);
Loading history...
32
    }
33
34
    /**
35
     * Process the SAML Response sent by the IdP.
36
     *
37
     * Fires "SignedIn" event if a valid user is found.
38
     *
39
     * @param Auth $auth
40
     *
41
     * @return \Illuminate\Support\Facades\Redirect
42
     *
43
     * @throws OneLoginError
44
     * @throws \OneLogin\Saml2\ValidationError
45
     */
46
    public function acs(Auth $auth, $idpName, Request $request)
47
    {
48
        $this->setRequest($request);
49
        $errors = $auth->acs();
50
51
        if (!empty($errors)) {
52
            logger()->error('saml2.error_detail', ['error' => $auth->getLastErrorReason()]);
0 ignored issues
show
Bug introduced by
The function logger was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

52
            /** @scrutinizer ignore-call */ 
53
            logger()->error('saml2.error_detail', ['error' => $auth->getLastErrorReason()]);
Loading history...
53
            session()->flash('saml2.error_detail', [$auth->getLastErrorReason()]);
0 ignored issues
show
Bug introduced by
The function session was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

53
            /** @scrutinizer ignore-call */ 
54
            session()->flash('saml2.error_detail', [$auth->getLastErrorReason()]);
Loading history...
54
55
            logger()->error('saml2.error', $errors);
56
            session()->flash('saml2.error', $errors);
57
58
            return redirect(config('saml2.errorRoute'));
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

58
            return /** @scrutinizer ignore-call */ redirect(config('saml2.errorRoute'));
Loading history...
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

58
            return redirect(/** @scrutinizer ignore-call */ config('saml2.errorRoute'));
Loading history...
59
        }
60
61
        $user = $auth->getSaml2User();
62
63
        event(new SignedIn($user, $auth));
0 ignored issues
show
Bug introduced by
The function event was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

63
        /** @scrutinizer ignore-call */ 
64
        event(new SignedIn($user, $auth));
Loading history...
64
65
        $redirectUrl = $user->getIntendedUrl();
66
67
        $this->unsetRequest();
68
69
        if ($redirectUrl) {
70
            return redirect($redirectUrl);
71
        }
72
73
        return redirect($auth->getTenant()->relay_state_url ?: config('saml2.loginRoute'));
74
    }
75
76
    /**
77
     * Process the SAML Logout Response / Logout Request sent by the IdP.
78
     *
79
     * Fires 'saml2.logoutRequestReceived' event if its valid.
80
     *
81
     * This means the user logged out of the SSO infrastructure, you 'should' log him out locally too.
82
     *
83
     * @param Auth $auth
84
     *
85
     * @return \Illuminate\Support\Facades\Redirect
86
     *
87
     * @throws OneLoginError
88
     * @throws \Exception
89
     */
90
    public function sls(Auth $auth, $idpName, Request $request)
91
    {
92
        $this->setRequest($request);
93
94
        $errors = $auth->sls(config('saml2.retrieveParametersFromServer'));
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

94
        $errors = $auth->sls(/** @scrutinizer ignore-call */ config('saml2.retrieveParametersFromServer'));
Loading history...
95
96
        $this->unsetRequest();
97
98
        if (!empty($errors)) {
99
            logger()->error('saml2.error_detail', ['error' => $auth->getLastErrorReason()]);
0 ignored issues
show
Bug introduced by
The function logger was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

99
            /** @scrutinizer ignore-call */ 
100
            logger()->error('saml2.error_detail', ['error' => $auth->getLastErrorReason()]);
Loading history...
100
            session()->flash('saml2.error_detail', [$auth->getLastErrorReason()]);
0 ignored issues
show
Bug introduced by
The function session was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

100
            /** @scrutinizer ignore-call */ 
101
            session()->flash('saml2.error_detail', [$auth->getLastErrorReason()]);
Loading history...
101
102
            logger()->error('saml2.error', $errors);
103
            session()->flash('saml2.error', $errors);
104
105
            return redirect(config('saml2.errorRoute'));
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

105
            return /** @scrutinizer ignore-call */ redirect(config('saml2.errorRoute'));
Loading history...
106
        }
107
108
        return redirect(config('saml2.logoutRoute')); //may be set a configurable default
109
    }
110
111
    /**
112
     * Initiate a login request.
113
     *
114
     * @param Illuminate\Http\Request $request
0 ignored issues
show
Bug introduced by
The type Slides\Saml2\Http\Contro...Illuminate\Http\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
115
     * @param Auth $auth
116
     *
117
     * @return void
118
     *
119
     * @throws OneLoginError
120
     */
121
    public function login(Request $request, Auth $auth, $idpName)
122
    {
123
        $this->setRequest($request);
124
125
        $redirectUrl = $auth->getTenant()->relay_state_url ?: config('saml2.loginRoute');
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

125
        $redirectUrl = $auth->getTenant()->relay_state_url ?: /** @scrutinizer ignore-call */ config('saml2.loginRoute');
Loading history...
126
127
        $redirectUrl = $auth->login(
128
            $request->query('returnTo', $redirectUrl),
129
            [],
130
            false,
131
            false,
132
            true
133
        );
134
135
        $this->unsetRequest();
136
137
        return redirect($redirectUrl);
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

137
        return /** @scrutinizer ignore-call */ redirect($redirectUrl);
Loading history...
138
    }
139
140
    /**
141
     * Initiate a logout request.
142
     *
143
     * @param Illuminate\Http\Request $request
144
     * @param Auth $auth
145
     *
146
     * @return void
147
     *
148
     * @throws OneLoginError
149
     */
150
    public function logout(Request $request, Auth $auth)
151
    {
152
        $this->setRequest($request);
153
154
        $redirectUrl = $auth->logout(
155
            $request->query('returnTo'),
156
            $request->query('nameId'),
157
            $request->query('sessionIndex'),
158
            null,
159
            true
160
        );
161
162
        $this->unsetRequest();
163
164
        return redirect($redirectUrl);
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

164
        return /** @scrutinizer ignore-call */ redirect($redirectUrl);
Loading history...
165
    }
166
167
    /**
168
     * Add needed superglobals for php-saml that swoole does not provide
169
     *
170
     * @param Request $request
171
     *
172
     * @return void
173
     */
174
    private function setRequest(Request $request)
175
    {
176
        $_POST['SAMLResponse'] = array_key_exists('SAMLResponse', $request->post()) ? $request->post()['SAMLResponse'] : null;
177
        $_GET['SAMLResponse'] = array_key_exists('SAMLResponse', $request->query()) ? $request->query()['SAMLResponse'] : null;
178
        $_GET['SAMLRequest'] = array_key_exists('SAMLRequest', $request->query()) ? $request->query()['SAMLRequest'] : null;
179
        $_GET['RelayState'] = array_key_exists('RelayState', $request->query()) ? $request->query()['RelayState'] : null;
180
        $_GET['Signature'] = array_key_exists('Signature', $request->query()) ? $request->query()['Signature'] : null;
181
        $_REQUEST['RelayState'] = array_key_exists('RelayState', $request->all()) ? $request->all()['RelayState'] : null;
182
183
        if (!empty($request->server->get('HTTP_X_FORWARDED_PROTO'))) {
184
            $_SERVER['HTTP_X_FORWARDED_PROTO'] = $request->server->get('HTTP_X_FORWARDED_PROTO');
185
        }
186
        if (!empty($request->server->get('HTTP_X_FORWARDED_HOST'))) {
187
            $_SERVER['HTTP_X_FORWARDED_HOST'] = $request->server->get('HTTP_X_FORWARDED_HOST');
188
        } else {
189
            $_SERVER['HTTP_HOST'] = parse_url(config('app.url'), PHP_URL_HOST);
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

189
            $_SERVER['HTTP_HOST'] = parse_url(/** @scrutinizer ignore-call */ config('app.url'), PHP_URL_HOST);
Loading history...
190
        }
191
    }
192
193
    /**
194
     * Remove superglobals that were needed for php-saml that swoole does not provide
195
     *
196
     *
197
     * @return void
198
     */
199
    private function unsetRequest()
200
    {
201
        unset(
202
            $_POST['SAMLResponse'],
203
            $_GET['SAMLResponse'],
204
            $_GET['SAMLRequest'],
205
            $_GET['RelayState'],
206
            $_GET['Signature'],
207
            $_REQUEST['RelayState'],
208
            $_SERVER['HTTP_X_FORWARDED_PROTO'],
209
            $_SERVER['HTTP_X_FORWARDED_HOST'],
210
            $_SERVER['HTTP_HOST'],
211
        );
212
    }
213
}