|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Slides\Saml2; |
|
4
|
|
|
|
|
5
|
|
|
use OneLogin\Saml2\Auth as OneLoginAuth; |
|
6
|
|
|
use OneLogin\Saml2\Error as OneLoginError; |
|
7
|
|
|
use Slides\Saml2\Events\SignedOut; |
|
8
|
|
|
use Slides\Saml2\Models\Tenant; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class Auth |
|
12
|
|
|
* |
|
13
|
|
|
* @package Slides\Saml2 |
|
14
|
|
|
*/ |
|
15
|
|
|
class Auth |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* The base authentication handler. |
|
19
|
|
|
* |
|
20
|
|
|
* @var OneLoginAuth |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $base; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The resolved tenant. |
|
26
|
|
|
* |
|
27
|
|
|
* @var Tenant |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $tenant; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Auth constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param OneLoginAuth $auth |
|
35
|
|
|
* @param Tenant $tenant |
|
36
|
|
|
*/ |
|
37
|
12 |
|
public function __construct(OneLoginAuth $auth, Tenant $tenant) |
|
38
|
|
|
{ |
|
39
|
12 |
|
$this->base = $auth; |
|
40
|
12 |
|
$this->tenant = $tenant; |
|
41
|
12 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Checks whether a user is authenticated. |
|
45
|
|
|
* |
|
46
|
|
|
* @return bool |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public function isAuthenticated() |
|
49
|
|
|
{ |
|
50
|
1 |
|
return $this->base->isAuthenticated(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Create a SAML2 user. |
|
55
|
|
|
* |
|
56
|
|
|
* @return Saml2User |
|
57
|
|
|
*/ |
|
58
|
3 |
|
public function getSaml2User() |
|
59
|
|
|
{ |
|
60
|
3 |
|
return new Saml2User($this->base, $this->tenant); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* The ID of the last message processed. |
|
65
|
|
|
* |
|
66
|
|
|
* @return String |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getLastMessageId() |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->base->getLastMessageId(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Initiate a saml2 login flow. |
|
75
|
|
|
* |
|
76
|
|
|
* It will redirect! Before calling this, check if user is |
|
77
|
|
|
* authenticated (here in saml2). That would be true when the assertion was received this request. |
|
78
|
|
|
* |
|
79
|
|
|
* @param string|null $returnTo The target URL the user should be returned to after login. |
|
80
|
|
|
* @param array $parameters Extra parameters to be added to the GET |
|
81
|
|
|
* @param bool $forceAuthn When true the AuthNReuqest will set the ForceAuthn='true' |
|
82
|
|
|
* @param bool $isPassive When true the AuthNReuqest will set the Ispassive='true' |
|
83
|
|
|
* @param bool $stay True if we want to stay (returns the url string) False to redirect |
|
84
|
|
|
* @param bool $setNameIdPolicy When true the AuthNReuqest will set a nameIdPolicy element |
|
85
|
|
|
* |
|
86
|
|
|
* @return string|null If $stay is True, it return a string with the SLO URL + LogoutRequest + parameters |
|
87
|
|
|
* |
|
88
|
|
|
* @throws OneLoginError |
|
89
|
|
|
*/ |
|
90
|
1 |
|
public function login( |
|
91
|
|
|
$returnTo = null, |
|
92
|
|
|
$parameters = array(), |
|
93
|
|
|
$forceAuthn = false, |
|
94
|
|
|
$isPassive = false, |
|
95
|
|
|
$stay = false, |
|
96
|
|
|
$setNameIdPolicy = true |
|
97
|
|
|
) |
|
98
|
|
|
{ |
|
99
|
1 |
|
return $this->base->login($returnTo, $parameters, $forceAuthn, $isPassive, $stay, $setNameIdPolicy); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Initiate a saml2 logout flow. It will close session on all other SSO services. |
|
104
|
|
|
* You should close local session if applicable. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string|null $returnTo The target URL the user should be returned to after logout. |
|
107
|
|
|
* @param string|null $nameId The NameID that will be set in the LogoutRequest. |
|
108
|
|
|
* @param string|null $sessionIndex The SessionIndex (taken from the SAML Response in the SSO process). |
|
109
|
|
|
* @param string|null $nameIdFormat The NameID Format will be set in the LogoutRequest. |
|
110
|
|
|
* @param bool $stay True if we want to stay (returns the url string) False to redirect |
|
111
|
|
|
* @param string|null $nameIdNameQualifier The NameID NameQualifier will be set in the LogoutRequest. |
|
112
|
|
|
* |
|
113
|
|
|
* @return string|null If $stay is True, it return a string with the SLO URL + LogoutRequest + parameters |
|
114
|
|
|
* |
|
115
|
|
|
* @throws OneLoginError |
|
116
|
|
|
*/ |
|
117
|
1 |
|
public function logout( |
|
118
|
|
|
$returnTo = null, |
|
119
|
|
|
$nameId = null, |
|
120
|
|
|
$sessionIndex = null, |
|
121
|
|
|
$nameIdFormat = null, |
|
122
|
|
|
$stay = false, |
|
123
|
|
|
$nameIdNameQualifier = null |
|
124
|
|
|
) |
|
125
|
|
|
{ |
|
126
|
1 |
|
$auth = $this->base; |
|
127
|
|
|
|
|
128
|
1 |
|
return $auth->logout($returnTo, [], $nameId, $sessionIndex, $stay, $nameIdFormat, $nameIdNameQualifier); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Process the SAML Response sent by the IdP. |
|
133
|
|
|
* |
|
134
|
|
|
* @return array|null |
|
135
|
|
|
* |
|
136
|
|
|
* @throws OneLoginError |
|
137
|
|
|
* @throws \OneLogin\Saml2\ValidationError |
|
138
|
|
|
*/ |
|
139
|
3 |
|
public function acs() |
|
140
|
|
|
{ |
|
141
|
3 |
|
$this->base->processResponse(); |
|
142
|
|
|
|
|
143
|
3 |
|
$errors = $this->base->getErrors(); |
|
144
|
|
|
|
|
145
|
3 |
|
if (!empty($errors)) { |
|
146
|
1 |
|
return $errors; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
2 |
|
if (!$this->base->isAuthenticated()) { |
|
150
|
1 |
|
return ['error' => 'Could not authenticate']; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
1 |
|
return null; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Process the SAML Logout Response / Logout Request sent by the IdP. |
|
158
|
|
|
* |
|
159
|
|
|
* Returns an array with errors if it can not logout. |
|
160
|
|
|
* |
|
161
|
|
|
* @param bool $retrieveParametersFromServer |
|
162
|
|
|
* |
|
163
|
|
|
* @return array |
|
164
|
|
|
* |
|
165
|
|
|
* @throws \OneLogin\Saml2\Error |
|
166
|
|
|
*/ |
|
167
|
2 |
|
public function sls($retrieveParametersFromServer = false) |
|
168
|
|
|
{ |
|
169
|
|
|
$this->base->processSLO(false, null, $retrieveParametersFromServer, function () { |
|
170
|
|
|
event(new SignedOut()); |
|
|
|
|
|
|
171
|
2 |
|
}); |
|
172
|
|
|
|
|
173
|
2 |
|
$errors = $this->base->getErrors(); |
|
174
|
|
|
|
|
175
|
2 |
|
return $errors; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Get metadata about the local SP. Use this to configure your Saml2 IdP. |
|
180
|
|
|
* |
|
181
|
|
|
* @return string |
|
182
|
|
|
* |
|
183
|
|
|
* @throws \OneLogin\Saml2\Error |
|
184
|
|
|
* @throws \Exception |
|
185
|
|
|
* @throws \InvalidArgumentException If metadata is not correctly set |
|
186
|
|
|
*/ |
|
187
|
|
|
public function getMetadata() |
|
188
|
|
|
{ |
|
189
|
|
|
$settings = $this->base->getSettings(); |
|
190
|
|
|
$metadata = $settings->getSPMetadata(); |
|
191
|
|
|
$errors = $settings->validateMetadata($metadata); |
|
192
|
|
|
|
|
193
|
|
|
if (!count($errors)) { |
|
194
|
|
|
return $metadata; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
throw new \InvalidArgumentException( |
|
198
|
|
|
'Invalid SP metadata: ' . implode(', ', $errors), |
|
199
|
|
|
OneLoginError::METADATA_SP_INVALID |
|
200
|
|
|
); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Get the last error reason from \OneLogin_Saml2_Auth, useful for error debugging. |
|
205
|
|
|
* |
|
206
|
|
|
* @see \OneLogin_Saml2_Auth::getLastErrorReason() |
|
207
|
|
|
* |
|
208
|
|
|
* @return string |
|
209
|
|
|
*/ |
|
210
|
1 |
|
public function getLastErrorReason() |
|
211
|
|
|
{ |
|
212
|
1 |
|
return $this->base->getLastErrorReason(); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Get the base authentication handler. |
|
217
|
|
|
* |
|
218
|
|
|
* @return OneLoginAuth |
|
219
|
|
|
*/ |
|
220
|
|
|
public function getBase() |
|
221
|
|
|
{ |
|
222
|
|
|
return $this->base; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Set a tenant |
|
227
|
|
|
* |
|
228
|
|
|
* @param Tenant $tenant |
|
229
|
|
|
* |
|
230
|
|
|
* @return void |
|
231
|
|
|
*/ |
|
232
|
|
|
public function setTenant(Tenant $tenant) |
|
233
|
|
|
{ |
|
234
|
|
|
$this->tenant = $tenant; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Get a resolved tenant. |
|
239
|
|
|
* |
|
240
|
|
|
* @return Tenant|null |
|
241
|
|
|
*/ |
|
242
|
|
|
public function getTenant() |
|
243
|
|
|
{ |
|
244
|
|
|
return $this->tenant; |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|