Passed
Pull Request — master (#106)
by
unknown
03:46
created

Auth::getMetadata()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 8
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 13
ccs 0
cts 9
cp 0
crap 6
rs 10
1
<?php
2
3
namespace Slides\Saml2;
4
5
use OneLogin\Saml2\Auth as OneLoginAuth;
6
use OneLogin\Saml2\Error as OneLoginError;
7
use OneLogin\Saml2\Utils as OneLoginUtils;
8
use Slides\Saml2\Events\SignedOut;
9
use Slides\Saml2\Models\Tenant;
10
11
/**
12
 * Class Auth
13
 *
14
 * @package Slides\Saml2
15
 */
16
class Auth
17
{
18
    /**
19
     * The base authentication handler.
20
     *
21
     * @var OneLoginAuth
22
     */
23
    protected $base;
24
25
    /**
26
     * The resolved tenant.
27
     *
28
     * @var Tenant
29
     */
30
    protected $tenant;
31
32
    /**
33
     * Auth constructor.
34
     *
35
     * @param OneLoginAuth $auth
36
     * @param Tenant $tenant
37
     */
38 12
    public function __construct(OneLoginAuth $auth, Tenant $tenant)
39
    {
40 12
        $this->base = $auth;
41 12
        $this->tenant = $tenant;
42 12
    }
43
44
    /**
45
     * Checks whether a user is authenticated.
46
     *
47
     * @return bool
48
     */
49 1
    public function isAuthenticated()
50
    {
51 1
        return $this->base->isAuthenticated();
52
    }
53
54
    /**
55
     * Create a SAML2 user.
56
     *
57
     * @return Saml2User
58
     */
59 3
    public function getSaml2User()
60
    {
61 3
        return new Saml2User($this->base, $this->tenant);
62
    }
63
64
    /**
65
     * The ID of the last message processed.
66
     *
67
     * @return String
68
     */
69
    public function getLastMessageId()
70
    {
71
        return $this->base->getLastMessageId();
72
    }
73
74
    /**
75
     * Initiate a saml2 login flow.
76
     *
77
     * It will redirect! Before calling this, check if user is
78
     * authenticated (here in saml2). That would be true when the assertion was received this request.
79
     *
80
     * @param string|null $returnTo The target URL the user should be returned to after login.
81
     * @param array $parameters Extra parameters to be added to the GET
82
     * @param bool $forceAuthn When true the AuthNReuqest will set the ForceAuthn='true'
83
     * @param bool $isPassive When true the AuthNReuqest will set the Ispassive='true'
84
     * @param bool $stay True if we want to stay (returns the url string) False to redirect
85
     * @param bool $setNameIdPolicy When true the AuthNReuqest will set a nameIdPolicy element
86
     *
87
     * @return string|null If $stay is True, it return a string with the SLO URL + LogoutRequest + parameters
88
     *
89
     * @throws OneLoginError
90
     */
91 1
    public function login(
92
        $returnTo = null,
93
        $parameters = array(),
94
        $forceAuthn = false,
95
        $isPassive = false,
96
        $stay = false,
97
        $setNameIdPolicy = true
98
    )
99
    {
100 1
        return $this->base->login($returnTo, $parameters, $forceAuthn, $isPassive, $stay, $setNameIdPolicy);
101
    }
102
103
    /**
104
     * Initiate a saml2 logout flow. It will close session on all other SSO services.
105
     * You should close local session if applicable.
106
     *
107
     * @param string|null $returnTo The target URL the user should be returned to after logout.
108
     * @param string|null $nameId The NameID that will be set in the LogoutRequest.
109
     * @param string|null $sessionIndex The SessionIndex (taken from the SAML Response in the SSO process).
110
     * @param string|null $nameIdFormat The NameID Format will be set in the LogoutRequest.
111
     * @param bool $stay True if we want to stay (returns the url string) False to redirect
112
     * @param string|null $nameIdNameQualifier The NameID NameQualifier will be set in the LogoutRequest.
113
     *
114
     * @return string|null If $stay is True, it return a string with the SLO URL + LogoutRequest + parameters
115
     *
116
     * @throws OneLoginError
117
     */
118 1
    public function logout(
119
        $returnTo = null,
120
        $nameId = null,
121
        $sessionIndex = null,
122
        $nameIdFormat = null,
123
        $stay = false,
124
        $nameIdNameQualifier = null
125
    )
126
    {
127 1
        $auth = $this->base;
128
129 1
        return $auth->logout($returnTo, [], $nameId, $sessionIndex, $stay, $nameIdFormat, $nameIdNameQualifier);
130
    }
131
132
    /**
133
     * Process the SAML Response sent by the IdP.
134
     *
135
     * @return array|null
136
     *
137
     * @throws OneLoginError
138
     * @throws \OneLogin\Saml2\ValidationError
139
     */
140 3
    public function acs()
141
    {
142 3
        $this->base->processResponse();
143
144 3
        $errors = $this->base->getErrors();
145
146 3
        if (!empty($errors)) {
147 1
            return $errors;
148
        }
149
150 2
        if (!$this->base->isAuthenticated()) {
151 1
            return ['error' => 'Could not authenticate'];
152
        }
153
154 1
        return null;
155
    }
156
157
    /**
158
     * Process the SAML Logout Response / Logout Request sent by the IdP.
159
     *
160
     * Returns an array with errors if it can not logout.
161
     *
162
     * @param bool $retrieveParametersFromServer
163
     *
164
     * @return array
165
     *
166
     * @throws \OneLogin\Saml2\Error
167
     */
168 2
    public function sls($retrieveParametersFromServer = false)
169
    {
170
        $this->base->processSLO(false, null, $retrieveParametersFromServer, function () {
171
            OneLoginUtils::deleteLocalSession();
172
            event(new SignedOut());
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

172
            /** @scrutinizer ignore-call */ 
173
            event(new SignedOut());
Loading history...
173 2
        });
174
175 2
        $errors = $this->base->getErrors();
176
177 2
        return $errors;
178
    }
179
180
    /**
181
     * Get metadata about the local SP. Use this to configure your Saml2 IdP.
182
     *
183
     * @return string
184
     *
185
     * @throws \OneLogin\Saml2\Error
186
     * @throws \Exception
187
     * @throws \InvalidArgumentException If metadata is not correctly set
188
     */
189
    public function getMetadata()
190
    {
191
        $settings = $this->base->getSettings();
192
        $metadata = $settings->getSPMetadata();
193
        $errors = $settings->validateMetadata($metadata);
194
195
        if (!count($errors)) {
196
            return $metadata;
197
        }
198
199
        throw new \InvalidArgumentException(
200
            'Invalid SP metadata: ' . implode(', ', $errors),
201
            OneLoginError::METADATA_SP_INVALID
202
        );
203
    }
204
205
    /**
206
     * Get the last error reason from \OneLogin_Saml2_Auth, useful for error debugging.
207
     *
208
     * @see \OneLogin_Saml2_Auth::getLastErrorReason()
209
     *
210
     * @return string
211
     */
212 1
    public function getLastErrorReason()
213
    {
214 1
        return $this->base->getLastErrorReason();
215
    }
216
217
    /**
218
     * Get the base authentication handler.
219
     *
220
     * @return OneLoginAuth
221
     */
222
    public function getBase()
223
    {
224
        return $this->base;
225
    }
226
227
    /**
228
     * Set a tenant
229
     *
230
     * @param Tenant $tenant
231
     *
232
     * @return void
233
     */
234
    public function setTenant(Tenant $tenant)
235
    {
236
        $this->tenant = $tenant;
237
    }
238
239
    /**
240
     * Get a resolved tenant.
241
     *
242
     * @return Tenant|null
243
     */
244
    public function getTenant()
245
    {
246
        return $this->tenant;
247
    }
248
}
249