1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace flipbox\saml\core\records; |
4
|
|
|
|
5
|
|
|
use flipbox\ember\records\ActiveRecord; |
6
|
|
|
use flipbox\keychain\records\KeyChainRecord; |
7
|
|
|
use flipbox\saml\core\models\GroupOptions; |
8
|
|
|
use SAML2\DOMDocumentFactory; |
9
|
|
|
use SAML2\XML\md\EntityDescriptor; |
10
|
|
|
use yii\db\ActiveQuery; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class AbstractProvider |
14
|
|
|
* @package flipbox\saml\core\records |
15
|
|
|
*/ |
16
|
|
|
abstract class AbstractProvider extends ActiveRecord implements ProviderInterface |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
use traits\EntityDescriptor, traits\KeyChain, traits\MapUser; |
20
|
|
|
|
21
|
|
|
const METADATA_HASH_ALGORITHM = 'sha256'; |
22
|
|
|
const DEFAULT_GROUPS_ATTRIBUTE_NAME = 'groups'; |
23
|
|
|
|
24
|
|
|
protected $metadataModel; |
25
|
|
|
protected $cachedKeychain; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* This method helps initiate the login process for a remote provider. |
29
|
|
|
* When using this method, say your craft site is the SP. This method will be helpful |
30
|
|
|
* on the IDP provider record. Going to this login path will |
31
|
|
|
* initiate the login process for this IDP. Returns null when you getLoginPath for the |
32
|
|
|
* local provider (the current craft site). |
33
|
|
|
* |
34
|
|
|
* @return string|null |
35
|
|
|
*/ |
36
|
|
|
abstract public function getLoginPath(); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Similar to getLoginPath(), this method initiates logout with the intended remote |
40
|
|
|
* provider. |
41
|
|
|
* |
42
|
|
|
* @return string|null |
43
|
|
|
*/ |
44
|
|
|
abstract public function getLogoutPath(); |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritdoc |
48
|
|
|
*/ |
49
|
|
|
public function beforeSave($insert) |
50
|
|
|
{ |
51
|
|
|
if (! $this->entityId) { |
|
|
|
|
52
|
|
|
$this->entityId = $this->getEntityId(); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (is_array($this->mapping)) { |
|
|
|
|
56
|
|
|
$this->serializeMapping(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if ($this->groupOptions instanceof GroupOptions) { |
|
|
|
|
60
|
|
|
$this->serializeGroupOptions(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->sha256 = hash(static::METADATA_HASH_ALGORITHM, $this->metadata); |
|
|
|
|
64
|
|
|
|
65
|
|
|
$this->metadata = $this->getMetadataModel()->toXML()->ownerDocument->saveXML(); |
|
|
|
|
66
|
|
|
|
67
|
|
|
return parent::beforeSave($insert); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
public function getEntityId() |
74
|
|
|
{ |
75
|
|
|
$metadata = $this->getMetadataModel(); |
76
|
|
|
return $metadata->getEntityID(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return EntityDescriptor |
81
|
|
|
*/ |
82
|
|
|
public function getMetadataModel() |
83
|
|
|
{ |
84
|
|
|
|
85
|
|
|
if (! $this->metadataModel && $this->metadata) { |
|
|
|
|
86
|
|
|
$this->metadataModel = new EntityDescriptor( |
87
|
|
|
DOMDocumentFactory::fromString($this->metadata)->documentElement |
|
|
|
|
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this->metadataModel; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param EntityDescriptor $descriptor |
96
|
|
|
* @return $this |
97
|
|
|
*/ |
98
|
|
|
public function setMetadataModel(EntityDescriptor $descriptor) |
99
|
|
|
{ |
100
|
|
|
$this->metadataModel = $descriptor; |
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
public function isIdentityProvider() |
108
|
|
|
{ |
109
|
|
|
return $this->providerType === static::TYPE_IDP; |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
|
|
public function isServiceProvider() |
116
|
|
|
{ |
117
|
|
|
return $this->providerType === static::TYPE_SP; |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public function getType() |
124
|
|
|
{ |
125
|
|
|
return $this->providerType; |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return ActiveQuery |
130
|
|
|
*/ |
131
|
|
|
public function getLink() |
132
|
|
|
{ |
133
|
|
|
return $this->hasOne(LinkRecord::class, [ |
134
|
|
|
'providerId' => 'id', |
135
|
|
|
]); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return ActiveQuery |
140
|
|
|
*/ |
141
|
|
|
public function getKeychain() |
142
|
|
|
{ |
143
|
|
|
return $this->hasOne(KeyChainRecord::class, [ |
144
|
|
|
'id' => 'keyChainId', |
145
|
|
|
])->viaTable(LinkRecord::tableName(), [ |
146
|
|
|
'providerId' => 'id', |
147
|
|
|
]); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param KeyChainRecord $keyChain |
152
|
|
|
* @return AbstractProvider |
153
|
|
|
*/ |
154
|
|
|
public function setKeychain(KeyChainRecord $keyChain) |
155
|
|
|
{ |
156
|
|
|
$this->populateRelation('keychain', $keyChain); |
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param array $mapping |
162
|
|
|
* @return $this |
163
|
|
|
*/ |
164
|
|
|
public function setMapping(array $mapping) |
165
|
|
|
{ |
166
|
|
|
$this->mapping = $mapping; |
|
|
|
|
167
|
|
|
|
168
|
|
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return array |
173
|
|
|
*/ |
174
|
|
|
public function getMapping() |
175
|
|
|
{ |
176
|
|
|
if ($this->hasMapping()) { |
177
|
|
|
$this->unserializeMapping(); |
178
|
|
|
} elseif (! $this->mapping) { |
|
|
|
|
179
|
|
|
$this->mapping = []; |
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $this->mapping; |
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param GroupOptions $groupOptions |
187
|
|
|
* @return $this |
188
|
|
|
*/ |
189
|
|
|
public function setGroupOptions(GroupOptions $groupOptions) |
190
|
|
|
{ |
191
|
|
|
$this->groupOptions = $groupOptions; |
|
|
|
|
192
|
|
|
|
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function getGroupOptions(): GroupOptions |
197
|
|
|
{ |
198
|
|
|
if ($this->hasGroupOptions()) { |
199
|
|
|
$this->unserializeGroupOptions(); |
200
|
|
|
} elseif (! $this->groupOptions) { |
|
|
|
|
201
|
|
|
$this->groupOptions = new GroupOptions(); |
|
|
|
|
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $this->groupOptions; |
|
|
|
|
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @return false|string |
209
|
|
|
*/ |
210
|
|
|
protected function serializeMapping() |
211
|
|
|
{ |
212
|
|
|
return $this->mapping = json_encode($this->mapping); |
|
|
|
|
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @return GroupOptions|string |
217
|
|
|
*/ |
218
|
|
|
protected function unserializeMapping() |
219
|
|
|
{ |
220
|
|
|
if ($this->hasMapping()) { |
221
|
|
|
$this->mapping = json_decode($this->mapping, true); |
|
|
|
|
222
|
|
|
} |
223
|
|
|
return $this->mapping; |
|
|
|
|
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @return false|string |
228
|
|
|
*/ |
229
|
|
|
protected function serializeGroupOptions() |
230
|
|
|
{ |
231
|
|
|
return $this->groupOptions = json_encode($this->groupOptions); |
|
|
|
|
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @return GroupOptions|string |
236
|
|
|
*/ |
237
|
|
|
protected function unserializeGroupOptions() |
238
|
|
|
{ |
239
|
|
|
if ($this->hasGroupOptions()) { |
240
|
|
|
$this->groupOptions = new GroupOptions(json_decode($this->groupOptions, true)); |
|
|
|
|
241
|
|
|
} |
242
|
|
|
return $this->groupOptions; |
|
|
|
|
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @return bool |
247
|
|
|
*/ |
248
|
|
|
public function hasMapping() |
249
|
|
|
{ |
250
|
|
|
return $this->hasJsonProperty('mapping'); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @return bool |
255
|
|
|
*/ |
256
|
|
|
public function hasGroupOptions(): bool |
257
|
|
|
{ |
258
|
|
|
return $this->hasJsonProperty('groupOptions'); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @param string $property |
263
|
|
|
* @return bool |
264
|
|
|
*/ |
265
|
|
|
protected function hasJsonProperty(string $property) |
266
|
|
|
{ |
267
|
|
|
if (! $this->{$property}) { |
268
|
|
|
return false; |
269
|
|
|
} |
270
|
|
|
try { |
271
|
|
|
json_decode($this->{$property}, true); |
272
|
|
|
return true; |
273
|
|
|
} catch (\Exception $e) { |
274
|
|
|
return false; |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
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@property
annotation 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.