4 return true; |
4 return true; |
5 |
5 |
6 $plugins->attachHook("userprefs_jbox", "yubikey_ucp_setup();"); |
6 $plugins->attachHook("userprefs_jbox", "yubikey_ucp_setup();"); |
7 $plugins->attachHook("userprefs_body", "return yubikey_user_cp(\$section);"); |
7 $plugins->attachHook("userprefs_body", "return yubikey_user_cp(\$section);"); |
8 $plugins->attachHook("login_form_html", "yubikey_inject_html_login();"); |
8 $plugins->attachHook("login_form_html", "yubikey_inject_html_login();"); |
|
9 $plugins->attachHook("ucp_register_form", "yubikey_inject_registration_form();"); |
|
10 $plugins->attachHook("ucp_register_validate", "yubikey_register_validate(\$error);"); |
|
11 $plugins->attachHook("user_registered", "yubikey_register_insert_key(\$user_id);"); |
9 |
12 |
10 function yubikey_ucp_setup() |
13 function yubikey_ucp_setup() |
11 { |
14 { |
12 userprefs_menu_add('usercp_sec_profile', 'yubiucp_panel_title', makeUrlNS('Special', 'Preferences/Yubikey')); |
15 userprefs_menu_add('usercp_sec_profile', 'yubiucp_panel_title', makeUrlNS('Special', 'Preferences/Yubikey')); |
13 } |
16 } |
266 </td> |
269 </td> |
267 </tr> |
270 </tr> |
268 <?php |
271 <?php |
269 } |
272 } |
270 |
273 |
|
274 function yubikey_inject_registration_form() |
|
275 { |
|
276 global $lang; |
|
277 |
|
278 $preset_otp = isset($_POST['yubikey_otp']) ? $_POST['yubikey_otp'] : false; |
|
279 ?> |
|
280 <tr> |
|
281 <td class="row1"> |
|
282 <?php echo $lang->get('yubiucp_reg_field_otp'); ?><br /> |
|
283 <small><?php |
|
284 if ( getConfig('yubikey_reg_require_otp', '0') == '1' ) |
|
285 echo $lang->get('yubiucp_reg_field_otp_hint_required'); |
|
286 else |
|
287 echo $lang->get('yubiucp_reg_field_otp_hint_optional'); |
|
288 ?></small> |
|
289 </td> |
|
290 <td class="row1"> |
|
291 <?php |
|
292 echo generate_yubikey_field('yubikey_otp', $preset_otp); |
|
293 ?> |
|
294 </td> |
|
295 <td class="row1"> |
|
296 </td> |
|
297 </tr> |
|
298 <?php |
|
299 } |
|
300 |
|
301 function yubikey_register_validate(&$error) |
|
302 { |
|
303 global $db, $session, $paths, $template, $plugins; // Common objects |
|
304 global $lang; |
|
305 |
|
306 $otp_required = getConfig('yubikey_reg_require_otp', '0') == '1'; |
|
307 $have_otp = !empty($_POST['yubikey_otp']); |
|
308 if ( $otp_required && !$have_otp ) |
|
309 { |
|
310 $error = $lang->get('yubiucp_reg_err_otp_required'); |
|
311 return false; |
|
312 } |
|
313 if ( $have_otp ) |
|
314 { |
|
315 $result = yubikey_validate_otp($_POST['yubikey_otp']); |
|
316 if ( !$result['success'] ) |
|
317 { |
|
318 $error = '<b>' . $lang->get('yubiucp_reg_err_otp_invalid') . '</b><br />' . $lang->get("yubiauth_err_{$result['error']}"); |
|
319 return false; |
|
320 } |
|
321 // check for double enrollment |
|
322 $yubi_uid = substr($_POST['yubikey_otp'], 0, 12); |
|
323 // Note on SQL injection: yubikey_validate_otp() has already ensured that this is safe |
|
324 $q = $db->sql_query('SELECT 1 FROM ' . table_prefix . "yubikey WHERE yubi_uid = '$yubi_uid';"); |
|
325 if ( !$q ) |
|
326 $db->_die(); |
|
327 if ( $db->numrows() > 0 ) |
|
328 { |
|
329 $error = '<b>' . $lang->get('yubiucp_reg_err_otp_invalid') . '</b><br />' . $lang->get('yubiucp_err_double_enrollment_single'); |
|
330 return false; |
|
331 } |
|
332 $db->free_result(); |
|
333 } |
|
334 } |
|
335 |
|
336 function yubikey_register_insert_key($user_id) |
|
337 { |
|
338 global $db, $session, $paths, $template, $plugins; // Common objects |
|
339 if ( !empty($_POST['yubikey_otp']) ) |
|
340 { |
|
341 $yubi_uid = $db->escape(substr($_POST['yubikey_otp'], 0, 12)); |
|
342 $q = $db->sql_query('INSERT INTO ' . table_prefix . "yubikey ( user_id, yubi_uid ) VALUES ( $user_id, '$yubi_uid' );"); |
|
343 if ( !$q ) |
|
344 $db->_die(); |
|
345 } |
|
346 } |