/
RealMeLoginForm.php
243 lines (206 loc) · 7.52 KB
/
RealMeLoginForm.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
class RealMeLoginForm extends LoginForm
{
/**
* @config
* @var bool true if you want the RealMe login form to include jQuery, false if you're including it yourself
*/
private static $include_jquery;
/**
* @config
* @var bool true if you want the RealMe login form JS to be included, false if you're including it yourself
*/
private static $include_javascript;
/**
* @config
* @var bool true if you want the RealMe login form CSS to be included, false if you're including it yourself
*/
private static $include_css;
/**
* @config
* @var string Widget theme can be one of 'default', 'light', or 'dark'. Default is 'default'.
*/
private static $widget_theme;
/**
* @config
* @var string The service name to display in the login box ("To access the [online service], you need a RealMe login.")
*/
private static $service_name_1 = null;
/**
* @config
* @var string The service name to display in the What's RealMe popup header ("To log in to [this service] you need a RealMe login.")
*/
private static $service_name_2 = null;
/**
* @config
* @var string The service name to display in the What's RealMe popup text ("[This service] uses RealMe login.")
*/
private static $service_name_3 = null;
/**
* @var array
*/
private static $allowed_actions = array(
'redirectToRealMe'
);
protected static $action_button_name = 'redirectToRealMe';
/**
* @var string The authentication class tied to this login form
*/
protected $authenticator_class = 'RealMeAuthenticator';
/**
* Returns an instance of this class
*
* @param Controller $controller
* @param string $name
*/
public function __construct($controller, $name)
{
/** @var RealMeService $service */
$service = Injector::inst()->get('RealMeService');
$integrationType = $service->config()->integration_type;
$fields = new FieldList(array(
new HiddenField('AuthenticationMethod', null, $this->authenticator_class)
));
if($integrationType === RealMeService::TYPE_ASSERT) {
$loginButtonContent = ArrayData::create(array(
'Label' => _t('RealMeLoginForm.ASSERTLOGINBUTTON', 'Share your details with {orgname}', '', ['orgname' => $service->config()->metadata_organisation_display_name])
))->renderWith('RealMeLoginButton');
} else {
// Login button
$loginButtonContent = ArrayData::create(array(
'Label' => _t('RealMeLoginForm.LOGINBUTTON', 'Login')
))->renderWith('RealMeLoginButton');
}
$actions = new FieldList(array(
FormAction::create(self::$action_button_name, _t('RealMeLoginForm.LOGINBUTTON', 'LoginAction'))
->setUseButtonTag(true)
->setButtonContent($loginButtonContent)
->setAttribute('class', 'realme_button')
));
// Taken from MemberLoginForm
if (isset($_REQUEST['BackURL'])) {
$backURL = $_REQUEST['BackURL'];
} elseif (Session::get('RealMeBackURL')) {
$backURL = Session::get('RealMeBackURL');
}
if (isset($backURL)) {
// Ensure that $backURL isn't redirecting us back to login form or a RealMe authentication page
if (strpos($backURL, 'Security/login') === false && strpos($backURL, 'Security/realme') === false) {
$fields->push(new HiddenField('BackURL', 'BackURL', $backURL));
}
}
// optionally include requirements {@see /realme/_config/config.yml}
if ($this->config()->include_jquery) {
Requirements::javascript(THIRDPARTY_DIR . "/jquery/jquery.js");
}
if ($this->config()->include_javascript) {
Requirements::javascript(REALME_MODULE_PATH . "/javascript/realme.js");
}
if ($this->config()->include_css) {
Requirements::css(REALME_MODULE_PATH . "/css/realme.css");
}
parent::__construct($controller, $name, $fields, $actions);
}
/**
* Process login form submission
*
* @param array $data
* @param Form $form
* @return SS_HTTPResponse If successfully processed, returns void (SimpleSAMLphp redirects to RealMe)
* @throws SS_HTTPResponse_Exception
*/
public function redirectToRealMe($data, Form $form)
{
/** @var RealMeService $service */
$service = Injector::inst()->get('RealMeService');
// If there's no service, ensure we throw a predictable error
if (null === $service) {
return $this->controller->httpError(500);
}
// This will either redirect to RealMe (via SimpleSAMLphp) or return true/false to indicate logged in state
$loggedIn = $service->enforceLogin();
if (true === $loggedIn) {
return $this->controller->redirect($service->getBackURL());
}
return Security::permissionFailure(
$this->controller,
_t(
'RealMeSecurityExtension.LOGINFAILURE',
'Unfortunately we\'re not able to authenticate you via RealMe right now.'
)
);
}
/**
* Example function exposing a RealMe config option to the login form template
*
* Theme options are: default, light & dark. These are appended to a css class
* on the template which is applied to the element .realme_widget.
*
* @see realme/_config/config.yml
* @see realme/templates/Includes/RealMeLoginForm.ss
*
* @return string
*/
public function getRealMeWidgetTheme()
{
if ($theme = $this->config()->widget_theme) {
return $theme;
}
return 'default';
}
/**
* Gets the service name based on either a config value, or falling back to the $Title specified in SiteConfig
* @param string $name The service name to get from config
* @return string
*/
private function getServiceName($name = 'service_name_1')
{
if ($this->config()->$name) {
return $this->config()->$name;
} else {
return SiteConfig::current_site_config()->Title;
}
}
public function getServiceName1()
{
return $this->getServiceName('service_name_1');
}
public function getServiceName2()
{
return $this->getServiceName('service_name_2');
}
public function getServiceName3()
{
return $this->getServiceName('service_name_3');
}
public function forTemplate()
{
/** @var RealMeService $service */
$service = Injector::inst()->get('RealMeService');
$integrationType = $service->config()->integration_type;
if($integrationType === RealMeService::TYPE_ASSERT) {
$html = $this->renderWith([
'RealMeAssertForm'
]);
// Now that we've rendered, clear message
$this->clearMessage();
return $html;
} else {
return parent::forTemplate();
}
}
/**
* Returns the last error message that the RealMe service provided, if any
* @return string|null
*/
public function RealMeLastError()
{
$message = Session::get('RealMe.LastErrorMessage');
Session::clear('RealMe.LastErrorMessage');
return $message;
}
public function HasRealMeLastError()
{
return Session::get('RealMe.LastErrorMessage') !== null;
}
}