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