1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: dsmrt |
5
|
|
|
* Date: 1/23/18 |
6
|
|
|
* Time: 9:26 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\saml\core\models; |
10
|
|
|
|
11
|
|
|
use craft\base\Model; |
12
|
|
|
use craft\config\GeneralConfig; |
13
|
|
|
use craft\helpers\UrlHelper; |
14
|
|
|
|
15
|
|
|
abstract class AbstractSettings extends Model implements SettingsInterface |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
const ENDPOINT_PREFIX = 'sso'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $myType; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $entityId; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
public $endpointPrefix = self::ENDPOINT_PREFIX; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* This is the endpoint used to initiate login. Set the general.php config for `loginPath` to this. |
37
|
|
|
* @see GeneralConfig::$loginPath |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $loginEndpoint = 'login'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* This is the endpoint used to initiate login. Set the general.php config for `loginPath` to this. |
45
|
|
|
* @see GeneralConfig::$loginPath |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $logoutEndpoint = 'logout'; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* This is the endpoint used to initiate login. Set the general.php config for `loginPath` to this. |
53
|
|
|
* @see GeneralConfig::$loginPath |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected $loginRequestEndpoint = 'login/request'; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* This is the endpoint used to initiate logout. In this case, `logoutPath` cannot be used. |
61
|
|
|
* Point your logout button to this endpoint. |
62
|
|
|
* |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
protected $logoutRequestEndpoint = 'logout/request'; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritdoc |
69
|
|
|
*/ |
70
|
|
|
public function attributes() |
71
|
|
|
{ |
72
|
|
|
return array_merge( |
73
|
|
|
parent::attributes(), |
74
|
|
|
[ |
75
|
|
|
'entityId', |
76
|
|
|
] |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/******************************************* |
81
|
|
|
* ENTITY ID |
82
|
|
|
*******************************************/ |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param $entityId |
86
|
|
|
* @return $this |
87
|
|
|
*/ |
88
|
|
|
public function setEntityId($entityId) |
89
|
|
|
{ |
90
|
|
|
$this->entityId = $entityId; |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return string |
97
|
|
|
* @throws \craft\errors\SiteNotFoundException |
98
|
|
|
*/ |
99
|
|
|
public function getEntityId() |
100
|
|
|
{ |
101
|
|
|
if (! $this->entityId) { |
102
|
|
|
$this->entityId = UrlHelper::baseUrl(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return \Craft::parseEnv($this->entityId); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function getEndpointPrefix() |
109
|
|
|
{ |
110
|
|
|
return \Craft::parseEnv($this->endpointPrefix); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param $myType |
115
|
|
|
* @return $this |
116
|
|
|
*/ |
117
|
|
|
public function setMyType($myType) |
118
|
|
|
{ |
119
|
|
|
$this->myType = $myType; |
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
public function getMyType() |
127
|
|
|
{ |
128
|
|
|
return $this->myType; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
public function getRemoteType() |
135
|
|
|
{ |
136
|
|
|
return $this->getMyType() === self::IDP ? self::SP : self::IDP; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return bool |
141
|
|
|
*/ |
142
|
|
|
public function isIDP() |
143
|
|
|
{ |
144
|
|
|
return $this->getMyType() === self::IDP; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
public function isSP() |
151
|
|
|
{ |
152
|
|
|
return $this->getMyType() === self::SP; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
protected function buildEndpointUrl($url) |
156
|
|
|
{ |
157
|
|
|
return sprintf('/%s/%s', $this->getEndpointPrefix(), $url); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @inheritdoc |
162
|
|
|
*/ |
163
|
|
|
public function getDefaultLoginEndpoint() |
164
|
|
|
{ |
165
|
|
|
return UrlHelper::siteUrl( |
166
|
|
|
$this->buildEndpointUrl( |
167
|
|
|
$this->loginEndpoint |
168
|
|
|
) |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @inheritdoc |
174
|
|
|
*/ |
175
|
|
|
public function getDefaultLogoutEndpoint() |
176
|
|
|
{ |
177
|
|
|
return UrlHelper::siteUrl( |
178
|
|
|
$this->buildEndpointUrl($this->logoutEndpoint) |
179
|
|
|
); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @inheritdoc |
184
|
|
|
*/ |
185
|
|
|
public function getDefaultLogoutRequestEndpoint() |
186
|
|
|
{ |
187
|
|
|
return UrlHelper::siteUrl( |
188
|
|
|
$this->buildEndpointUrl($this->logoutRequestEndpoint) |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @inheritdoc |
194
|
|
|
*/ |
195
|
|
|
public function getDefaultLoginRequestEndpoint() |
196
|
|
|
{ |
197
|
|
|
return UrlHelper::siteUrl( |
198
|
|
|
$this->buildEndpointUrl($this->loginRequestEndpoint) |
199
|
|
|
); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @inheritdoc |
204
|
|
|
*/ |
205
|
|
|
public function getDefaultLoginPath() |
206
|
|
|
{ |
207
|
|
|
return $this->buildEndpointUrl($this->loginEndpoint); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @inheritdoc |
212
|
|
|
*/ |
213
|
|
|
public function getDefaultLogoutPath() |
214
|
|
|
{ |
215
|
|
|
return $this->buildEndpointUrl($this->logoutEndpoint); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @inheritdoc |
220
|
|
|
*/ |
221
|
|
|
public function getDefaultLogoutRequestPath() |
222
|
|
|
{ |
223
|
|
|
return $this->buildEndpointUrl($this->logoutRequestEndpoint); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @inheritdoc |
228
|
|
|
*/ |
229
|
|
|
public function getDefaultLoginRequestPath() |
230
|
|
|
{ |
231
|
|
|
return |
232
|
|
|
$this->buildEndpointUrl($this->loginRequestEndpoint); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|