Test Failed
Pull Request — master (#48)
by
unknown
05:05
created

Saml2Controller   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 34
c 5
b 0
f 0
dl 0
loc 131
rs 10
wmc 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A metadata() 0 5 1
A acs() 0 29 6
A sls() 0 15 2
A login() 0 5 2
A logout() 0 6 1
A wouldCauseInfiniteLoop() 0 4 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)
47
    {
48
        $errors = $auth->acs();
49
50
        if (!empty($errors)) {
51
            if (config('saml2.debug')) {
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

51
            if (/** @scrutinizer ignore-call */ config('saml2.debug')) {
Loading history...
52
                logger()->debug('[Saml2] Error with IdP SAML Response. [Debug] Assertion: ' . $auth->getBase()->getLastResponseXML());
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()->debug('[Saml2] Error with IdP SAML Response. [Debug] Assertion: ' . $auth->getBase()->getLastResponseXML());
Loading history...
53
            }
54
55
            logger()->error('saml2.error_detail', ['error' => $auth->getLastErrorReason()]);
56
            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

56
            /** @scrutinizer ignore-call */ 
57
            session()->flash('saml2.error_detail', [$auth->getLastErrorReason()]);
Loading history...
57
58
            logger()->error('saml2.error', $errors);
59
            session()->flash('saml2.error', $errors);
60
61
            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

61
            return /** @scrutinizer ignore-call */ redirect(config('saml2.errorRoute'));
Loading history...
62
        }
63
64
        $user = $auth->getSaml2User();
65
66
        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

66
        /** @scrutinizer ignore-call */ 
67
        event(new SignedIn($user, $auth));
Loading history...
67
68
        $redirectUrl = $user->getIntendedUrl();
69
70
        if ($redirectUrl !== null && !$this->wouldCauseInfiniteLoop($auth, $redirectUrl)) {
71
            return redirect($redirectUrl);
72
        }
73
74
        return redirect($auth->getTenant()->relay_state_url ?: config('saml2.loginRoute'));
75
    }
76
77
    private function wouldCauseInfiniteLoop(Auth $auth, $redirectUrl)
78
    {
79
        $loginUrl = route('saml.login', ['uuid' => $auth->getTenant()->uuid]);
0 ignored issues
show
Bug introduced by
The function route 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

79
        $loginUrl = /** @scrutinizer ignore-call */ route('saml.login', ['uuid' => $auth->getTenant()->uuid]);
Loading history...
80
        return $redirectUrl === $loginUrl;
81
    }
82
83
    /**
84
     * Process the SAML Logout Response / Logout Request sent by the IdP.
85
     *
86
     * Fires 'saml2.logoutRequestReceived' event if its valid.
87
     *
88
     * This means the user logged out of the SSO infrastructure, you 'should' log him out locally too.
89
     *
90
     * @param Auth $auth
91
     *
92
     * @return \Illuminate\Support\Facades\Redirect
93
     *
94
     * @throws OneLoginError
95
     * @throws \Exception
96
     */
97
    public function sls(Auth $auth)
98
    {
99
        $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

99
        $errors = $auth->sls(/** @scrutinizer ignore-call */ config('saml2.retrieveParametersFromServer'));
Loading history...
100
101
        if (!empty($errors)) {
102
            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

102
            /** @scrutinizer ignore-call */ 
103
            logger()->error('saml2.error_detail', ['error' => $auth->getLastErrorReason()]);
Loading history...
103
            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

103
            /** @scrutinizer ignore-call */ 
104
            session()->flash('saml2.error_detail', [$auth->getLastErrorReason()]);
Loading history...
104
105
            logger()->error('saml2.error', $errors);
106
            session()->flash('saml2.error', $errors);
107
108
            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

108
            return /** @scrutinizer ignore-call */ redirect(config('saml2.errorRoute'));
Loading history...
109
        }
110
111
        return redirect(config('saml2.logoutRoute')); //may be set a configurable default
112
    }
113
114
    /**
115
     * Initiate a login request.
116
     *
117
     * @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...
118
     * @param Auth $auth
119
     *
120
     * @return void
121
     *
122
     * @throws OneLoginError
123
     */
124
    public function login(Request $request, Auth $auth)
125
    {
126
        $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

126
        $redirectUrl = $auth->getTenant()->relay_state_url ?: /** @scrutinizer ignore-call */ config('saml2.loginRoute');
Loading history...
127
128
        $auth->login($request->query('returnTo', $redirectUrl));
129
    }
130
131
    /**
132
     * Initiate a logout request.
133
     *
134
     * @param Illuminate\Http\Request $request
135
     * @param Auth $auth
136
     *
137
     * @return void
138
     *
139
     * @throws OneLoginError
140
     */
141
    public function logout(Request $request, Auth $auth)
142
    {
143
        $auth->logout(
144
            $request->query('returnTo'),
145
            $request->query('nameId'),
146
            $request->query('sessionIndex')
147
        );
148
    }
149
}
150