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