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; |
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->mapping = \json_encode($this->mapping); |
|
|
|
|
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
|
|
|
* @return array |
162
|
|
|
*/ |
163
|
|
|
public function getMapping() |
164
|
|
|
{ |
165
|
|
|
if (is_string($this->mapping) && $this->hasMapping()) { |
|
|
|
|
166
|
|
|
$this->mapping = json_decode($this->mapping, true); |
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $this->mapping; |
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param GroupOptions $groupOptions |
174
|
|
|
* @return $this |
175
|
|
|
*/ |
176
|
|
|
public function setGroupOptions(GroupOptions $groupOptions) |
177
|
|
|
{ |
178
|
|
|
$this->groupOptions = $groupOptions; |
|
|
|
|
179
|
|
|
|
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function getGroupOptions(): GroupOptions |
184
|
|
|
{ |
185
|
|
|
if ($this->hasGroupOptions()) { |
186
|
|
|
$this->unserializeGroupOptions(); |
187
|
|
|
} elseif (! $this->groupOptions) { |
|
|
|
|
188
|
|
|
$this->groupOptions = new GroupOptions(); |
|
|
|
|
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $this->groupOptions; |
|
|
|
|
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return false|string |
196
|
|
|
*/ |
197
|
|
|
protected function serializeGroupOptions() |
198
|
|
|
{ |
199
|
|
|
return $this->groupOptions = json_encode($this->groupOptions); |
|
|
|
|
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @return GroupOptions|string |
204
|
|
|
*/ |
205
|
|
|
protected function unserializeGroupOptions() |
206
|
|
|
{ |
207
|
|
|
if ($this->hasGroupOptions()) { |
208
|
|
|
$this->groupOptions = new GroupOptions(json_decode($this->groupOptions, true)); |
|
|
|
|
209
|
|
|
} |
210
|
|
|
return $this->groupOptions; |
|
|
|
|
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @return bool |
215
|
|
|
*/ |
216
|
|
|
protected function hasMapping() |
217
|
|
|
{ |
218
|
|
|
return $this->hasJsonProperty('mapping'); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @return bool |
223
|
|
|
*/ |
224
|
|
|
protected function hasGroupOptions(): bool |
225
|
|
|
{ |
226
|
|
|
return $this->hasJsonProperty('groupOptions'); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @param string $property |
231
|
|
|
* @return bool |
232
|
|
|
*/ |
233
|
|
|
protected function hasJsonProperty(string $property) |
234
|
|
|
{ |
235
|
|
|
if (! $this->{$property}) { |
236
|
|
|
return false; |
237
|
|
|
} |
238
|
|
|
try { |
239
|
|
|
json_decode($this->{$property}, true); |
240
|
|
|
return true; |
241
|
|
|
} catch (\Exception $e) { |
242
|
|
|
return false; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
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.