|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace flipbox\saml\core\records; |
|
4
|
|
|
|
|
5
|
|
|
use craft\db\ActiveRecord; |
|
6
|
|
|
use craft\helpers\StringHelper; |
|
7
|
|
|
use craft\records\Site; |
|
8
|
|
|
use flipbox\keychain\records\KeyChainRecord; |
|
9
|
|
|
use flipbox\saml\core\helpers\UrlHelper; |
|
10
|
|
|
use flipbox\saml\core\models\GroupOptions; |
|
11
|
|
|
use flipbox\saml\core\models\MetadataOptions; |
|
12
|
|
|
use flipbox\saml\core\records\traits\Ember; |
|
13
|
|
|
use SAML2\DOMDocumentFactory; |
|
14
|
|
|
use SAML2\XML\md\EntityDescriptor; |
|
15
|
|
|
use yii\db\ActiveQuery; |
|
16
|
|
|
use yii\db\ActiveQueryInterface; |
|
17
|
|
|
use flipbox\saml\core\models\AbstractSettings; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class AbstractProvider |
|
21
|
|
|
* @package flipbox\saml\core\records |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract class AbstractProvider extends ActiveRecord implements ProviderInterface |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
use traits\EntityDescriptor, traits\KeyChain, traits\MapUser, Ember; |
|
27
|
|
|
|
|
28
|
|
|
const METADATA_HASH_ALGORITHM = 'sha256'; |
|
29
|
|
|
const DEFAULT_GROUPS_ATTRIBUTE_NAME = 'groups'; |
|
30
|
|
|
|
|
31
|
|
|
protected $metadataModel; |
|
32
|
|
|
protected $cachedKeychain; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* This method helps initiate the login process for a remote provider. |
|
36
|
|
|
* When using this method, say your craft site is the SP. This method will be helpful |
|
37
|
|
|
* on the IDP provider record. Going to this login path will |
|
38
|
|
|
* initiate the login process for this IDP. Returns null when you getLoginPath for the |
|
39
|
|
|
* local provider (the current craft site). |
|
40
|
|
|
* |
|
41
|
|
|
* @return string|null |
|
42
|
|
|
*/ |
|
43
|
|
|
abstract public function getLoginPath(); |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Similar to getLoginPath(), this method initiates logout with the intended remote |
|
47
|
|
|
* provider. |
|
48
|
|
|
* |
|
49
|
|
|
* @return string|null |
|
50
|
|
|
*/ |
|
51
|
|
|
abstract public function getLogoutPath(); |
|
52
|
|
|
|
|
53
|
|
|
abstract protected function getDefaultSettings():AbstractSettings; |
|
54
|
|
|
|
|
55
|
|
|
public function getLoginRequestEndpoint(AbstractSettings $settings = null) |
|
56
|
|
|
{ |
|
57
|
|
|
return UrlHelper::buildEndpointUrl( |
|
58
|
|
|
$settings ?? $this->getDefaultSettings(), |
|
59
|
|
|
UrlHelper::LOGIN_REQUEST_ENDPOINT, |
|
60
|
|
|
$this, |
|
61
|
|
|
true |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function getLoginEndpoint(AbstractSettings $settings = null) |
|
66
|
|
|
{ |
|
67
|
|
|
return UrlHelper::buildEndpointUrl( |
|
68
|
|
|
$settings ?? $this->getDefaultSettings(), |
|
69
|
|
|
UrlHelper::LOGIN_ENDPOINT, |
|
70
|
|
|
$this, |
|
71
|
|
|
true |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function getLogoutRequestEndpoint(AbstractSettings $settings = null) |
|
76
|
|
|
{ |
|
77
|
|
|
return UrlHelper::buildEndpointUrl( |
|
78
|
|
|
$settings ?? $this->getDefaultSettings(), |
|
79
|
|
|
UrlHelper::LOGOUT_REQUEST_ENDPOINT, |
|
80
|
|
|
$this, |
|
81
|
|
|
true |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function getLogoutEndpoint(AbstractSettings $settings = null) |
|
86
|
|
|
{ |
|
87
|
|
|
return UrlHelper::buildEndpointUrl( |
|
88
|
|
|
$settings ?? $this->getDefaultSettings(), |
|
89
|
|
|
UrlHelper::LOGOUT_ENDPOINT, |
|
90
|
|
|
$this, |
|
91
|
|
|
true |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @inheritDoc |
|
97
|
|
|
*/ |
|
98
|
|
|
public function loadDefaultValues($skipIfSet = true) |
|
99
|
|
|
{ |
|
100
|
|
|
parent::loadDefaultValues($skipIfSet); |
|
101
|
|
|
|
|
102
|
|
|
// fix the issue with postgres pulling zero as default |
|
103
|
|
|
if (!trim($this->uid)) { |
|
104
|
|
|
$this->generateUid(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @throws \Exception |
|
112
|
|
|
*/ |
|
113
|
|
|
public function generateUid() |
|
114
|
|
|
{ |
|
115
|
|
|
if (!$this->uid) { |
|
116
|
|
|
$this->uid = StringHelper::UUID(); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @inheritdoc |
|
122
|
|
|
*/ |
|
123
|
|
|
public function beforeSave($insert) |
|
124
|
|
|
{ |
|
125
|
|
|
if (! $this->entityId) { |
|
|
|
|
|
|
126
|
|
|
$this->entityId = $this->getEntityId(); |
|
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
if (is_array($this->mapping)) { |
|
|
|
|
|
|
130
|
|
|
$this->mapping = json_encode($this->mapping); |
|
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
if ($this->groupOptions instanceof GroupOptions) { |
|
|
|
|
|
|
134
|
|
|
$this->groupOptions = json_encode($this->groupOptions); |
|
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
if ($this->metadataOptions instanceof MetadataOptions) { |
|
|
|
|
|
|
138
|
|
|
$this->metadataOptions = json_encode($this->metadataOptions); |
|
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$this->sha256 = hash(static::METADATA_HASH_ALGORITHM, $this->metadata); |
|
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
$this->metadata = $this->getMetadataModel()->toXML()->ownerDocument->saveXML(); |
|
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
if ($this->site instanceof Site) { |
|
|
|
|
|
|
146
|
|
|
$this->siteId = $this->site->id; |
|
|
|
|
|
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return parent::beforeSave($insert); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @return string |
|
154
|
|
|
*/ |
|
155
|
|
|
public function getEntityId() |
|
156
|
|
|
{ |
|
157
|
|
|
$metadata = $this->getMetadataModel(); |
|
158
|
|
|
return $metadata->getEntityID(); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @return EntityDescriptor |
|
163
|
|
|
*/ |
|
164
|
|
|
public function getMetadataModel() |
|
165
|
|
|
{ |
|
166
|
|
|
|
|
167
|
|
|
if (! $this->metadataModel && $this->metadata) { |
|
|
|
|
|
|
168
|
|
|
$this->metadataModel = new EntityDescriptor( |
|
169
|
|
|
DOMDocumentFactory::fromString($this->metadata)->documentElement |
|
|
|
|
|
|
170
|
|
|
); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
return $this->metadataModel; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @param EntityDescriptor $descriptor |
|
178
|
|
|
* @return $this |
|
179
|
|
|
*/ |
|
180
|
|
|
public function setMetadataModel(EntityDescriptor $descriptor) |
|
181
|
|
|
{ |
|
182
|
|
|
$this->metadataModel = $descriptor; |
|
183
|
|
|
return $this; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @return bool |
|
188
|
|
|
*/ |
|
189
|
|
|
public function isIdentityProvider() |
|
190
|
|
|
{ |
|
191
|
|
|
return $this->providerType === static::TYPE_IDP; |
|
|
|
|
|
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @return bool |
|
196
|
|
|
*/ |
|
197
|
|
|
public function isServiceProvider() |
|
198
|
|
|
{ |
|
199
|
|
|
return $this->providerType === static::TYPE_SP; |
|
|
|
|
|
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @return string |
|
204
|
|
|
*/ |
|
205
|
|
|
public function getType() |
|
206
|
|
|
{ |
|
207
|
|
|
return $this->providerType; |
|
|
|
|
|
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @return ActiveQuery |
|
212
|
|
|
*/ |
|
213
|
|
|
public function getLink() |
|
214
|
|
|
{ |
|
215
|
|
|
return $this->hasOne(LinkRecord::class, [ |
|
216
|
|
|
'providerId' => 'id', |
|
217
|
|
|
]); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* @return ActiveQuery |
|
222
|
|
|
*/ |
|
223
|
|
|
public function getKeychain() |
|
224
|
|
|
{ |
|
225
|
|
|
return $this->hasOne(KeyChainRecord::class, [ |
|
226
|
|
|
'id' => 'keyChainId', |
|
227
|
|
|
])->viaTable(LinkRecord::tableName(), [ |
|
228
|
|
|
'providerId' => 'id', |
|
229
|
|
|
]); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* @param KeyChainRecord $keyChain |
|
234
|
|
|
* @return AbstractProvider |
|
235
|
|
|
*/ |
|
236
|
|
|
public function setKeychain(KeyChainRecord $keyChain) |
|
237
|
|
|
{ |
|
238
|
|
|
$this->populateRelation('keychain', $keyChain); |
|
239
|
|
|
return $this; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* |
|
244
|
|
|
*/ |
|
245
|
|
|
public function setSite(Site $site) |
|
246
|
|
|
{ |
|
247
|
|
|
$this->populateRelation('site', $site); |
|
248
|
|
|
return $this; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Returns the provider's site. |
|
253
|
|
|
* |
|
254
|
|
|
* @return ActiveQueryInterface The relational query object. |
|
255
|
|
|
*/ |
|
256
|
|
|
public function getSite(): ActiveQueryInterface |
|
257
|
|
|
{ |
|
258
|
|
|
return $this->hasOne(Site::class, ['id' => 'siteId']); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* @return \craft\models\Site|null |
|
263
|
|
|
*/ |
|
264
|
|
|
public function getSiteModel() |
|
265
|
|
|
{ |
|
266
|
|
|
$site = $this->getSite()->one(); |
|
267
|
|
|
|
|
268
|
|
|
if ($site instanceof Site) { |
|
269
|
|
|
return new \craft\models\Site($site); |
|
|
|
|
|
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
return null; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* @param array $mapping |
|
277
|
|
|
* @return $this |
|
278
|
|
|
*/ |
|
279
|
|
|
public function setMapping(array $mapping) |
|
280
|
|
|
{ |
|
281
|
|
|
$this->mapping = array_values($mapping); |
|
|
|
|
|
|
282
|
|
|
|
|
283
|
|
|
return $this; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* @return array |
|
288
|
|
|
*/ |
|
289
|
|
|
public function getMapping() |
|
290
|
|
|
{ |
|
291
|
|
|
$mapping = []; |
|
292
|
|
|
if ($this->hasMapping()) { |
|
293
|
|
|
$mapping = json_decode($this->mapping, true); |
|
|
|
|
|
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
return $mapping; |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
public function setMetadataOptions(MetadataOptions $metadataOptions) |
|
300
|
|
|
{ |
|
301
|
|
|
$this->metadataOptions = $metadataOptions; |
|
|
|
|
|
|
302
|
|
|
|
|
303
|
|
|
return $this; |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
public function getMetadataOptions(): MetadataOptions |
|
307
|
|
|
{ |
|
308
|
|
|
$metadataOptions = new MetadataOptions(); |
|
309
|
|
|
if ($this->hasMetadataOptions()) { |
|
310
|
|
|
$metadataOptions = MetadataOptions::jsonUnserialize($this->metadataOptions); |
|
|
|
|
|
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
return $metadataOptions; |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
/** |
|
317
|
|
|
* @param GroupOptions $groupOptions |
|
318
|
|
|
* @return $this |
|
319
|
|
|
*/ |
|
320
|
|
|
public function setGroupOptions(GroupOptions $groupOptions) |
|
321
|
|
|
{ |
|
322
|
|
|
$this->groupOptions = $groupOptions; |
|
|
|
|
|
|
323
|
|
|
|
|
324
|
|
|
return $this; |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
public function getGroupOptions(): GroupOptions |
|
328
|
|
|
{ |
|
329
|
|
|
$groupOptions = new GroupOptions(); |
|
330
|
|
|
if ($this->hasGroupOptions()) { |
|
331
|
|
|
$groupOptions = GroupOptions::jsonUnserialize($this->groupOptions); |
|
|
|
|
|
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
return $groupOptions; |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
/** |
|
338
|
|
|
* @return bool |
|
339
|
|
|
*/ |
|
340
|
|
|
public function hasMapping() |
|
341
|
|
|
{ |
|
342
|
|
|
return $this->hasJsonProperty('mapping'); |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
/** |
|
346
|
|
|
* @return bool |
|
347
|
|
|
*/ |
|
348
|
|
|
public function hasGroupOptions(): bool |
|
349
|
|
|
{ |
|
350
|
|
|
return $this->hasJsonProperty('groupOptions'); |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* @return bool |
|
355
|
|
|
*/ |
|
356
|
|
|
public function hasMetadataOptions(): bool |
|
357
|
|
|
{ |
|
358
|
|
|
return $this->hasJsonProperty('metadataOptions'); |
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
|
|
/** |
|
362
|
|
|
* @param string $property |
|
363
|
|
|
* @return bool |
|
364
|
|
|
*/ |
|
365
|
|
|
protected function hasJsonProperty(string $property) |
|
366
|
|
|
{ |
|
367
|
|
|
if (! $this->{$property}) { |
|
368
|
|
|
return false; |
|
369
|
|
|
} |
|
370
|
|
|
try { |
|
371
|
|
|
json_decode($this->{$property}, true); |
|
372
|
|
|
return true; |
|
373
|
|
|
} catch (\Exception $e) { |
|
374
|
|
|
return false; |
|
375
|
|
|
} |
|
376
|
|
|
} |
|
377
|
|
|
} |
|
378
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.