-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtfa.form.inc
217 lines (202 loc) · 6.98 KB
/
tfa.form.inc
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
<?php
/**
* @file
* Form-related functions.
*/
/**
* Start of TFA process form.
*
* @param object $account
* User account object.
*
* @return array
* Form array
*/
function tfa_begin_form($account) {
$tfa_form = backdrop_get_form('tfa_form', $account);
// Function added in Backdrop 1.30.0 to support simplified login pages.
if (!function_exists('user_login_page_wrapper')) {
return $tfa_form;
}
return user_login_page_wrapper($tfa_form);
}
/**
* Main TFA process form builder.
*
* Invokes plugin getForm() and handles multi-step fallback.
*/
function tfa_form($form, $form_state, $account) {
$tfa = tfa_get_process($account);
// Check flood tables.
if (_tfa_hit_flood($tfa)) {
module_invoke_all('tfa_flood_hit', $tfa->getContext());
backdrop_goto('user/login');
return;
}
// Get TFA plugins form.
$form = $tfa->getForm($form, $form_state);
if ($tfa->hasFallback()) {
$form['actions']['fallback'] = array(
'#type' => 'submit',
'#value' => t("Can't access your account?"),
'#submit' => array('tfa_form_submit'),
'#limit_validation_errors' => array(),
'#weight' => 20,
);
}
// Set account element.
$form['account'] = array(
'#type' => 'value',
'#value' => $account,
);
return $form;
}
/**
* TFA form validation handler.
*
* Invokes plugin validateForm() and getErrorMessages().
*/
function tfa_form_validate($form, &$form_state) {
// No validation when issuing fallback.
if (isset($form_state['values']['fallback']) && $form_state['values']['op'] === $form_state['values']['fallback']) {
return;
}
$account = $form['account']['#value'];
$tfa = tfa_get_process($account);
if (!$tfa->validateForm($form, $form_state)) {
foreach ($tfa->getErrorMessages() as $element => $message) {
form_set_error($element, $message);
}
$identifier = config_get('user.flood', 'flood_uid_only') ? $account->uid : $account->uid . '-' . ip_address();
flood_register_event('tfa_user', config_get('tfa.settings', 'tfa_user_window'), $identifier);
if (_tfa_hit_flood($tfa)) {
module_invoke_all('tfa_flood_hit', $tfa->getContext());
backdrop_goto('user/login');
}
}
}
/**
* TFA form submission handler.
*
* Invokes plugin submitForm() and processComplete() and handles starting
* multi-step if appropriate.
*/
function tfa_form_submit($form, &$form_state) {
$account = $form['account']['#value'];
$tfa = tfa_get_process($account);
if (!$tfa->submitForm($form, $form_state)) {
// If fallback was triggered TFA process has been reset to new validate
// plugin so run begin and store new context.
if (isset($form_state['values']['fallback']) && $form_state['values']['op'] === $form_state['values']['fallback']) {
$tfa->begin();
}
$context = $tfa->getContext();
tfa_set_context($account, $context);
$form_state['rebuild'] = TRUE;
}
else {
// TFA process is complete so finalize and authenticate user.
$context = tfa_get_context($account);
$tfa->finalize();
tfa_login($account);
// Set redirect based on query parameters, existing $form_state or context.
$form_state['redirect'] = _tfa_form_get_destination($context, $form_state, $account);
}
}
/**
* Get destination and options for $form_state['redirect'].
*
* @param array $context
* TFA account context.
* @param array $form_state
* Form API state array from tfa_form_submit().
* @param object $account
* User account.
*
* @return array
* Array with the redirect destination, and options.
*/
function _tfa_form_get_destination(array $context, array $form_state, $account) {
$options = array();
$destination = 'user';
if (!empty($_GET['pass-reset-token'])) {
// If pass-reset-token was set then complete final process interrupted
// from user_pass_reset().
watchdog('tfa', 'User %name used one-time login link', array('%name' => $account->name));
backdrop_set_message(t('You have just used your one-time login link. It is no longer necessary to use this link to log in. Please change your password.'));
// Let the user's password be changed without the current password check.
$token = backdrop_hash_base64(backdrop_random_bytes(32));
$_SESSION['pass_reset_' . $account->uid] = $token;
$options['query']['pass-reset-token'] = $token;
}
if (!empty($form_state['redirect'])) {
if (is_array($form_state['redirect'])) {
$destination = $form_state['redirect'][0];
$options = $form_state['redirect'][1];
}
else {
$destination = $form_state['redirect'];
}
}
if (!empty($_GET['destination']) && !url_is_external($_GET['destination'])) {
$query_destination = backdrop_parse_url($_GET['destination']);
$destination = $query_destination['path'];
$options['query'] = !empty($options['query']) ?
array_merge($options['query'], $query_destination['query']) : $query_destination['query'];
$options['fragment'] = $query_destination['fragment'];
unset($_GET['destination']);
}
// Context redirect takes final precedence.
if (!empty($context['redirect'])) {
if (is_array($context['redirect'])) {
$destination = $context['redirect'][0];
$options = $context['redirect'][1];
}
else {
$destination = $context['redirect'];
}
}
// Allow other modules to alter final destination. This is better than form
// altering TFA forms because callers would need to check the TFA process
// status to figure out when to act.
backdrop_alter('tfa_complete_redirect', $destination, $options);
return array($destination, $options);
}
/**
* Check if flood has been hit.
*
* @param Tfa $tfa
* Tfa object.
*
* @return bool
* TRUE if the flood has been hit.
*
* phpcs:disable Backdrop.Commenting.FunctionComment.TypeHintMissing
*/
function _tfa_hit_flood($tfa) {
if (config_get('tfa.settings', 'tfa_test_mode')) {
return FALSE;
}
$window = config_get('tfa.settings', 'tfa_flood_window');
$user_window = config_get('tfa.settings', 'tfa_user_window');
$context = $tfa->getContext();
$identifier = config_get('user.flood', 'flood_uid_only') ? $context['uid'] : $context['uid'] . '-' . ip_address();
// Check user specific flood.
if (!flood_is_allowed('tfa_user', config_get('tfa.settings', 'tfa_user_threshold'), $user_window, $identifier)) {
backdrop_set_message(t('You have reached the threshold for incorrect code entry attempts. Please try again in !time minutes.', array('!time' => round($user_window / 60))), 'error');
return TRUE;
}
// Check entire process flood.
elseif (!flood_is_allowed('tfa_begin', config_get('tfa.settings', 'tfa_begin_threshold'), $window)) {
backdrop_set_message(t('You have reached the threshold for TFA attempts. Please try again in !time minutes.', array('!time' => round($window / 60))), 'error');
return TRUE;
}
// Check TFA plugin flood.
elseif (!$tfa->floodIsAllowed($user_window)) {
foreach ($tfa->getErrorMessages() as $message) {
backdrop_set_message($message, 'error');
}
return TRUE;
}
return FALSE;
}